Skip to contentSkip to conversation

iio: Introduce and use value of π

PATCH In review Andy Shevchenko 4 participants 13 messages Nov 7, 2025 – Mar 16 1 message not in the archive

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 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 yet by this series.

All of the converted files were checked in disassembled format before and after the change to see that the value:
a) pre-calculated at compile-time;
b) is the same that is expected.

I didn't put the power-of-10 into the definition names as now they are the maximum that fits the respective signed type.

Series is supposed to go via IIO tree.

Changelog v2:
- redone to avoid unneeded calculations
- collected tags (Hans)

v1: <[email protected]>

Andy Shevchenko (6):
media: vidtv: Rename PI definition to PI_SAMPLES
units: Add 32- and 64-bit signed values of π
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 | 3 ++-
 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                                | 6 ++++++
 6 files changed, 19 insertions(+), 9 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 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
index 9da18eac04b5..b3217d643b1e 100644
@@ -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		180
 
 static 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
#2 lore raw

Re: [PATCH v2 1/6] media: vidtv: Rename PI definition to PI_SAMPLES

Hi Andy,

Just FYI: I'll take this patch. Regardless of the other patches in this series it does make sense. I haven't seen any updates to this series, is it abandoned?

Regards,

Hans

Quoted 37 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.

---
 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
index 9da18eac04b5..b3217d643b1e 100644
@@ -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		180
 
 static 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 */
#3 lore raw

Re: [PATCH v2 1/6] media: vidtv: Rename PI definition to PI_SAMPLES

Quoted 3 lines · Hans Verkuil

Just FYI: I'll take this patch. Regardless of the other patches in this series it does make sense.

Thank you!

Quoted 1 line

I haven't seen any updates to this series, is it abandoned?

There is no consensus which names should be chosen. I have strong opinion about my choice, some do not agree with. Id est I like the idea to have PI as PI and not something longer and confusing. Same for 64-bit, just some short.

My rationale is that, in most of the cases the 32-bit value will be used and having it as is would be nice.

Another point is to have a macro that gives PI rounded to the chosen amount of digits (to avoid / 10 / 100 / et cetera in the users). I don't know how to implement that nicely, there is no loops or alike in C preprocessor.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

PATCH v2 2/6 units: Add 32- and 64-bit signed values of π

In reply to Andy Shevchenko

There are a few drivers that want to have these values, and one more known to come soon. Let's define the values for them.

---
 include/linux/units.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/units.h +6 -0
index f626e212d4ca..5b3293bce04c 100644
@@ -21,6 +21,12 @@
 #define PICO	1000000000000ULL
 #define FEMTO	1000000000000000ULL
 
+/* Value of π * 10⁸ (fits s32 or signed int) */
+#define PI	314159265
+
+/* Value of π * 10¹⁸ (fits s64 or signed long long) */
+#define PI_LL	3141592653589793238LL
+
 /* Hz based multipliers */
 #define NANOHZ_PER_HZ		1000000000UL
 #define MICROHZ_PER_HZ		1000000UL
Signature · 1 line
2.50.1
reviewer
#5 lore raw

Re: [PATCH v2 2/6] units: Add 32- and 64-bit signed values of π

Quoted 13 lines · Andy Shevchenko

There are a few drivers that want to have these values, and one more known to come soon. Let's define the values for them.

---
 include/linux/units.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/units.h +0 -0
index f626e212d4ca..5b3293bce04c 100644

include/linux/units.h line 25

Quoted 5 lines
include/linux/units.h +2 -0
@@ -21,6 +21,12 @@
 #define PICO	1000000000000ULL
 #define FEMTO	1000000000000000ULL
 
+/* Value of π * 10⁸ (fits s32 or signed int) */
+#define PI	314159265

It isn't clear to me at all in the other patches that e.g. DIV_ROUND_UP(PI, 1000) would be π ✕ 10⁴ (rounded to nearest integer, of course).

