All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] iio: mma8452: add freefall detection for Freescale's accelerometers
@ 2016-01-13 11:24 Martin Kepplinger
  2016-01-13 11:39 ` kbuild test robot
  2016-01-13 11:40 ` kbuild test robot
  0 siblings, 2 replies; 3+ messages in thread
From: Martin Kepplinger @ 2016-01-13 11:24 UTC (permalink / raw)
  To: jic23, knaack.h, lars, pmeerw, christoph.muellner, mfuzzey
  Cc: linux-iio, linux-kernel, Martin Kepplinger, Martin Kepplinger

This adds freefall event detection to the supported devices. It adds
the in_accel_x&y&z_mag_falling_en iio event attribute, which activates
freefall mode.

In freefall mode, the current acceleration magnitude (AND combination
of all axis values) is compared to the specified threshold.
If it falls under the threshold (in_accel_mag_falling_value),
the appropriate IIO event code is generated.

This is what the sysfs "events" directory for these devices looks
like after this change:

-rw-r--r--    4096 Oct 23 08:45 in_accel_mag_falling_period
-rw-r--r--    4096 Oct 23 08:45 in_accel_mag_falling_value
-rw-r--r--    4096 Oct 23 08:45 in_accel_mag_rising_period
-rw-r--r--    4096 Oct 23 08:45 in_accel_mag_rising_value
-r--r--r--    4096 Oct 23 08:45 in_accel_scale
-rw-r--r--    4096 Oct 23 08:45 in_accel_x&y&z_mag_falling_en
-rw-r--r--    4096 Oct 23 08:45 in_accel_x_mag_rising_en
-rw-r--r--    4096 Oct 23 08:45 in_accel_y_mag_rising_en
-rw-r--r--    4096 Oct 23 08:45 in_accel_z_mag_rising_en

Signed-off-by: Martin Kepplinger <martin.kepplinger@theobroma-systems.com>
Signed-off-by: Christoph Muellner <christoph.muellner@theobroma-systems.com>
---
revision history
----------------
v1:
	initial post
v2:
	build all from correct event and channel spec structs
v3:
	rising and falling are treated as equal now. Until last time, I had
	misunderstood the iio events' user API definition. This works and
	values always reflect the current state of operation.


 drivers/iio/accel/mma8452.c | 160 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 143 insertions(+), 17 deletions(-)

diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index ccc632a..4c63d36 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -15,7 +15,7 @@
  *
  * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
  *
- * TODO: orientation / freefall events, autosleep
+ * TODO: orientation events, autosleep
  */
 
 #include <linux/module.h>
@@ -416,6 +416,46 @@ fail:
 	return ret;
 }
 
