linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Nyekjaer <sean@geanix.com>
To: linux-iio@vger.kernel.org, jic23@kernel.org
Cc: Sean Nyekjaer <sean@geanix.com>,
	lorenzo.bianconi83@gmail.com, denis.ciocca@st.com,
	martin@geanix.com
Subject: [RFC PATCH 1/3] iio: imu: st_lsm6dsx: add wake on accelerometer threshold
Date: Fri, 14 Jun 2019 14:26:02 +0200	[thread overview]
Message-ID: <20190614122604.52935-2-sean@geanix.com> (raw)
In-Reply-To: <20190614122604.52935-1-sean@geanix.com>

Added wakeup-source option for waking on accelerometer events.
If the wakeup-source is specified, we activate this on our
way down to suspend. We start the accelerometer in
low power mode and wait for it to report a wake event.

Signed-off-by: Sean Nyekjaer <sean@geanix.com>
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  2 +
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 70 ++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
index edcd838037cd..ef4327fd57d6 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
@@ -147,6 +147,7 @@ struct st_lsm6dsx_hw {
 	struct device *dev;
 	struct regmap *regmap;
 	int irq;
+	bool irq_wake;
 
 	struct mutex fifo_lock;
 	struct mutex conf_lock;
@@ -155,6 +156,7 @@ struct st_lsm6dsx_hw {
 	u8 enable_mask;
 	u8 ts_sip;
 	u8 sip;
+	u8 wake_threshold;
 
 	u8 *buff;
 
diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
index aebbe0ddd8d8..092c4d02bd4e 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
@@ -36,6 +36,7 @@
 #include <linux/delay.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
+#include <linux/interrupt.h>
 #include <linux/pm.h>
 #include <linux/regmap.h>
 #include <linux/bitfield.h>
@@ -71,6 +72,17 @@
 #define ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR	0x24
 #define ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR	0x26
 
+#define ST_LSM6DSX_REG_TAP_CFG_ADDR		0x58
+#define ST_LSM6DSX_REG_TAP_CFG_INT_EN_MASK	BIT(7)
+#define ST_LSM6DSX_REG_TAP_CFG_INACT_EN_MASK	GENMASK(6, 5)
+
+#define ST_LSM6DSX_DEFAULT_WAKE_THRESH		0
+#define ST_LSM6DSX_REG_WAKE_UP_THS_ADDR		0x5B
+#define ST_LSM6DSX_REG_WAKE_UP_THS_THRES_MASK	GENMASK(5, 0)
+
+#define ST_LSM6DSX_REG_MD1_CFG_ADDR		0x5E
+#define ST_LSM6DSX_REG_MD1_CFG_INT1_WU_MASK	BIT(5)
+
 #define ST_LSM6DSX_ACC_FS_2G_GAIN		IIO_G_TO_M_S_2(61)
 #define ST_LSM6DSX_ACC_FS_4G_GAIN		IIO_G_TO_M_S_2(122)
 #define ST_LSM6DSX_ACC_FS_8G_GAIN		IIO_G_TO_M_S_2(244)
@@ -428,6 +440,19 @@ static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 odr)
 				  ST_LSM6DSX_SHIFT_VAL(val, reg->mask));
 }
 
+static int st_lsm6dsx_set_wake_threshold(struct st_lsm6dsx_hw *hw, u8 threshold)
+{
+	int err = 0;
+
+	err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_WAKE_UP_THS_ADDR,
+				 ST_LSM6DSX_REG_WAKE_UP_THS_THRES_MASK,
+				 threshold);
+	if (!err)
+		hw->wake_threshold = threshold;
+
+	return err;
+}
+
 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor)
 {
 	int err;
@@ -754,6 +779,8 @@ static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
 	if (err < 0)
 		return err;
 
+	st_lsm6dsx_set_wake_threshold(hw, ST_LSM6DSX_DEFAULT_WAKE_THRESH);
+
 	return st_lsm6dsx_init_hw_timer(hw);
 }
 
@@ -853,6 +880,10 @@ int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
 			return err;
 	}
 