Calling these PI_E8 and PI_E18 or PI_x10_8 and PI_x10_18 would help to clear that up.

Quoted 7 lines
include/linux/units.h +4 -0
+
+/* Value of π * 10¹⁸ (fits s64 or signed long long) */
+#define PI_LL	3141592653589793238LL
+
 /* Hz based multipliers */
 #define NANOHZ_PER_HZ		1000000000UL
 #define MICROHZ_PER_HZ		1000000UL
reviewer
#6 lore raw

Re: [PATCH v2 2/6] units: Add 32- and 64-bit signed values of π

Quoted 17 lines · David Lechner
Quoted 16 lines · Andy Shevchenko

There are a few drivers that want to have these values, and one more known to come soon. Let's define the values for them.

---
 include/linux/units.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/units.h +0 -0
index f626e212d4ca..5b3293bce04c 100644
@@ -21,6 +21,12 @@
 #define PICO	1000000000000ULL
 #define FEMTO	1000000000000000ULL
 
Quoted 5 lines

include/linux/units.h line 25

Quoted 2 lines
include/linux/units.h +2 -0
+/* Value of π * 10⁸ (fits s32 or signed int) */
+#define PI	314159265

It isn't clear to me at all in the other patches that e.g. DIV_ROUND_UP(PI, 1000) would be π ✕ 10⁴ (rounded to nearest

Typo. Should be DIV_ROUND_UP(PI, 10000) or DIV_ROUND_UP(PI, 10 * KILO).

Quoted 13 lines

integer, of course).

Calling these PI_E8 and PI_E18 or PI_x10_8 and PI_x10_18 would help to clear that up.

Quoted 7 lines
include/linux/units.h +4 -0
+
+/* Value of π * 10¹⁸ (fits s64 or signed long long) */
+#define PI_LL	3141592653589793238LL
+
 /* Hz based multipliers */
 #define NANOHZ_PER_HZ		1000000000UL
 #define MICROHZ_PER_HZ		1000000UL

A message that is not in the archive (sent off-list or not yet received): CAHp75VexvdYyjt1GkbfqOotkFpLeb=io6outJ5dpRqBv2qPNng@mail.gmail.com

maintainer
#8 lore raw

Re: [PATCH v2 2/6] units: Add 32- and 64-bit signed values of π

Quoted 22 lines · On Sat, 8 Nov 2025 21:02:54 +0200 Andy Shevchenko <[email protected]>…

пʼятниця, 7 листопада 2025 р. David Lechner <[email protected]> пише:

Quoted 20 lines · Andy Shevchenko
Quoted 9 lines

There are a few drivers that want to have these values, and one more known to come soon. Let's define the values for them.

include/linux/units.h line 25

Quoted 5 lines
include/linux/units.h +2 -0
@@ -21,6 +21,12 @@
 #define PICO 1000000000000ULL
 #define FEMTO        1000000000000000ULL
 
+/* Value of π * 10⁸ (fits s32 or signed int) */
+#define PI   314159265  

It isn't clear to me at all in the other patches that e.g. DIV_ROUND_UP(PI, 1000) would be π ✕ 10⁴ (rounded to nearest integer, of course).

Quoted 8 lines
Quoted 2 lines

Calling these PI_E8 and PI_E18 or PI_x10_8 and PI_x10_18 would help to clear that up.

This will be an awful and ugly name(s). I fully disagree on such a proposal. The power is chosen to fit the type with maximum precision, no need to explain this in the name.

With no indicate of the multiplier we have to check it every time wwe use it which isn't ideal either.

So I agree with David that we have to indicate this somewhere. Maybe hold the multiplier in a separate define
#define PI_MUL
#define PI_MUL_LL

Then compute the divisions by (PI_MUL) / KILO or something along those lines?

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
include/linux/units.h +4 -0
+
+/* Value of π * 10¹⁸ (fits s64 or signed long long) */
+#define PI_LL        3141592653589793238LL
+
 /* Hz based multipliers */
 #define NANOHZ_PER_HZ                1000000000UL
 #define MICROHZ_PER_HZ               1000000UL  
