mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Martin Kepplinger <martink@posteo.de>
To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de,
	pmeerw@pmeerw.net, christoph.muellner@theobroma-systems.com,
	mfuzzey@parkeon.com
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Martin Kepplinger <martink@posteo.de>
Subject: [PATCH 4/4] iio: mma8452: add low_power mode
Date: Thu,  3 Mar 2016 09:24:04 +0100	[thread overview]
Message-ID: <1456993444-21320-5-git-send-email-martink@posteo.de> (raw)
In-Reply-To: <1456993444-21320-1-git-send-email-martink@posteo.de>

This adds a mode of operation that consumes less power by lesser
oversampling. It's exposed in IIO sysfs as in_accelX_power_mode, as
documented.

It consumes roughly half the power the default low_noise mode does.
See the datasheet for details.

Signed-off-by: Martin Kepplinger <martink@posteo.de>
Signed-off-by: Christoph Muellner <christoph.muellner@theobroma-systems.com>
---
 drivers/iio/accel/mma8452.c | 55 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index 5ca0d16..5b5abec 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -75,6 +75,9 @@
 #define  MMA8452_CTRL_DR_DEFAULT		0x4 /* 50 Hz sample frequency */
 #define MMA8452_CTRL_REG2			0x2b
 #define  MMA8452_CTRL_REG2_RST			BIT(6)
+#define  MMA8452_CTRL_REG2_MODS_MASK		0x1b
+#define  MMA8452_CTRL_REG2_MODS_NORMAL		0x00
+#define  MMA8452_CTRL_REG2_MODS_LOW_POWER	0x1b
 #define MMA8452_CTRL_REG4			0x2d
 #define MMA8452_CTRL_REG5			0x2e
 #define MMA8452_OFF_X				0x2f
@@ -950,6 +953,56 @@ static struct attribute_group mma8452_event_attribute_group = {
 	.attrs = mma8452_event_attributes,
 };
 
+static const char * const mma8452_power_modes[] = {"low_noise", "low_power"};
+
+static int mma8452_get_power_mode(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *chan)
+{
+	struct mma8452_data *data = iio_priv(indio_dev);
+	int reg;
+
+	reg = i2c_smbus_read_byte_data(data->client,
+				       MMA8452_CTRL_REG2);
+	if (reg < 0)
+		return reg;
+
+	return !(reg & MMA8452_CTRL_REG2_MODS_MASK);
+}
+
+static int mma8452_set_power_mode(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *chan,
+				  unsigned int mode)
+{
+	struct mma8452_data *data = iio_priv(indio_dev);
+	int reg;
+
+	reg = i2c_smbus_read_byte_data(data->client,
+				       MMA8452_CTRL_REG2);
+	if (reg < 0)
+		return reg;
+
+	reg &= ~MMA8452_CTRL_REG2_MODS_MASK;
+	if (mode)
+		reg |= MMA8452_CTRL_REG2_MODS_LOW_POWER;
+	else
+		reg |= MMA8452_CTRL_REG2_MODS_NORMAL;
+
+	return mma8452_change_config(data, MMA8452_CTRL_REG2, reg);
+}
+
+static const struct iio_enum mma8452_power_mode_enum = {
+	.items = mma8452_power_modes,
+	.num_items = ARRAY_SIZE(mma8452_power_modes),
+	.get = mma8452_get_power_mode,
+	.set = mma8452_set_power_mode,
+};
+
+static const struct iio_chan_spec_ext_info mma8452_ext_info[] = {
+	IIO_ENUM("power_mode", true, &mma8452_power_mode_enum),
+	IIO_ENUM_AVAILABLE("power_mode", &mma8452_power_mode_enum),
+	{ },
+};
+
 #define MMA8452_FREEFALL_CHANNEL(modifier) { \
 	.type = IIO_ACCEL, \
 	.modified = 1, \
@@ -987,6 +1040,7 @@ static struct attribute_group mma8452_event_attribute_group = {
 	}, \
 	.event_spec = mma8452_transient_event, \
 	.num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
+	.ext_info = mma8452_ext_info, \
 }
 
 #define MMA8652_CHANNEL(axis, idx, bits) { \
@@ -1007,6 +1061,7 @@ static struct attribute_group mma8452_event_attribute_group = {
 	}, \
 	.event_spec = mma8452_motion_event, \
 	.num_event_specs = ARRAY_SIZE(mma8452_motion_event), \
+	.ext_info = mma8452_ext_info, \
 }
 
 static const struct iio_chan_spec mma8451_channels[] = {
-- 
2.1.4

  parent reply	other threads:[~2016-03-03  8:27 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-03  8:24 iio: mma8452: power saving features for v4.7 Martin Kepplinger
2016-03-03  8:24 ` [PATCH 1/4] iio: mma8452: coding style fixes Martin Kepplinger
2016-03-05 17:19   ` Jonathan Cameron
2016-03-03  8:24 ` [PATCH 2/4] iio: mma8452: avoid switching to active because of config change Martin Kepplinger
2016-03-05 17:23   ` Jonathan Cameron
2016-03-03  8:24 ` [PATCH 3/4] iio: mma8452: add support for runtime power management Martin Kepplinger
2016-03-06 12:59   ` Jonathan Cameron
2016-03-03  8:24 ` Martin Kepplinger [this message]
2016-03-05 17:40   ` [PATCH 4/4] iio: mma8452: add low_power mode Jonathan Cameron
2016-03-06 15:49     ` Martin Kepplinger
2016-03-09 21:17       ` 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=1456993444-21320-5-git-send-email-martink@posteo.de \
    --to=martink@posteo.de \
    --cc=christoph.muellner@theobroma-systems.com \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfuzzey@parkeon.com \
    --cc=pmeerw@pmeerw.net \
    /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

all inboxes | Powered by JetHome®