[PATCH v1 11/12] iio: accel: adxl313: implement power-save on inactivity

Lothar Rubusch posted 12 patches 7 months ago
There is a newer version of this series
[PATCH v1 11/12] iio: accel: adxl313: implement power-save on inactivity
Posted by Lothar Rubusch 7 months ago
Link activity and inactivity to indicate the internal power-saving state.
Add auto-sleep to be linked to inactivity.

Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
 drivers/iio/accel/adxl313.h      |  3 +++
 drivers/iio/accel/adxl313_core.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/accel/adxl313.h b/drivers/iio/accel/adxl313.h
index 1398ac54d19b..eafdf8f57816 100644
--- a/drivers/iio/accel/adxl313.h
+++ b/drivers/iio/accel/adxl313.h
@@ -41,6 +41,9 @@
 #define ADXL313_RATE_BASE		6
 
 #define ADXL313_POWER_CTL_MSK		BIT(3)
+#define ADXL313_POWER_CTL_INACT_MSK	GENMASK(5, 4)
+#define ADXL313_POWER_CTL_LINK		BIT(5)
+#define ADXL313_POWER_CTL_AUTO_SLEEP	BIT(4)
 #define ADXL313_MEASUREMENT_MODE	BIT(3)
 #define ADXL313_MEASUREMENT_STANDBY	0x00
 
diff --git a/drivers/iio/accel/adxl313_core.c b/drivers/iio/accel/adxl313_core.c
index 46ca30abc7fd..aabb4a1b9b9b 100644
--- a/drivers/iio/accel/adxl313_core.c
+++ b/drivers/iio/accel/adxl313_core.c
@@ -392,6 +392,7 @@ static int adxl313_set_act_inact_en(struct adxl313_data *data,
 	unsigned int axis_ctrl;
 	unsigned int threshold;
 	unsigned int inact_time_s;
+	bool act_en, inact_en;
 	bool en;
 	int ret;
 
@@ -423,9 +424,31 @@ static int adxl313_set_act_inact_en(struct adxl313_data *data,
 		en = cmd_en && threshold && inact_time_s;
 	}
 
-	return regmap_update_bits(data->regmap, ADXL313_REG_INT_ENABLE,
-				  adxl313_act_int_reg[type],
-				  en ? adxl313_act_int_reg[type] : 0);
+	ret = regmap_update_bits(data->regmap, ADXL313_REG_INT_ENABLE,
+				 adxl313_act_int_reg[type],
+				 en ? adxl313_act_int_reg[type] : 0);
+	if (ret)
+		return ret;
+
+	/*
+	 * Advanced power saving: Set sleep and link bit only when ACT and INACT
+	 * are set. Check enable against regmap cached values.
+	 */
+	ret = adxl313_is_act_inact_en(data, ADXL313_ACTIVITY, &act_en);
+	if (ret)
+		return ret;
+
+	ret = adxl313_is_act_inact_en(data, ADXL313_INACTIVITY, &inact_en);
+	if (ret)
+		return ret;
+
+	en = en && act_en && inact_en;
+
+	return regmap_update_bits(data->regmap,
+				  ADXL313_REG_POWER_CTL,
+				  ADXL313_POWER_CTL_INACT_MSK,
+				  en ? (ADXL313_POWER_CTL_AUTO_SLEEP | ADXL313_POWER_CTL_LINK)
+					  : 0);
 }
 
 static int adxl313_read_raw(struct iio_dev *indio_dev,
-- 
2.39.5
Re: [PATCH v1 11/12] iio: accel: adxl313: implement power-save on inactivity
Posted by Andy Shevchenko 7 months ago
On Sun, May 18, 2025 at 11:13:20AM +0000, Lothar Rubusch wrote:
> Link activity and inactivity to indicate the internal power-saving state.
> Add auto-sleep to be linked to inactivity.

...

> +	en = en && act_en && inact_en;
> +
> +	return regmap_update_bits(data->regmap,
> +				  ADXL313_REG_POWER_CTL,
> +				  ADXL313_POWER_CTL_INACT_MSK,

> +				  en ? (ADXL313_POWER_CTL_AUTO_SLEEP | ADXL313_POWER_CTL_LINK)
> +					  : 0);

Make it a logical split

				  en ?
				  (ADXL313_POWER_CTL_AUTO_SLEEP | ADXL313_POWER_CTL_LINK) : 0);

-- 
With Best Regards,
Andy Shevchenko