Skip to contentSkip to conversation

units: complement the set of Hz units

PATCH Applied Dmitry Rokosov 2 participants 5 messages Aug 1, 2022 – Aug 28, 2022

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

v3 cover units: complement the set of Hz units

During msa311 accel IIO driver development

https://lore.kernel.org/linux-iio/[email protected]/

Andy requested to use proper units in the hz->ms calculation. Current units.h header doesn't have milli, micro and nano HZ coefficients, so some drivers (in the IIO subsystem) implement their own copies for that.

The current patchset resolves such a problem and intoduces general MILLIHZ_PER_HZ, MICROHZ_PER_HZ and NANOHZ_PER_HZ definitions in the units.h, and fixes all drivers which duplicate these units.

Changes:
* v2->v3:
- changed UHZ_PER_HZ to MICROHZ_PER_HZ and NHZ_PER_HZ to NANOHZ_PER_HZ to save name consistency for all new HZ units

* v1->v2:
- changed MHZ_PER_HZ to a different name as Andy suggested (suppose MILLIHZ_PER_HZ is good enough)

Dmitry Rokosov (3):
units: complement the set of Hz units
iio: accel: adxl345: use HZ macro from units.h
iio: common: scmi_sensors: use HZ macro from units.h

 drivers/iio/accel/adxl345_core.c           | 7 ++++---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 8 ++++----
 include/linux/units.h                      | 3 +++
 3 files changed, 11 insertions(+), 7 deletions(-)
Signature · 1 line
2.36.0
#1 lore raw

PATCH v3 1/3 units: complement the set of Hz units

Currently, Hz units do not have milli, micro and nano Hz coefficients. Some drivers (IIO especially) use their analogues to calculate appropriate Hz values. This patch includes them to units.h definitions, so they can be used from different kernel places.

---
 include/linux/units.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/units.h +3 -0
index 681fc652e3d7..2793a41e73a2 100644
@@ -20,6 +20,9 @@
 #define PICO	1000000000000ULL
 #define FEMTO	1000000000000000ULL
 
+#define NANOHZ_PER_HZ		1000000000UL
+#define MICROHZ_PER_HZ		1000000UL
+#define MILLIHZ_PER_HZ		1000UL
 #define HZ_PER_KHZ		1000UL
 #define KHZ_PER_MHZ		1000UL
 #define HZ_PER_MHZ		1000000UL
Signature · 1 line
2.36.0
#2 lore raw

PATCH v3 2/3 iio: accel: adxl345: use HZ macro from units.h

In reply to Dmitry Rokosov

Remove duplicated definition of NHZ_PER_HZ, because it's available in the units.h as NANOHZ_PER_HZ.

---
 drivers/iio/accel/adxl345_core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/accel/adxl345_core.c +4 -3
index 370bfec1275a..1919e0089c11 100644
@@ -10,6 +10,7 @@
 #include <linux/module.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
+#include <linux/units.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -32,7 +33,6 @@
 
 #define ADXL345_BW_RATE			GENMASK(3, 0)
 #define ADXL345_BASE_RATE_NANO_HZ	97656250LL
-#define NHZ_PER_HZ			1000000000LL
 
 #define ADXL345_POWER_CTL_MEASURE	BIT(3)
 #define ADXL345_POWER_CTL_STANDBY	0x00
@@ -139,7 +139,7 @@ static int adxl345_read_raw(struct iio_dev *indio_dev,
 
 		samp_freq_nhz = ADXL345_BASE_RATE_NANO_HZ <<
 				(regval & ADXL345_BW_RATE);
-		*val = div_s64_rem(samp_freq_nhz, NHZ_PER_HZ, val2);
+		*val = div_s64_rem(samp_freq_nhz, NANOHZ_PER_HZ, val2);
 
 		return IIO_VAL_INT_PLUS_NANO;
 	}