+	if (dev->of_node)
+		if (of_property_read_bool(dev->of_node, "wakeup-source"))
+			device_init_wakeup(dev, true);
+
 	return 0;
 }
 EXPORT_SYMBOL(st_lsm6dsx_probe);
@@ -879,6 +910,26 @@ static int __maybe_unused st_lsm6dsx_suspend(struct device *dev)
 	if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS)
 		err = st_lsm6dsx_flush_fifo(hw);
 
+	if (device_may_wakeup(dev)) {
+		sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
+		err = st_lsm6dsx_sensor_enable(sensor);
+
+		/* Enable inactivity function - low power ACC, GYRO powered-down */
+		err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_TAP_CFG_ADDR,
+				ST_LSM6DSX_REG_TAP_CFG_INT_EN_MASK |
+				ST_LSM6DSX_REG_TAP_CFG_INACT_EN_MASK,
+				ST_LSM6DSX_REG_TAP_CFG_INT_EN_MASK |
+				ST_LSM6DSX_REG_TAP_CFG_INACT_EN_MASK);
+
+		/* Enable wakeup interrupt */
+		err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_MD1_CFG_ADDR,
+				ST_LSM6DSX_REG_MD1_CFG_INT1_WU_MASK,
+				ST_LSM6DSX_REG_MD1_CFG_INT1_WU_MASK);
+
+		/* Enable wake from IRQ */
+		hw->irq_wake = (enable_irq_wake(hw->irq) == 0);
+	}
+
 	return err;
 }
 
@@ -888,6 +939,25 @@ static int __maybe_unused st_lsm6dsx_resume(struct device *dev)
 	struct st_lsm6dsx_sensor *sensor;
 	int i, err = 0;
 
+	if (device_may_wakeup(dev) && hw->irq_wake) {
+		disable_irq_wake(hw->irq);
+		hw->irq_wake = false;
+
+		sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
+		err = st_lsm6dsx_sensor_disable(sensor);
+
+		/* Disable inactivity function */
+		err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_TAP_CFG_ADDR,
+				ST_LSM6DSX_REG_TAP_CFG_INT_EN_MASK |
+				ST_LSM6DSX_REG_TAP_CFG_INACT_EN_MASK,
+				0);
+
+		/* Disable wakeup interrupt */
+		err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_MD1_CFG_ADDR,
+				ST_LSM6DSX_REG_MD1_CFG_INT1_WU_MASK,
+				0);
+	}
+
 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
 		sensor = iio_priv(hw->iio_devs[i]);
 		if (!(hw->enable_mask & BIT(sensor->id)))
-- 
2.22.0


  reply	other threads:[~2019-06-14 12:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-14 12:26 [RFC PATCH 0/3] io: imu: st_lsm6dsx: wake on acc event Sean Nyekjaer
2019-06-14 12:26 ` Sean Nyekjaer [this message]
2019-06-15  8:30   ` [RFC PATCH 1/3] iio: imu: st_lsm6dsx: add wake on accelerometer threshold Lorenzo Bianconi
2019-06-14 12:26 ` [RFC PATCH 2/3] iio: imu: st_lsm6dsx: add wake on accelerometer enable hook in sysfs Sean Nyekjaer
2019-06-15  8:38   ` Lorenzo Bianconi
2019-06-16 13:30   ` Jonathan Cameron
2019-06-17 16:29     ` Sean Nyekjaer
2019-06-14 12:26 ` [RFC PATCH 3/3] iio: imu: st_lsm6dsx: add wake on accelerometer threshold " Sean Nyekjaer
2019-06-15  8:35   ` Lorenzo Bianconi
2019-06-16 13:42     ` Jonathan Cameron
2019-06-16 13:24 ` [RFC PATCH 0/3] io: imu: st_lsm6dsx: wake on acc event Jonathan Cameron
2019-06-16 13:27   ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190614122604.52935-2-sean@geanix.com \
    --to=sean@geanix.com \
    --cc=denis.ciocca@st.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=lorenzo.bianconi83@gmail.com \
    --cc=martin@geanix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).