#9 lore raw

Re: [PATCH v2 2/6] units: Add 32- and 64-bit signed values of π

Quoted 6 lines · Jonathan Cameron
Quoted 4 lines · On Sat, 8 Nov 2025 21:02:54 +0200 Andy Shevchenko <[email protected]>…

пʼятниця, 7 листопада 2025 р. David Lechner <[email protected]> пише:

Quoted 3 lines · Andy Shevchenko
Quoted 2 lines

There are a few drivers that want to have these values, and one more known to come soon. Let's define the values for them.

...

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines

include/linux/units.h line 25

Quoted 2 lines
include/linux/units.h +2 -0
+/* Value of π * 10⁸ (fits s32 or signed int) */
+#define PI   314159265  

It isn't clear to me at all in the other patches that e.g. DIV_ROUND_UP(PI, 1000) would be π ✕ 10⁴ (rounded to nearest integer, of course).

Quoted 6 lines
Quoted 2 lines

Calling these PI_E8 and PI_E18 or PI_x10_8 and PI_x10_18 would help to clear that up.

This will be an awful and ugly name(s). I fully disagree on such a proposal. The power is chosen to fit the type with maximum precision, no need to explain this in the name.

With no indicate of the multiplier we have to check it every time wwe use it which isn't ideal either.

Quoted 6 lines

So I agree with David that we have to indicate this somewhere. Maybe hold the multiplier in a separate define
#define PI_MUL
#define PI_MUL_LL

Then compute the divisions by (PI_MUL) / KILO or something along those lines?

I see, we have a disagreement here, because I don't like to uglify the pure PI definition. So, please abandon this series then.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

include/linux/units.h line 28

Quoted 2 lines
include/linux/units.h +2 -0
+/* Value of π * 10¹⁸ (fits s64 or signed long long) */
+#define PI_LL        3141592653589793238LL

Note, this constant is used a lot in the tests (3+ modules), so if you go with your version, take this into account.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

/* Hz based multipliers */
#define NANOHZ_PER_HZ 1000000000UL
#define MICROHZ_PER_HZ 1000000UL

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

PATCH v2 3/6 media: dvb-frontends: atbm8830: Convert to use PI definition

In reply to Andy Shevchenko

Convert 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
index 778c865085bf..93181e0eb34f 100644
@@ -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 = (u64) 2 * DIV_ROUND_UP(PI, 10000) * (freq - fs);
 		t <<= 22;
 		do_div(t, fs);
 		do_div(t, 1000);
Signature · 1 line
2.50.1
#11 lore raw

PATCH v2 4/6 iio: cros_ec_sensors: Convert to use PI definition

In reply to Andy Shevchenko

Convert to use PI definition instead of open coded value of it.

---
 drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c +2 -1
index 82cef4a12442..e248ba1eb3f9 100644
@@ -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,7 +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,
+			*val2 = div_s64(val64 * DIV_ROUND_UP_ULL(PI_LL, GIGA),
 					180 << (CROS_EC_SENSOR_BITS - 1));
 			ret = IIO_VAL_INT_PLUS_NANO;
 			break;
Signature · 1 line
2.50.1
#12 lore raw

PATCH v2 5/6 iio: frequency: ad9523: Convert to use PI definition

In reply to Andy Shevchenko

Convert 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
index 63c485e9e44c..781e0ada75d9 100644
@@ -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(PI, 100)) /
 			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(PI, 100);
 		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
#13 lore raw

PATCH v2 6/6 iio: position: iqs624-pos: Convert to use PI definition

In reply to Andy Shevchenko

Convert 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
index 8239239c6ee2..24d7a2591858 100644
@@ -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			(PI / 180000)
 #define IQS624_POS_SCALE2			100000
 
 struct iqs624_pos_private {
Signature · 1 line
2.50.1