iio: Introduce and use value of π
Subsystems (5)
cover iio: Introduce and use value of π
There are a few drivers that use value of π (small Greek PI) of different precision. Instead of hard coding over and over convert them to use a defined constant, which one of the patches in this series introduces. No functional changes involved.
Note, the respective IIO macros are not converted as it might include unwanted churn, and hard to use definitions in the initialisers.
Andy Shevchenko (6):
media: vidtv: Rename PI definition to PI_SAMPLES
units: Add value of π * 10⁹
media: dvb-frontends: atbm8830: Convert to use PI definition
iio: cros_ec_sensors: Convert to use PI definition
iio: frequency: ad9523: Convert to use PI definition
iio: position: iqs624-pos: Convert to use PI definition
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c | 4 ++-- drivers/iio/frequency/ad9523.c | 5 +++-- drivers/iio/position/iqs624-pos.c | 3 ++- drivers/media/dvb-frontends/atbm8830.c | 5 +++-- drivers/media/test-drivers/vidtv/vidtv_s302m.c | 6 +++--- include/linux/units.h | 3 +++ 6 files changed, 16 insertions(+), 10 deletions(-)
Signature · 1 line
2.50.1
PATCH 1/6 media: vidtv: Rename PI definition to PI_SAMPLES
The definition of PI in the driver is not the actual value in radians, but rather degrees. Since we are going to have a value in radians defined in a global header, rename this definition to avoid potential collisions. No functional changes.
--- drivers/media/test-drivers/vidtv/vidtv_s302m.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/test-drivers/vidtv/vidtv_s302m.c +3 -3
@@ -45,7 +45,7 @@#define FF_S302M_DEFAULT_PTS_OFFSET 100000/* Used by the tone generator: number of samples for PI */ -#define PI 180 +#define PI_SAMPLES 180static const u8 reverse[256 ] = {/* from ffmpeg */
@@ -259,10 +259,10 @@ static u16 vidtv_s302m_get_sample(struct vidtv_encoder *e)if (!ctx->last_tone)return 0x8000 ; - pos = (2 *PI * ctx->note_offset * ctx->last_tone) /S302M_SAMPLING_RATE_HZ ; + pos = (2 *PI_SAMPLES * ctx->note_offset * ctx->last_tone) /S302M_SAMPLING_RATE_HZ ; ctx->note_offset++; -return (fixp_sin32 (pos % (2 *PI )) >>16 ) +0x8000 ; +return (fixp_sin32 (pos % (2 *PI_SAMPLES )) >>16 ) +0x8000 ; }/* bug somewhere */
Signature · 1 line
2.50.1
Re: [PATCH v1 1/6] media: vidtv: Rename PI definition to PI_SAMPLES
Quoted 6 lines · Andy Shevchenko
The definition of PI in the driver is not the actual value in radians, but rather degrees. Since we are going to have a value in radians defined in a global header, rename this definition to avoid potential collisions. No functional changes.
Signed-off-by Andy Shevchenko <[email protected]>
Feel free to merge this through a non-media subsystem. If you want me to take this as a separate media subsystem patch, then let me know.
Regards,
Hans
Quoted 30 lines
--- drivers/media/test-drivers/vidtv/vidtv_s302m.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/media/test-drivers/vidtv/vidtv_s302m.c +3
-3@@ -45,7 +45,7 @@#define FF_S302M_DEFAULT_PTS_OFFSET 100000/* Used by the tone generator: number of samples for PI */ -#define PI 180 +#define PI_SAMPLES 180static const u8 reverse[256 ] = {/* from ffmpeg */ @@ -259,10 +259,10 @@ static u16 vidtv_s302m_get_sample(struct vidtv_encoder *e)if (!ctx->last_tone)return 0x8000 ; - pos = (2 *PI * ctx->note_offset * ctx->last_tone) /S302M_SAMPLING_RATE_HZ ; + pos = (2 *PI_SAMPLES * ctx->note_offset * ctx->last_tone) /S302M_SAMPLING_RATE_HZ ; ctx->note_offset++; -return (fixp_sin32 (pos % (2 *PI )) >>16 ) +0x8000 ; +return (fixp_sin32 (pos % (2 *PI_SAMPLES )) >>16 ) +0x8000 ; }/* bug somewhere */
PATCH 2/6 units: Add value of π * 10⁹
In reply to Andy ShevchenkoThere are a few drivers that want to have this value, and at least one known to come soon. Let's define a value for them.
--- include/linux/units.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/units.h +3 -0
@@ -21,6 +21,9 @@#define PICO 1000000000000ULL#define FEMTO 1000000000000000ULL +/* Value of π * 10⁹ */ +#define PI 3141592653LL +/* Hz based multipliers */ #define NANOHZ_PER_HZ 1000000000UL#define MICROHZ_PER_HZ 1000000UL
Signature · 1 line
2.50.1
Jonathan Cameron
maintainerRe: [PATCH v1 2/6] units: Add value of π * 10⁹
Quoted 2 lines · On Mon, 27 Oct 2025 15:34:51 +0100 Andy Shevchenko…
There are a few drivers that want to have this value, and at least one known to come soon. Let's define a value for them.
Is there any way we can make the x10^9 bit obvious in the naming? Or do something a bit nasty like defining a macro along the lines of
PI(scale)?
e.g. PI(NANO), PI(10000)
Quoted 20 lines
Signed-off-by Andy Shevchenko <[email protected]>--- include/linux/units.h | 3 +++ 1 file changed, 3 insertions(+)include/linux/units.h +3
-0@@ -21,6 +21,9 @@#define PICO 1000000000000ULL#define FEMTO 1000000000000000ULL +/* Value of π * 10⁹ */ +#define PI 3141592653LL +/* Hz based multipliers */ #define NANOHZ_PER_HZ 1000000000UL#define MICROHZ_PER_HZ 1000000UL
David Lechner
reviewerRe: [PATCH v1 2/6] units: Add value of π * 10⁹
Quoted 12 lines · Jonathan Cameron
Quoted 2 lines · On Mon, 27 Oct 2025 15:34:51 +0100 Andy Shevchenko…
There are a few drivers that want to have this value, and at least one known to come soon. Let's define a value for them.
Is there any way we can make the x10^9 bit obvious in the naming? Or do something a bit nasty like defining a macro along the lines of
PI(scale)?
e.g. PI(NANO), PI(10000)
This was my first thought when looking at this as well.
Or something like PI_x10(6).
Re: [PATCH v1 2/6] units: Add value of π * 10⁹
Quoted 10 lines · David Lechner
Quoted 9 lines · Jonathan Cameron
Quoted 2 lines · On Mon, 27 Oct 2025 15:34:51 +0100 Andy Shevchenko…
There are a few drivers that want to have this value, and at least one known to come soon. Let's define a value for them.
Is there any way we can make the x10^9 bit obvious in the naming? Or do something a bit nasty like defining a macro along the lines of
Quoted 6 lines
Quoted 3 lines
PI(scale)?
e.g. PI(NANO), PI(10000)This was my first thought when looking at this as well.
Or something like
PI_x10(6).
We need a good macro that may _at compile-time_ convert 64-bit input to 32-bit value that may be suitable for 32-bit arithmetics / architectures.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 2/6] units: Add value of π * 10⁹
In reply to Andy ShevchenkoQuoted 13 lines · On Mon, 27 Oct 2025 15:34:51 +0100 Andy Shevchenko…
There are a few drivers that want to have this value, and at least one known to come soon. Let's define a value for them.
Signed-off-by Andy Shevchenko <[email protected]>--- include/linux/units.h | 3 +++ 1 file changed, 3 insertions(+)include/linux/units.h +0
-0
include/linux/units.h line 25
Quoted 5 lines
include/linux/units.h +2
-0@@ -21,6 +21,9 @@#define PICO 1000000000000ULL#define FEMTO 1000000000000000ULL +/* Value of π * 10⁹ */ +#define PI 3141592653LL
Is that the right value?
IIRC the next digits are 58979 (I used to know the next few as well) which means it should be rounded up.
David
Quoted 4 lines
include/linux/units.h +1
-0+/* Hz based multipliers */ #define NANOHZ_PER_HZ 1000000000UL#define MICROHZ_PER_HZ 1000000UL
Re: [PATCH v1 2/6] units: Add value of π * 10⁹
Quoted 6 lines · David Laight
Quoted 2 lines · On Mon, 27 Oct 2025 15:34:51 +0100 Andy Shevchenko…
There are a few drivers that want to have this value, and at least one known to come soon. Let's define a value for them.
Quoted 6 lines
include/linux/units.h line 25
Quoted 2 lines
include/linux/units.h +2
-0+/* Value of π * 10⁹ */ +#define PI 3141592653LLIs that the right value?
IIRC the next digits are 58979 (I used to know the next few as well) which means it should be rounded up.
Right, today I have the same thought that actually ChromeOS driver has a off-by-one issue there.
Btw, do you know if we can have compile-time divisions that can make 32-bit constants out of the 64-bit input? DIV_ROUND_CLOSEST_ULL() doesn't seem allow that.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 2/6] units: Add value of π * 10⁹
Quoted 8 lines · On Tue, 28 Oct 2025 10:08:04 +0200 Andy Shevchenko…
Quoted 7 lines · David Laight
Quoted 4 lines · On Mon, 27 Oct 2025 15:34:51 +0100 Andy Shevchenko…
There are a few drivers that want to have this value, and at least one known to come soon. Let's define a value for them.
+/* Value of π * 10⁹ */
Thinks - should non-ascii characters be allowed in comments. They can cause grief.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
+#define PI 3141592653LL
Is that the right value?
IIRC the next digits are 58979 (I used to know the next few as well) which means it should be rounded up.Right, today I have the same thought that actually ChromeOS driver has a off-by-one issue there.
Not as though it is likely to make a difference.
If any code needs anything more accurate than 3.1416 it probably cares whether the value is rounded down or up.
Quoted 3 lines
Btw, do you know if we can have compile-time divisions that can make 32-bit constants out of the 64-bit input?
DIV_ROUND_CLOSEST_ULL()doesn't seem allow that.
Not sure, I did wonder how much thought had gone into the LL suffix. The value used will fit in u32 - but then any maths becomes unsigned. OTOH using LL forces 64bit maths on 32bit - not good.
I suspect it would be better to drop a digit so the value fits in a signed 32bit variable.
The other issue is whether scaling by a power of 10 is even right. If the code has to rescale the value using divides you really want to divide by a big power of 2 not 10.
So multiplying by 2**29 may be more useful.
David
PATCH 3/6 media: dvb-frontends: atbm8830: Convert to use PI definition
In reply to Andy ShevchenkoConvert to use PI definition instead of open coded value of it.
--- drivers/media/dvb-frontends/atbm8830.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/dvb-frontends/atbm8830.c +3 -2
@@ -6,7 +6,8 @@* Copyright (C) 2009 David T.L. Wong <[email protected]> */ -#include <asm/div64.h> +#include <linux/math64.h> +#include <linux/units.h> #include <media/dvb_frontend.h> #include "atbm8830.h"
@@ -112,7 +113,7 @@ static int set_if_freq(struct atbm_state *priv, u32 freq /*in kHz*/)if (freq !=0 ) {/* 2 * PI * (freq - fs) / fs * (2 ^ 22) */ - t = (u64 )2 *31416 * (freq - fs); + t =2 *DIV_ROUND_UP_ULL (PI ,100000 ) * (freq - fs); t <<=22 ;do_div (t, fs);do_div (t,1000 );
Signature · 1 line
2.50.1
Re: [PATCH v1 3/6] media: dvb-frontends: atbm8830: Convert to use PI definition
Quoted 3 lines · Andy Shevchenko
Convert to use PI definition instead of open coded value of it.
Signed-off-by Andy Shevchenko <[email protected]>
Feel free to merge this through a non-media subsystem. If you want me to take this as a separate media subsystem patch once units.h is patched, then let me know.
Regards,
Hans
Quoted 27 lines
--- drivers/media/dvb-frontends/atbm8830.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/media/dvb-frontends/atbm8830.c +3
-2@@ -6,7 +6,8 @@* Copyright (C) 2009 David T.L. Wong <[email protected]> */ -#include <asm/div64.h> +#include <linux/math64.h> +#include <linux/units.h> #include <media/dvb_frontend.h> #include "atbm8830.h" @@ -112,7 +113,7 @@ static int set_if_freq(struct atbm_state *priv, u32 freq /*in kHz*/)if (freq !=0 ) {/* 2 * PI * (freq - fs) / fs * (2 ^ 22) */ - t = (u64 )2 *31416 * (freq - fs); + t =2 *DIV_ROUND_UP_ULL (PI ,100000 ) * (freq - fs); t <<=22 ;do_div (t, fs);do_div (t,1000 );
PATCH 4/6 iio: cros_ec_sensors: Convert to use PI definition
In reply to Andy ShevchenkoConvert to use PI definition instead of open coded value of it.
--- drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c +2 -2
@@ -21,6 +21,7 @@#include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_device.h> +#include <linux/units.h> #include <linux/slab.h> #define CROS_EC_SENSORS_MAX_CHANNELS 4
@@ -120,8 +121,7 @@ static int cros_ec_sensors_read(struct iio_dev *indio_dev,* loss. Round to the nearest integer. */ *val =0 ; - *val2 =div_s64 (val64 *3141592653ULL , -180 << (CROS_EC_SENSOR_BITS -1 )); + *val2 =div_s64 (val64 *PI ,180 << (CROS_EC_SENSOR_BITS -1 )); ret =IIO_VAL_INT_PLUS_NANO ;break ;case MOTIONSENSE_TYPE_MAG :
Signature · 1 line
2.50.1
PATCH 5/6 iio: frequency: ad9523: Convert to use PI definition
In reply to Andy ShevchenkoConvert to use PI definition instead of open coded value of it.
--- drivers/iio/frequency/ad9523.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iio/frequency/ad9523.c +3 -2
@@ -15,6 +15,7 @@#include <linux/err.h> #include <linux/module.h> #include <linux/delay.h> +#include <linux/units.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h>
@@ -652,7 +653,7 @@ static int ad9523_read_raw(struct iio_dev *indio_dev,AD9523_CLK_DIST_DIV_REV (ret);return IIO_VAL_INT ;case IIO_CHAN_INFO_PHASE : - code = (AD9523_CLK_DIST_DIV_PHASE_REV (ret) *3141592 ) / + code = (AD9523_CLK_DIST_DIV_PHASE_REV (ret) *DIV_ROUND_UP_ULL (PI ,1000 )) /AD9523_CLK_DIST_DIV_REV (ret); *val = code /1000000 ; *val2 = code %1000000 ;
@@ -701,7 +702,7 @@ static int ad9523_write_raw(struct iio_dev *indio_dev,break ;case IIO_CHAN_INFO_PHASE : code = val *1000000 + val2 %1000000 ; - tmp = (code *AD9523_CLK_DIST_DIV_REV (ret)) /3141592 ; + tmp = (code *AD9523_CLK_DIST_DIV_REV (reg)) /DIV_ROUND_UP_ULL (PI ,1000 ); tmp =clamp (tmp,0 ,63 ); reg &= ~AD9523_CLK_DIST_DIV_PHASE (~0 ); reg |=AD9523_CLK_DIST_DIV_PHASE (tmp);
Signature · 1 line
2.50.1
PATCH 6/6 iio: position: iqs624-pos: Convert to use PI definition
In reply to Andy ShevchenkoConvert to use PI definition instead of open coded value of it.
--- drivers/iio/position/iqs624-pos.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/position/iqs624-pos.c +2 -1
@@ -15,10 +15,11 @@#include <linux/notifier.h> #include <linux/platform_device.h> #include <linux/regmap.h> +#include <linux/units.h> #define IQS624_POS_DEG_OUT 0x16 -#define IQS624_POS_SCALE1 (314159 / 180) +#define IQS624_POS_SCALE1 DIV_ROUND_UP_ULL(PI, 1800000)#define IQS624_POS_SCALE2 100000struct iqs624_pos_private {
Signature · 1 line
2.50.1