[PATCH v2] iio: imu: use min() to improve code

Qianfeng Rong posted 1 patch 1 month, 2 weeks ago
.../imu/inv_icm42600/inv_icm42600_buffer.c    | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
[PATCH v2] iio: imu: use min() to improve code
Posted by Qianfeng Rong 1 month, 2 weeks ago
Use min() to reduce code in inv_icm42600_buffer_update_fifo_period()
and inv_icm42600_buffer_update_watermark(), and improve readability.

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
v2: Remove the redundant variable and comment as suggested by David.
---
 .../imu/inv_icm42600/inv_icm42600_buffer.c    | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
index 7c4ed981db04..ca744aaee542 100644
--- a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
+++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
@@ -5,6 +5,7 @@
 
 #include <linux/kernel.h>
 #include <linux/device.h>
+#include <linux/minmax.h>
 #include <linux/mutex.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
@@ -100,7 +101,7 @@ ssize_t inv_icm42600_fifo_decode_packet(const void *packet, const void **accel,
 
 void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st)
 {
-	u32 period_gyro, period_accel, period;
+	u32 period_gyro, period_accel;
 
 	if (st->fifo.en & INV_ICM42600_SENSOR_GYRO)
 		period_gyro = inv_icm42600_odr_to_period(st->conf.gyro.odr);
@@ -112,12 +113,7 @@ void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st)
 	else
 		period_accel = U32_MAX;
 
-	if (period_gyro <= period_accel)
-		period = period_gyro;
-	else
-		period = period_accel;
-
-	st->fifo.period = period;
+	st->fifo.period = min(period_gyro, period_accel);
 }
 
 int inv_icm42600_buffer_set_fifo_en(struct inv_icm42600_state *st,
@@ -204,7 +200,7 @@ int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st)
 {
 	size_t packet_size, wm_size;
 	unsigned int wm_gyro, wm_accel, watermark;
-	u32 period_gyro, period_accel, period;
+	u32 period_gyro, period_accel;
 	u32 latency_gyro, latency_accel, latency;
 	bool restore;
 	__le16 raw_wm;
@@ -237,13 +233,8 @@ int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st)
 			latency = latency_gyro - (latency_accel % latency_gyro);
 		else
 			latency = latency_accel - (latency_gyro % latency_accel);
-		/* use the shortest period */
-		if (period_gyro <= period_accel)
-			period = period_gyro;
-		else
-			period = period_accel;
 		/* all this works because periods are multiple of each others */
-		watermark = latency / period;
+		watermark = latency / min(period_gyro, period_accel);
 		if (watermark < 1)
 			watermark = 1;
 		/* update effective watermark */
-- 
2.34.1
Re: [PATCH v2] iio: imu: use min() to improve code
Posted by David Lechner 1 month, 2 weeks ago
On 8/16/25 7:05 AM, Qianfeng Rong wrote:
> Use min() to reduce code in inv_icm42600_buffer_update_fifo_period()
> and inv_icm42600_buffer_update_watermark(), and improve readability.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
Reviewed-by: David Lechner <dlechner@baylibre.com>
Re: [PATCH v2] iio: imu: use min() to improve code
Posted by Jonathan Cameron 1 month, 2 weeks ago
On Sat, 16 Aug 2025 11:56:24 -0500
David Lechner <dlechner@baylibre.com> wrote:

> On 8/16/25 7:05 AM, Qianfeng Rong wrote:
> > Use min() to reduce code in inv_icm42600_buffer_update_fifo_period()
> > and inv_icm42600_buffer_update_watermark(), and improve readability.
> > 
> > Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> > ---  
> Reviewed-by: David Lechner <dlechner@baylibre.com>
> 
Applied with tweak to patch title to include inv_icm42600.
When people are deciding whether to look at a patch or not
having info on what driver it applies to is always useful.

Jonathan