+static int mma8452_freefall_mode_enabled(struct mma8452_data *data)
+{
+	int val;
+	const struct mma_chip_info *chip = data->chip_info;
+
+	val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
+	if (val < 0)
+		return val;
+
+	return !(val & MMA8452_FF_MT_CFG_OAE);
+}
+
+static int mma8452_set_freefall_mode(struct mma8452_data *data, u8 state)
+{
+	int val, ret;
+	const struct mma_chip_info *chip = data->chip_info;
+
+	val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
+	if (val < 0)
+		return val;
+
+	if (state && !(mma8452_freefall_mode_enabled(data))) {
+		val |= BIT(idx_x + chip->ev_cfg_chan_shift);
+		val |= BIT(idx_y + chip->ev_cfg_chan_shift);
+		val |= BIT(idx_z + chip->ev_cfg_chan_shift);
+		val &= ~MMA8452_FF_MT_CFG_OAE;
+	} else if (!state && mma8452_freefall_mode_enabled(data)) {
+		val &= ~BIT(idx_x + chip->ev_cfg_chan_shift);
+		val &= ~BIT(idx_y + chip->ev_cfg_chan_shift);
+		val &= ~BIT(idx_z + chip->ev_cfg_chan_shift);
+		val |= MMA8452_FF_MT_CFG_OAE;
+	}
+
+	ret = mma8452_change_config(data, chip->ev_cfg, val);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
 static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
 					   int val, int val2)
 {
@@ -609,12 +649,22 @@ static int mma8452_read_event_config(struct iio_dev *indio_dev,
 	const struct mma_chip_info *chip = data->chip_info;
 	int ret;
 
-	ret = i2c_smbus_read_byte_data(data->client,
-				       data->chip_info->ev_cfg);
-	if (ret < 0)
-		return ret;
+	switch (dir) {
+	case IIO_EV_DIR_FALLING:
+		return mma8452_freefall_mode_enabled(data);
+	case IIO_EV_DIR_RISING:
+		if (mma8452_freefall_mode_enabled(data))
+			return 0;
+
+		ret = i2c_smbus_read_byte_data(data->client,
+					       data->chip_info->ev_cfg);
+		if (ret < 0)
+			return ret;
 
-	return !!(ret & BIT(chan->scan_index + chip->ev_cfg_chan_shift));
+		return !!(ret & BIT(chan->scan_index + chip->ev_cfg_chan_shift));
+	default:
+		return -EINVAL;
+	}
 }
 
 static int mma8452_write_event_config(struct iio_dev *indio_dev,
@@ -627,19 +677,38 @@ static int mma8452_write_event_config(struct iio_dev *indio_dev,
 	const struct mma_chip_info *chip = data->chip_info;
 	int val;
 
-	val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
-	if (val < 0)
-		return val;
-
-	if (state)
-		val |= BIT(chan->scan_index + chip->ev_cfg_chan_shift);
-	else
-		val &= ~BIT(chan->scan_index + chip->ev_cfg_chan_shift);
+	switch (dir) {
+	case IIO_EV_DIR_FALLING:
+		if ((!state && mma8452_freefall_mode_enabled) ||
+		    (state && !mma8452_freefall_mode_enabled))
+			return mma8452_set_freefall_mode(data, state);
+		else
+			return 0;
+	case IIO_EV_DIR_RISING:
+		val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
+		if (val < 0)
+			return val;
+
+		if (state) {
+			if (mma8452_freefall_mode_enabled(data)) {
+				val &= ~BIT(idx_x + chip->ev_cfg_chan_shift);
+				val &= ~BIT(idx_y + chip->ev_cfg_chan_shift);
+				val &= ~BIT(idx_z + chip->ev_cfg_chan_shift);
+				val |= MMA8452_FF_MT_CFG_OAE;
+			}
+			val |= BIT(chan->scan_index + chip->ev_cfg_chan_shift);
+		} else {
+			if (mma8452_freefall_mode_enabled(data))
+				return -EBUSY;
+			val &= ~BIT(chan->scan_index + chip->ev_cfg_chan_shift);
+		}
 
-	val |= chip->ev_cfg_ele;
-	val |= MMA8452_FF_MT_CFG_OAE;
+		val |= chip->ev_cfg_ele;
 
-	return mma8452_change_config(data, chip->ev_cfg, val);
+		return mma8452_change_config(data, chip->ev_cfg, val);
+	default:
+		return -EINVAL;
+	}
 }
 
 static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
@@ -652,6 +721,16 @@ static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
 	if (src < 0)
 		return;
 
+	if (mma8452_freefall_mode_enabled(data)) {
+		iio_push_event(indio_dev,
+			       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
+						  IIO_MOD_X_AND_Y_AND_Z,
+						  IIO_EV_TYPE_MAG,
+						  IIO_EV_DIR_FALLING),
+			       ts);
+		return;
+	}
+
 	if (src & data->chip_info->ev_src_xe)
 		iio_push_event(indio_dev,
 			       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
@@ -745,6 +824,27 @@ static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
 	return 0;
 }
 
+static const struct iio_event_spec mma8452_freefall_event[] = {
+	{
+		.type = IIO_EV_TYPE_MAG,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+		.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
+					BIT(IIO_EV_INFO_PERIOD) |
+					BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
+	},
+};
+
+static const struct iio_event_spec mma8652_freefall_event[] = {
+	{
+		.type = IIO_EV_TYPE_MAG,
+		.dir = IIO_EV_DIR_FALLING,
+		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
+		.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
+					BIT(IIO_EV_INFO_PERIOD)
+	},
+};
+
 static const struct iio_event_spec mma8452_transient_event[] = {
 	{
 		.type = IIO_EV_TYPE_MAG,
@@ -781,6 +881,24 @@ static struct attribute_group mma8452_event_attribute_group = {
 	.attrs = mma8452_event_attributes,
 };
 
+#define MMA8452_FREEFALL_CHANNEL(modifier) { \
+	.type = IIO_ACCEL, \
+	.modified = 1, \
+	.channel2 = modifier, \
+	.scan_index = -1, \
+	.event_spec = mma8452_freefall_event, \
+	.num_event_specs = ARRAY_SIZE(mma8452_freefall_event), \
+}
+
+#define MMA8652_FREEFALL_CHANNEL(modifier) { \
+	.type = IIO_ACCEL, \
+	.modified = 1, \
+	.channel2 = modifier, \
+	.scan_index = -1, \
+	.event_spec = mma8652_freefall_event, \
+	.num_event_specs = ARRAY_SIZE(mma8652_freefall_event), \
+}
+
 #define MMA8452_CHANNEL(axis, idx, bits) { \
 	.type = IIO_ACCEL, \
 	.modified = 1, \
@@ -827,6 +945,7 @@ static const struct iio_chan_spec mma8452_channels[] = {
 	MMA8452_CHANNEL(Y, idx_y, 12),
 	MMA8452_CHANNEL(Z, idx_z, 12),
 	IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
+	MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
 };
 
 static const struct iio_chan_spec mma8453_channels[] = {
@@ -834,6 +953,7 @@ static const struct iio_chan_spec mma8453_channels[] = {
 	MMA8452_CHANNEL(Y, idx_y, 10),
 	MMA8452_CHANNEL(Z, idx_z, 10),
 	IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
+	MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
 };
 
 static const struct iio_chan_spec mma8652_channels[] = {
@@ -841,6 +961,7 @@ static const struct iio_chan_spec mma8652_channels[] = {
 	MMA8652_CHANNEL(Y, idx_y, 12),
 	MMA8652_CHANNEL(Z, idx_z, 12),
 	IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
+	MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
 };
 
 static const struct iio_chan_spec mma8653_channels[] = {
@@ -848,6 +969,7 @@ static const struct iio_chan_spec mma8653_channels[] = {
 	MMA8652_CHANNEL(Y, idx_y, 10),
 	MMA8652_CHANNEL(Z, idx_z, 10),
 	IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
+	MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
 };
 
 enum {
@@ -1190,6 +1312,10 @@ static int mma8452_probe(struct i2c_client *client,
 	if (ret < 0)
 		goto buffer_cleanup;
 
+	ret = mma8452_set_freefall_mode(data, 0);
+	if (ret)
+		return ret;
+
 	return 0;
 
 buffer_cleanup:
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] iio: mma8452: add freefall detection for Freescale's accelerometers
  2016-01-13 11:24 [PATCH v3] iio: mma8452: add freefall detection for Freescale's accelerometers Martin Kepplinger
@ 2016-01-13 11:39 ` kbuild test robot
  2016-01-13 11:40 ` kbuild test robot
  1 sibling, 0 replies; 3+ messages in thread
From: kbuild test robot @ 2016-01-13 11:39 UTC (permalink / raw)
  To: Martin Kepplinger
  Cc: kbuild-all, jic23, knaack.h, lars, pmeerw, christoph.muellner,
	mfuzzey, linux-iio, linux-kernel, Martin Kepplinger,
	Martin Kepplinger

[-- Attachment #1: Type: text/plain, Size: 2103 bytes --]

Hi Martin,

[auto build test WARNING on next-20160113]
[cannot apply to v4.4-rc8 v4.4-rc7 v4.4-rc6 v4.4]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Martin-Kepplinger/iio-mma8452-add-freefall-detection-for-Freescale-s-accelerometers/20160113-192756
config: x86_64-randconfig-x017-01110856 (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   drivers/iio/accel/mma8452.c: In function 'mma8452_write_event_config':
>> drivers/iio/accel/mma8452.c:682:15: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
      if ((!state && mma8452_freefall_mode_enabled) ||
                  ^
   drivers/iio/accel/mma8452.c:683:17: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
          (state && !mma8452_freefall_mode_enabled))
                    ^

vim +682 drivers/iio/accel/mma8452.c

   666			return -EINVAL;
   667		}
   668	}
   669	
   670	static int mma8452_write_event_config(struct iio_dev *indio_dev,
   671					      const struct iio_chan_spec *chan,
   672					      enum iio_event_type type,
   673					      enum iio_event_direction dir,
   674					      int state)
   675	{
   676		struct mma8452_data *data = iio_priv(indio_dev);
   677		const struct mma_chip_info *chip = data->chip_info;
   678		int val;
   679	
   680		switch (dir) {
   681		case IIO_EV_DIR_FALLING:
 > 682			if ((!state && mma8452_freefall_mode_enabled) ||
   683			    (state && !mma8452_freefall_mode_enabled))
   684				return mma8452_set_freefall_mode(data, state);
   685			else
   686				return 0;
   687		case IIO_EV_DIR_RISING:
   688			val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
   689			if (val < 0)
   690				return val;

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 29153 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] iio: mma8452: add freefall detection for Freescale's accelerometers
  2016-01-13 11:24 [PATCH v3] iio: mma8452: add freefall detection for Freescale's accelerometers Martin Kepplinger
  2016-01-13 11:39 ` kbuild test robot
@ 2016-01-13 11:40 ` kbuild test robot
  1 sibling, 0 replies; 3+ messages in thread
From: kbuild test robot @ 2016-01-13 11:40 UTC (permalink / raw)
  To: Martin Kepplinger
  Cc: kbuild-all, jic23, knaack.h, lars, pmeerw, christoph.muellner,
	mfuzzey, linux-iio, linux-kernel, Martin Kepplinger,
	Martin Kepplinger

[-- Attachment #1: Type: text/plain, Size: 5147 bytes --]

Hi Martin,

[auto build test WARNING on next-20160113]
[cannot apply to v4.4-rc8 v4.4-rc7 v4.4-rc6 v4.4]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Martin-Kepplinger/iio-mma8452-add-freefall-detection-for-Freescale-s-accelerometers/20160113-192756
config: x86_64-randconfig-x015-01110856 (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   In file included from include/uapi/linux/stddef.h:1:0,
                    from include/linux/stddef.h:4,
                    from include/uapi/linux/posix_types.h:4,
                    from include/uapi/linux/types.h:13,
                    from include/linux/types.h:5,
                    from include/linux/list.h:4,
                    from include/linux/module.h:9,
                    from drivers/iio/accel/mma8452.c:21:
   drivers/iio/accel/mma8452.c: In function 'mma8452_write_event_config':
   drivers/iio/accel/mma8452.c:682:15: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
      if ((!state && mma8452_freefall_mode_enabled) ||
                  ^
   include/linux/compiler.h:147:28: note: in definition of macro '__trace_if'
     if (__builtin_constant_p((cond)) ? !!(cond) :   \
                               ^
>> drivers/iio/accel/mma8452.c:682:3: note: in expansion of macro 'if'
      if ((!state && mma8452_freefall_mode_enabled) ||
      ^
   drivers/iio/accel/mma8452.c:683:17: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
          (state && !mma8452_freefall_mode_enabled))
                    ^
   include/linux/compiler.h:147:28: note: in definition of macro '__trace_if'
     if (__builtin_constant_p((cond)) ? !!(cond) :   \
                               ^
>> drivers/iio/accel/mma8452.c:682:3: note: in expansion of macro 'if'
      if ((!state && mma8452_freefall_mode_enabled) ||
      ^
   drivers/iio/accel/mma8452.c:682:15: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
      if ((!state && mma8452_freefall_mode_enabled) ||
                  ^
   include/linux/compiler.h:147:40: note: in definition of macro '__trace_if'
     if (__builtin_constant_p((cond)) ? !!(cond) :   \
                                           ^
>> drivers/iio/accel/mma8452.c:682:3: note: in expansion of macro 'if'
      if ((!state && mma8452_freefall_mode_enabled) ||
      ^
   drivers/iio/accel/mma8452.c:683:17: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
          (state && !mma8452_freefall_mode_enabled))
                    ^
   include/linux/compiler.h:147:40: note: in definition of macro '__trace_if'
     if (__builtin_constant_p((cond)) ? !!(cond) :   \
                                           ^
>> drivers/iio/accel/mma8452.c:682:3: note: in expansion of macro 'if'
      if ((!state && mma8452_freefall_mode_enabled) ||
      ^
   drivers/iio/accel/mma8452.c:682:15: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
      if ((!state && mma8452_freefall_mode_enabled) ||
                  ^
   include/linux/compiler.h:158:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^
>> drivers/iio/accel/mma8452.c:682:3: note: in expansion of macro 'if'
      if ((!state && mma8452_freefall_mode_enabled) ||
      ^
   drivers/iio/accel/mma8452.c:683:17: warning: the address of 'mma8452_freefall_mode_enabled' will always evaluate as 'true' [-Waddress]
          (state && !mma8452_freefall_mode_enabled))
                    ^
   include/linux/compiler.h:158:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^
>> drivers/iio/accel/mma8452.c:682:3: note: in expansion of macro 'if'
      if ((!state && mma8452_freefall_mode_enabled) ||
      ^

vim +/if +682 drivers/iio/accel/mma8452.c

   666			return -EINVAL;
   667		}
   668	}
   669	
   670	static int mma8452_write_event_config(struct iio_dev *indio_dev,
   671					      const struct iio_chan_spec *chan,
   672					      enum iio_event_type type,
   673					      enum iio_event_direction dir,
   674					      int state)
   675	{
   676		struct mma8452_data *data = iio_priv(indio_dev);
   677		const struct mma_chip_info *chip = data->chip_info;
   678		int val;
   679	
   680		switch (dir) {
   681		case IIO_EV_DIR_FALLING:
 > 682			if ((!state && mma8452_freefall_mode_enabled) ||
   683			    (state && !mma8452_freefall_mode_enabled))
   684				return mma8452_set_freefall_mode(data, state);
   685			else
   686				return 0;
   687		case IIO_EV_DIR_RISING:
   688			val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
   689			if (val < 0)
   690				return val;

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 26161 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-01-13 11:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-13 11:24 [PATCH v3] iio: mma8452: add freefall detection for Freescale's accelerometers Martin Kepplinger
2016-01-13 11:39 ` kbuild test robot
2016-01-13 11:40 ` kbuild test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.