@@ -164,7 +164,8 @@ static int adxl345_write_raw(struct iio_dev *indio_dev,
 				    ADXL345_REG_OFS_AXIS(chan->address),
 				    val / 4);
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		n = div_s64(val * NHZ_PER_HZ + val2, ADXL345_BASE_RATE_NANO_HZ);
+		n = div_s64(val * NANOHZ_PER_HZ + val2,
+			    ADXL345_BASE_RATE_NANO_HZ);
 
 		return regmap_update_bits(data->regmap, ADXL345_REG_BW_RATE,
 					  ADXL345_BW_RATE,
Signature · 1 line
2.36.0
#3 lore raw

PATCH v3 3/3 iio: common: scmi_sensors: use HZ macro from units.h

In reply to Dmitry Rokosov

Remove duplicated definition of UHZ_PER_HZ, because it's available in the units.h as MICROHZ_PER_HZ.

---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +4 -4
index 793d628db55f..54ccf19ab2bb 100644
@@ -18,6 +18,7 @@
 #include <linux/scmi_protocol.h>
 #include <linux/time.h>
 #include <linux/types.h>
+#include <linux/units.h>
 
 #define SCMI_IIO_NUM_OF_AXIS 3
 
@@ -130,7 +131,6 @@ static const struct iio_buffer_setup_ops scmi_iio_buffer_ops = {
 static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
 {
 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
-	const unsigned long UHZ_PER_HZ = 1000000UL;
 	u64 sec, mult, uHz, sf;
 	u32 sensor_config;
 	char buf[32];
@@ -145,7 +145,7 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
 		return err;
 	}
 
-	uHz = val * UHZ_PER_HZ + val2;
+	uHz = val * MICROHZ_PER_HZ + val2;
 
 	/*
 	 * The seconds field in the sensor interval in SCMI is 16 bits long
@@ -156,10 +156,10 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
 	 * count the number of characters
 	 */
 	sf = (u64)uHz * 0xFFFF;
-	do_div(sf,  UHZ_PER_HZ);
+	do_div(sf,  MICROHZ_PER_HZ);
 	mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
 
-	sec = int_pow(10, mult) * UHZ_PER_HZ;
+	sec = int_pow(10, mult) * MICROHZ_PER_HZ;
 	do_div(sec, uHz);
 	if (sec == 0) {
 		dev_err(&iio_dev->dev,
Signature · 1 line
2.36.0
maintainer
#4 lore raw
In reply to Dmitry Rokosov
Quoted 11 lines · On Mon, 1 Aug 2022 14:37:23 +0000 Dmitry Rokosov <[email protected]>…

During msa311 accel IIO driver development

https://lore.kernel.org/linux-iio/[email protected]/

Andy requested to use proper units in the hz->ms calculation. Current units.h header doesn't have milli, micro and nano HZ coefficients, so some drivers (in the IIO subsystem) implement their own copies for that.

The current patchset resolves such a problem and intoduces general MILLIHZ_PER_HZ, MICROHZ_PER_HZ and NANOHZ_PER_HZ definitions in the units.h, and fixes all drivers which duplicate these units.

Ok, I'm just about convinced this is worth doing given the precedence of similar cases. Applied to the togreg branch of iio.git and pushed out as testing.

Thanks,

Jonathan

Quoted 20 lines

Changes:
* v2->v3:
- changed UHZ_PER_HZ to MICROHZ_PER_HZ and NHZ_PER_HZ to NANOHZ_PER_HZ to save name consistency for all new HZ units

* v1->v2:
- changed MHZ_PER_HZ to a different name as Andy suggested (suppose MILLIHZ_PER_HZ is good enough)

Dmitry Rokosov (3):
units: complement the set of Hz units
iio: accel: adxl345: use HZ macro from units.h
iio: common: scmi_sensors: use HZ macro from units.h

 drivers/iio/accel/adxl345_core.c           | 7 ++++---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 8 ++++----
 include/linux/units.h                      | 3 +++
 3 files changed, 11 insertions(+), 7 deletions(-)