All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 1/5] IIO: core: Introduce read_raw_multi
@ 2014-02-23 19:50 Srinivas Pandruvada
  2014-02-23 19:50 ` [PATCH v1 2/5] IIO: core: Modify scan element type Srinivas Pandruvada
                   ` (4 more replies)
  0 siblings, 5 replies; 15+ messages in thread
From: Srinivas Pandruvada @ 2014-02-23 19:50 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, Srinivas Pandruvada

This callback is introduced to overcome some limitations of existing
read_raw callback. The functionality of both existing read_raw and
read_raw_multi is similar, both are used to request values from the
device. The current read_raw callback allows only two return values.
The new read_raw_multi allows returning multiple values. Instead of
passing just address of val and val2, it passes length and pointer
to values. Depending on the type and length of passed buffer, iio
client drivers can return multiple values.

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
---
 drivers/iio/iio_core.h           |  2 +-
 drivers/iio/industrialio-core.c  | 51 +++++++++++++++++++++++-----------------
 drivers/iio/industrialio-event.c |  6 +++--
 drivers/iio/inkern.c             | 15 ++++++++++--
 include/linux/iio/iio.h          | 16 +++++++++++++
 5 files changed, 63 insertions(+), 27 deletions(-)

diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index f6db6af..4172f22 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -35,7 +35,7 @@ int __iio_add_chan_devattr(const char *postfix,
 			   struct list_head *attr_list);
 void iio_free_chan_devattr_list(struct list_head *attr_list);
 
-ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2);
+ssize_t iio_format_value(char *buf, unsigned int type, int *val);
 
 /* Event interface flags */
 #define IIO_BUSY_BIT_POS 1
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index acc911a..b1225a4 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -373,41 +373,42 @@ EXPORT_SYMBOL_GPL(iio_enum_write);
  * @buf: The buffer to which the formated value gets written
  * @type: One of the IIO_VAL_... constants. This decides how the val and val2
  *        parameters are formatted.
- * @val: First part of the value, exact meaning depends on the type parameter.
- * @val2: Second part of the value, exact meaning depends on the type parameter.
+ * @vals: pointer to the values, exact meaning depends on the type parameter.
  */
-ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2)
+ssize_t iio_format_value(char *buf, unsigned int type, int *vals)
 {
 	unsigned long long tmp;
 	bool scale_db = false;
 
 	switch (type) {
 	case IIO_VAL_INT:
-		return sprintf(buf, "%d\n", val);
+		return sprintf(buf, "%d\n", vals[0]);
 	case IIO_VAL_INT_PLUS_MICRO_DB:
 		scale_db = true;
 	case IIO_VAL_INT_PLUS_MICRO:
-		if (val2 < 0)
-			return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
+		if (vals[1] < 0)
+			return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
+					-vals[1],
 				scale_db ? " dB" : "");
 		else
-			return sprintf(buf, "%d.%06u%s\n", val, val2,
+			return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
 				scale_db ? " dB" : "");
 	case IIO_VAL_INT_PLUS_NANO:
-		if (val2 < 0)
-			return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
+		if (vals[1] < 0)
+			return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
+					-vals[1]);
 		else
-			return sprintf(buf, "%d.%09u\n", val, val2);
+			return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
 	case IIO_VAL_FRACTIONAL:
-		tmp = div_s64((s64)val * 1000000000LL, val2);
-		val2 = do_div(tmp, 1000000000LL);
-		val = tmp;
-		return sprintf(buf, "%d.%09u\n", val, val2);
+		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
+		vals[1] = do_div(tmp, 1000000000LL);
+		vals[0] = tmp;
+		return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
 	case IIO_VAL_FRACTIONAL_LOG2:
-		tmp = (s64)val * 1000000000LL >> val2;
-		val2 = do_div(tmp, 1000000000LL);
-		val = tmp;
-		return sprintf(buf, "%d.%09u\n", val, val2);
+		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
+		vals[1] = do_div(tmp, 1000000000LL);
+		vals[0] = tmp;
+		return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
 	default:
 		return 0;
 	}
@@ -419,14 +420,20 @@ static ssize_t iio_read_channel_info(struct device *dev,
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-	int val, val2;
-	int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
-					    &val, &val2, this_attr->address);
+	int vals[INDIO_MAX_RAW_ELEMENTS];
+	int ret;
+
+	if (indio_dev->info->read_raw_multi)
+		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
+			    INDIO_MAX_RAW_ELEMENTS, vals, this_attr->address);
+	else
+		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
+				    &vals[0], &vals[1], this_attr->address);
 
 	if (ret < 0)
 		return ret;
 
-	return iio_format_value(buf, ret, val, val2);
+	return iio_format_value(buf, ret, vals);
 }
 
 /**
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index c9c1419..0f7f78e 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -272,7 +272,7 @@ static ssize_t iio_ev_value_show(struct device *dev,
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-	int val, val2;
+	int val, val2, val_arr[2];
 	int ret;
 
 	ret = indio_dev->info->read_event_value(indio_dev,
@@ -281,7 +281,9 @@ static ssize_t iio_ev_value_show(struct device *dev,
 		&val, &val2);
 	if (ret < 0)
 		return ret;
-	return iio_format_value(buf, ret, val, val2);
+	val_arr[0] = val;
+	val_arr[1] = val2;
+	return iio_format_value(buf, ret, val_arr);
 }
 
 static ssize_t iio_ev_value_store(struct device *dev,
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 0cf5f8e..6a93d57 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -417,12 +417,23 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
 	enum iio_chan_info_enum info)
 {
 	int unused;
+	int vals[INDIO_MAX_RAW_ELEMENTS];
+	int ret;
 
 	if (val2 == NULL)
 		val2 = &unused;
 
-	return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
-						val, val2, info);
+	if (chan->indio_dev->info->read_raw_multi) {
+		ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
+					chan->channel, INDIO_MAX_RAW_ELEMENTS,
+					vals, info);
+		*val = vals[0];
+		*val2 = vals[1];
+	} else
+		ret = chan->indio_dev->info->read_raw(chan->indio_dev,
+					chan->channel, val, val2, info);
+
+	return ret;
 }
 
 int iio_read_channel_raw(struct iio_channel *chan, int *val)
diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index 75a8a20..ddf9f60 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -284,6 +284,8 @@ static inline s64 iio_get_time_ns(void)
 #define INDIO_ALL_BUFFER_MODES					\
 	(INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
 
+#define INDIO_MAX_RAW_ELEMENTS		4
+
 struct iio_trigger; /* forward declaration */
 struct iio_dev;
 
@@ -298,6 +300,14 @@ struct iio_dev;
  *			the channel in question.  Return value will specify the
  *			type of value returned by the device. val and val2 will
  *			contain the elements making up the returned value.
+ * @read_raw_multi:	function to return values from the device.
+ *			mask specifies which value. Note 0 means a reading of
+ *			the channel in question.  Return value will specify the
+ *			type of value returned by the device. vals pointer
+ *			contain the elements making up the returned value.
+ *			val_len specifies maximum number of elements
+ *			vals pointer can contain. In contrast to to read_raw
+ *			device can return multiple values not just two.
  * @write_raw:		function to write a value to the device.
  *			Parameters are the same as for read_raw.
  * @write_raw_get_fmt:	callback function to query the expected
@@ -324,6 +334,12 @@ struct iio_info {
 			int *val2,
 			long mask);
 
+	int (*read_raw_multi)(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan,
+			int val_len,
+			int *vals,
+			long mask);
+
 	int (*write_raw)(struct iio_dev *indio_dev,
 			 struct iio_chan_spec const *chan,
 			 int val,
-- 
1.8.3.2


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

* [PATCH v1 2/5] IIO: core: Modify scan element type
  2014-02-23 19:50 [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Srinivas Pandruvada
@ 2014-02-23 19:50 ` Srinivas Pandruvada
  2014-02-23 19:50 ` [PATCH v1 3/5] IIO: core: Introduce Quaternion types Srinivas Pandruvada
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 15+ messages in thread
From: Srinivas Pandruvada @ 2014-02-23 19:50 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, Srinivas Pandruvada

The current scan element type uses the following format:
  [be|le]:[s|u]bits/storagebits[>>shift].
To specify multiple elements in this type, added a repeat value.
So new format is:
  [be|le]:([s|u]bits/storagebits){repeat}[>>shift].
Here repeat is specifying how may times, real/storage bits are repeating.

When repeat is value is 0 or 1, then repeat value is not used in the format,
and it will be same as existing format.

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
---
 drivers/iio/industrialio-buffer.c | 41 +++++++++++++++++++++++++++++++++------
 include/linux/iio/iio.h           |  9 +++++++++
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index c67d83b..598eacd 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -150,7 +150,16 @@ static ssize_t iio_show_fixed_type(struct device *dev,
 		type = IIO_BE;
 #endif
 	}
-	return sprintf(buf, "%s:%c%d/%d>>%u\n",
+	if (this_attr->c->scan_type.repeat > 1)
+		return sprintf(buf, "%s:(%c%d/%d){%d}>>%u\n",
+		       iio_endian_prefix[type],
+		       this_attr->c->scan_type.sign,
+		       this_attr->c->scan_type.realbits,
+		       this_attr->c->scan_type.storagebits,
+		       this_attr->c->scan_type.repeat,
+		       this_attr->c->scan_type.shift);
+		else
+			return sprintf(buf, "%s:%c%d/%d>>%u\n",
 		       iio_endian_prefix[type],
 		       this_attr->c->scan_type.sign,
 		       this_attr->c->scan_type.realbits,
@@ -475,14 +484,22 @@ static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
 	for_each_set_bit(i, mask,
 			 indio_dev->masklength) {
 		ch = iio_find_channel_from_si(indio_dev, i);
-		length = ch->scan_type.storagebits / 8;
+		if (ch->scan_type.repeat > 1)
+			length = ch->scan_type.storagebits / 8 *
+				ch->scan_type.repeat;
+		else
+			length = ch->scan_type.storagebits / 8;
 		bytes = ALIGN(bytes, length);
 		bytes += length;
 	}
 	if (timestamp) {
 		ch = iio_find_channel_from_si(indio_dev,
 					      indio_dev->scan_index_timestamp);
-		length = ch->scan_type.storagebits / 8;
+		if (ch->scan_type.repeat > 1)
+			length = ch->scan_type.storagebits / 8 *
+				ch->scan_type.repeat;
+		else
+			length = ch->scan_type.storagebits / 8;
 		bytes = ALIGN(bytes, length);
 		bytes += length;
 	}
@@ -961,7 +978,11 @@ static int iio_buffer_update_demux(struct iio_dev *indio_dev,
 					       indio_dev->masklength,
 					       in_ind + 1);
 			ch = iio_find_channel_from_si(indio_dev, in_ind);
-			length = ch->scan_type.storagebits/8;
+			if (ch->scan_type.repeat > 1)
+				length = ch->scan_type.storagebits / 8 *
+					ch->scan_type.repeat;
+			else
+				length = ch->scan_type.storagebits / 8;
 			/* Make sure we are aligned */
 			in_loc += length;
 			if (in_loc % length)
@@ -973,7 +994,11 @@ static int iio_buffer_update_demux(struct iio_dev *indio_dev,
 			goto error_clear_mux_table;
 		}
 		ch = iio_find_channel_from_si(indio_dev, in_ind);
-		length = ch->scan_type.storagebits/8;
+		if (ch->scan_type.repeat > 1)
+			length = ch->scan_type.storagebits / 8 *
+				ch->scan_type.repeat;
+		else
+			length = ch->scan_type.storagebits / 8;
 		if (out_loc % length)
 			out_loc += length - out_loc % length;
 		if (in_loc % length)
@@ -994,7 +1019,11 @@ static int iio_buffer_update_demux(struct iio_dev *indio_dev,
 		}
 		ch = iio_find_channel_from_si(indio_dev,
 			indio_dev->scan_index_timestamp);
-		length = ch->scan_type.storagebits/8;
+		if (ch->scan_type.repeat > 1)
+			length = ch->scan_type.storagebits / 8 *
+				ch->scan_type.repeat;
+		else
+			length = ch->scan_type.storagebits / 8;
 		if (out_loc % length)
 			out_loc += length - out_loc % length;
 		if (in_loc % length)
diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index ddf9f60..3f1a83c 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -177,6 +177,14 @@ struct iio_event_spec {
  *			shift:		Shift right by this before masking out
  *					realbits.
  *			endianness:	little or big endian
+ *			repeat:		No. of times real/storage bits repeats.
+ *					By default, when repeat is set to 0
+ *					repeat value is treated as 1. When
+ *					repeat value is 0 or 1, then there is
+ *					no change in existing scan element
+ *					type, in sysfs. When set to more than
+ *					1, then the type element in sysfs will
+ *					show repeat value.
  * @info_mask_separate: What information is to be exported that is specific to
  *			this channel.
  * @info_mask_shared_by_type: What information is to be exported that is shared
@@ -219,6 +227,7 @@ struct iio_chan_spec {
 		u8	realbits;
 		u8	storagebits;
 		u8	shift;
+		u8	repeat;
 		enum iio_endian endianness;
 	} scan_type;
 	long			info_mask_separate;
-- 
1.8.3.2


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

* [PATCH v1 3/5] IIO: core: Introduce Quaternion types
  2014-02-23 19:50 [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Srinivas Pandruvada
  2014-02-23 19:50 ` [PATCH v1 2/5] IIO: core: Modify scan element type Srinivas Pandruvada
@ 2014-02-23 19:50 ` Srinivas Pandruvada
  2014-02-23 19:50 ` [PATCH v1 4/5] iio: hid-sensors: Added device rotation support Srinivas Pandruvada
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 15+ messages in thread
From: Srinivas Pandruvada @ 2014-02-23 19:50 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, Srinivas Pandruvada

Introduced IIO_VAL_QUATERNION and its formatting. Format used here:
x:y:z:w
In addition IIO_QUAT_ROT is added to the channel type and name.

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
---
 drivers/iio/industrialio-core.c | 4 ++++
 include/linux/iio/types.h       | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index b1225a4..69aaa7a 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -70,6 +70,7 @@ static const char * const iio_chan_type_name_spec[] = {
 	[IIO_CCT] = "cct",
 	[IIO_PRESSURE] = "pressure",
 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
+	[IIO_QUAT_ROT] = "rotquaternion",
 };
 
 static const char * const iio_modifier_names[] = {
@@ -409,6 +410,9 @@ ssize_t iio_format_value(char *buf, unsigned int type, int *vals)
 		vals[1] = do_div(tmp, 1000000000LL);
 		vals[0] = tmp;
 		return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
+	case IIO_VAL_QUATERNION:
+		return sprintf(buf, "%d:%d:%d:%d\n", vals[0], vals[1], vals[2],
+							vals[3]);
 	default:
 		return 0;
 	}
diff --git a/include/linux/iio/types.h b/include/linux/iio/types.h
index 084d882..256ad2d 100644
--- a/include/linux/iio/types.h
+++ b/include/linux/iio/types.h
@@ -30,6 +30,7 @@ enum iio_chan_type {
 	IIO_CCT,
 	IIO_PRESSURE,
 	IIO_HUMIDITYRELATIVE,
+	IIO_QUAT_ROT,
 };
 
 enum iio_modifier {
@@ -79,6 +80,7 @@ enum iio_event_direction {
 #define IIO_VAL_INT_PLUS_MICRO 2
 #define IIO_VAL_INT_PLUS_NANO 3
 #define IIO_VAL_INT_PLUS_MICRO_DB 4
+#define IIO_VAL_QUATERNION 5
 #define IIO_VAL_FRACTIONAL 10
 #define IIO_VAL_FRACTIONAL_LOG2 11
 
-- 
1.8.3.2


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

* [PATCH v1 4/5] iio: hid-sensors: Added device rotation support
  2014-02-23 19:50 [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Srinivas Pandruvada
  2014-02-23 19:50 ` [PATCH v1 2/5] IIO: core: Modify scan element type Srinivas Pandruvada
  2014-02-23 19:50 ` [PATCH v1 3/5] IIO: core: Introduce Quaternion types Srinivas Pandruvada
@ 2014-02-23 19:50 ` Srinivas Pandruvada
  2014-02-23 19:50 ` [PATCH v1 5/5] iio: Added ABI description for quaternion Srinivas Pandruvada
  2014-03-29 11:15 ` [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Jonathan Cameron
  4 siblings, 0 replies; 15+ messages in thread
From: Srinivas Pandruvada @ 2014-02-23 19:50 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, Srinivas Pandruvada

Added usage id processing for device rotation. This uses IIO
interfaces for triggered buffer to present data to user
mode.This uses HID sensor framework for registering callback
events from the sensor hub.
Data is exported to user space in the form of quaternion rotation
format.

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
---
 drivers/iio/orientation/Kconfig               |  11 +
 drivers/iio/orientation/Makefile              |   1 +
 drivers/iio/orientation/hid-sensor-rotation.c | 358 ++++++++++++++++++++++++++
 include/linux/hid-sensor-ids.h                |   1 +
 4 files changed, 371 insertions(+)
 create mode 100644 drivers/iio/orientation/hid-sensor-rotation.c

diff --git a/drivers/iio/orientation/Kconfig b/drivers/iio/orientation/Kconfig
index 58c62c8..da8bd8a 100644
--- a/drivers/iio/orientation/Kconfig
+++ b/drivers/iio/orientation/Kconfig
@@ -16,4 +16,15 @@ config HID_SENSOR_INCLINOMETER_3D
 	  Say yes here to build support for the HID SENSOR
 	  Inclinometer 3D.
 
+config HID_SENSOR_DEVICE_ROTATION
+	depends on HID_SENSOR_HUB
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select HID_SENSOR_IIO_COMMON
+	select HID_SENSOR_IIO_TRIGGER
+	tristate "HID Device Rotation"
+	help
+	  Say yes here to build support for the HID SENSOR
+	  device rotation.
+
 endmenu
diff --git a/drivers/iio/orientation/Makefile b/drivers/iio/orientation/Makefile
index 2c97572..4734dab 100644
--- a/drivers/iio/orientation/Makefile
+++ b/drivers/iio/orientation/Makefile
@@ -4,3 +4,4 @@
 
 # When adding new entries keep the list in alphabetical order
 obj-$(CONFIG_HID_SENSOR_INCLINOMETER_3D) += hid-sensor-incl-3d.o
+obj-$(CONFIG_HID_SENSOR_DEVICE_ROTATION) += hid-sensor-rotation.o
diff --git a/drivers/iio/orientation/hid-sensor-rotation.c b/drivers/iio/orientation/hid-sensor-rotation.c
new file mode 100644
index 0000000..419fa40
--- /dev/null
+++ b/drivers/iio/orientation/hid-sensor-rotation.c
@@ -0,0 +1,358 @@
+/*
+ * HID Sensors Driver
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/slab.h>
+#include <linux/hid-sensor-hub.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include "../common/hid-sensors/hid-sensor-trigger.h"
+
+#define MAX_QUATERNION_COMP	4
+
+struct dev_rot_state {
+	struct hid_sensor_hub_callbacks callbacks;
+	struct hid_sensor_common common_attributes;
+	struct hid_sensor_hub_attribute_info quaternion;
+	u32 sampled_vals[MAX_QUATERNION_COMP];
+};
+
+/* Channel definitions */
+static const struct iio_chan_spec dev_rot_channels[] = {
+	{
+		.type = IIO_QUAT_ROT,
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
+					BIT(IIO_CHAN_INFO_SCALE) |
+					BIT(IIO_CHAN_INFO_SAMP_FREQ) |
+					BIT(IIO_CHAN_INFO_HYSTERESIS) |
+					BIT(IIO_CHAN_INFO_RAW),
+	}
+};
+
+/* Adjust channel real bits based on report descriptor */
+static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
+					    int size)
+{
+	chan->scan_type.sign = 's';
+	/* Real storage bits will change based on the report desc. */
+	chan->scan_type.realbits = size * 8;
+	/* Maximum size of a sample to capture is u32 */
+	chan->scan_type.storagebits = sizeof(u32) * 8;
+	chan->scan_type.repeat = MAX_QUATERNION_COMP;
+}
+
+/* Channel read_raw handler */
+static int dev_rot_read_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			    int size, int *vals,
+			      long mask)
+{
+	struct dev_rot_state *rot_state = iio_priv(indio_dev);
+	int ret_type;
+	int i;
+
+	vals[0] = 0;
+	vals[1] = 0;
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (size >= MAX_QUATERNION_COMP) {
+			for (i = 0; i < MAX_QUATERNION_COMP; ++i)
+				vals[i] = rot_state->sampled_vals[i];
+			ret_type = IIO_VAL_QUATERNION;
+		} else
+			ret_type = -EINVAL;
+		break;
+	case IIO_CHAN_INFO_SCALE:
+		vals[0] = rot_state->quaternion.units;
+		ret_type = IIO_VAL_INT;
+		break;
+	case IIO_CHAN_INFO_OFFSET:
+		vals[1] = hid_sensor_convert_exponent(
+			rot_state->quaternion.unit_expo);
+		ret_type = IIO_VAL_INT;
+		break;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		ret_type = hid_sensor_read_samp_freq_value(
+			&rot_state->common_attributes, &vals[0], &vals[1]);
+		break;
+	case IIO_CHAN_INFO_HYSTERESIS:
+		ret_type = hid_sensor_read_raw_hyst_value(
+			&rot_state->common_attributes, &vals[0], &vals[1]);
+		break;
+	default:
+		ret_type = -EINVAL;
+		break;
+	}
+
+	return ret_type;
+}
+
+/* Channel write_raw handler */
+static int dev_rot_write_raw(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan,
+			       int val,
+			       int val2,
+			       long mask)
+{
+	struct dev_rot_state *rot_state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		ret = hid_sensor_write_samp_freq_value(
+				&rot_state->common_attributes, val, val2);
+		break;
+	case IIO_CHAN_INFO_HYSTERESIS:
+		ret = hid_sensor_write_raw_hyst_value(
+				&rot_state->common_attributes, val, val2);
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static const struct iio_info dev_rot_info = {
+	.driver_module = THIS_MODULE,
+	.read_raw_multi = &dev_rot_read_raw,
+	.write_raw = &dev_rot_write_raw,
+};
+
+/* Function to push data to buffer */
+static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
+{
+	dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n");
+	iio_push_to_buffers(indio_dev, (u8 *)data);
+	dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n");
+
+}
+
+/* Callback handler to send event after all samples are received and captured */
+static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
+				unsigned usage_id,
+				void *priv)
+{
+	struct iio_dev *indio_dev = platform_get_drvdata(priv);
+	struct dev_rot_state *rot_state = iio_priv(indio_dev);
+
+	dev_dbg(&indio_dev->dev, "dev_rot_proc_event [%d]\n",
+				rot_state->common_attributes.data_ready);
+
+	if (rot_state->common_attributes.data_ready)
+		hid_sensor_push_data(indio_dev,
+				(u8 *)rot_state->sampled_vals,
+				sizeof(rot_state->sampled_vals));
+
+	return 0;
+}
+
+/* Capture samples in local storage */
+static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
+				unsigned usage_id,
+				size_t raw_len, char *raw_data,
+				void *priv)
+{
+	struct iio_dev *indio_dev = platform_get_drvdata(priv);
+	struct dev_rot_state *rot_state = iio_priv(indio_dev);
+
+	if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
+		memcpy(rot_state->sampled_vals, raw_data,
+					sizeof(rot_state->sampled_vals));
+		dev_dbg(&indio_dev->dev, "Recd Quat len:%lu::%lu\n", raw_len,
+					sizeof(rot_state->sampled_vals));
+	}
+
+	return 0;
+}
+
+/* Parse report which is specific to an usage id*/
+static int dev_rot_parse_report(struct platform_device *pdev,
+				struct hid_sensor_hub_device *hsdev,
+				struct iio_chan_spec *channels,
+				unsigned usage_id,
+				struct dev_rot_state *st)
+{
+	int ret;
+
+	ret = sensor_hub_input_get_attribute_info(hsdev,
+				HID_INPUT_REPORT,
+				usage_id,
+				HID_USAGE_SENSOR_ORIENT_QUATERNION,
+				&st->quaternion);
+	if (ret)
+		return ret;
+	dev_rot_adjust_channel_bit_mask(&channels[0],
+		st->quaternion.size / MAX_QUATERNION_COMP);
+
+	dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
+		st->quaternion.report_id);
+
+	dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
+				st->quaternion.size);
+
+	/* Set Sensitivity field ids, when there is no individual modifier */
+	if (st->common_attributes.sensitivity.index < 0) {
+		sensor_hub_input_get_attribute_info(hsdev,
+			HID_FEATURE_REPORT, usage_id,
+			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
+			HID_USAGE_SENSOR_DATA_ORIENTATION,
+			&st->common_attributes.sensitivity);
+		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
+			st->common_attributes.sensitivity.index,
+			st->common_attributes.sensitivity.report_id);
+	}
+	return 0;
+}
+
+/* Function to initialize the processing for usage id */
+static int hid_dev_rot_probe(struct platform_device *pdev)
+{
+	int ret;
+	static char *name = "dev_rotation";
+	struct iio_dev *indio_dev;
+	struct dev_rot_state *rot_state;
+	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
+	struct iio_chan_spec *channels;
+
+	indio_dev = devm_iio_device_alloc(&pdev->dev,
+					  sizeof(struct dev_rot_state));
+	if (indio_dev == NULL)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, indio_dev);
+
+	rot_state = iio_priv(indio_dev);
+	rot_state->common_attributes.hsdev = hsdev;
+	rot_state->common_attributes.pdev = pdev;
+
+	ret = hid_sensor_parse_common_attributes(hsdev,
+				HID_USAGE_SENSOR_DEVICE_ORIENTATION,
+				&rot_state->common_attributes);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to setup common attributes\n");
+		return ret;
+	}
+
+	channels = kmemdup(dev_rot_channels, sizeof(dev_rot_channels),
+			   GFP_KERNEL);
+	if (!channels) {
+		dev_err(&pdev->dev, "failed to duplicate channels\n");
+		return -ENOMEM;
+	}
+
+	ret = dev_rot_parse_report(pdev, hsdev, channels,
+			HID_USAGE_SENSOR_DEVICE_ORIENTATION, rot_state);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to setup attributes\n");
+		goto error_free_dev_mem;
+	}
+
+	indio_dev->channels = channels;
+	indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
+	indio_dev->dev.parent = &pdev->dev;
+	indio_dev->info = &dev_rot_info;
+	indio_dev->name = name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
+		NULL, NULL);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
+		goto error_free_dev_mem;
+	}
+	rot_state->common_attributes.data_ready = false;
+	ret = hid_sensor_setup_trigger(indio_dev, name,
+					&rot_state->common_attributes);
+	if (ret) {
+		dev_err(&pdev->dev, "trigger setup failed\n");
+		goto error_unreg_buffer_funcs;
+	}
+
+	ret = iio_device_register(indio_dev);
+	if (ret) {
+		dev_err(&pdev->dev, "device register failed\n");
+		goto error_remove_trigger;
+	}
+
+	rot_state->callbacks.send_event = dev_rot_proc_event;
+	rot_state->callbacks.capture_sample = dev_rot_capture_sample;
+	rot_state->callbacks.pdev = pdev;
+	ret = sensor_hub_register_callback(hsdev,
+					HID_USAGE_SENSOR_DEVICE_ORIENTATION,
+					&rot_state->callbacks);
+	if (ret) {
+		dev_err(&pdev->dev, "callback reg failed\n");
+		goto error_iio_unreg;
+	}
+
+	return 0;
+
+error_iio_unreg:
+	iio_device_unregister(indio_dev);
+error_remove_trigger:
+	hid_sensor_remove_trigger(&rot_state->common_attributes);
+error_unreg_buffer_funcs:
+	iio_triggered_buffer_cleanup(indio_dev);
+error_free_dev_mem:
+	kfree(indio_dev->channels);
+	return ret;
+}
+
+/* Function to deinitialize the processing for usage id */
+static int hid_dev_rot_remove(struct platform_device *pdev)
+{
+	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
+	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+	struct dev_rot_state *rot_state = iio_priv(indio_dev);
+
+	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_DEVICE_ORIENTATION);
+	iio_device_unregister(indio_dev);
+	hid_sensor_remove_trigger(&rot_state->common_attributes);
+	iio_triggered_buffer_cleanup(indio_dev);
+	kfree(indio_dev->channels);
+
+	return 0;
+}
+
+static struct platform_device_id hid_dev_rot_ids[] = {
+	{
+		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
+		.name = "HID-SENSOR-20008a",
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
+
+static struct platform_driver hid_dev_rot_platform_driver = {
+	.id_table = hid_dev_rot_ids,
+	.driver = {
+		.name	= KBUILD_MODNAME,
+		.owner	= THIS_MODULE,
+	},
+	.probe		= hid_dev_rot_probe,
+	.remove		= hid_dev_rot_remove,
+};
+module_platform_driver(hid_dev_rot_platform_driver);
+
+MODULE_DESCRIPTION("HID Sensor Device Rotation");
+MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/hid-sensor-ids.h b/include/linux/hid-sensor-ids.h
index beaf965..3fc44a0 100644
--- a/include/linux/hid-sensor-ids.h
+++ b/include/linux/hid-sensor-ids.h
@@ -66,6 +66,7 @@
 #define HID_USAGE_SENSOR_ORIENT_TILT_Y				0x200480
 #define HID_USAGE_SENSOR_ORIENT_TILT_Z				0x200481
 
+#define HID_USAGE_SENSOR_DEVICE_ORIENTATION			0x20008A
 #define HID_USAGE_SENSOR_ORIENT_ROTATION_MATRIX			0x200482
 #define HID_USAGE_SENSOR_ORIENT_QUATERNION			0x200483
 #define HID_USAGE_SENSOR_ORIENT_MAGN_FLUX			0x200484
-- 
1.8.3.2


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

* [PATCH v1 5/5] iio: Added ABI description for quaternion
  2014-02-23 19:50 [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Srinivas Pandruvada
                   ` (2 preceding siblings ...)
  2014-02-23 19:50 ` [PATCH v1 4/5] iio: hid-sensors: Added device rotation support Srinivas Pandruvada
@ 2014-02-23 19:50 ` Srinivas Pandruvada
  2014-03-29 11:15 ` [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Jonathan Cameron
  4 siblings, 0 replies; 15+ messages in thread
From: Srinivas Pandruvada @ 2014-02-23 19:50 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, Srinivas Pandruvada

Added documentation for reading quaternion components for 3D rotations.

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
---
 Documentation/ABI/testing/sysfs-bus-iio | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
index 6e02c50..8daa91a 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -891,3 +891,12 @@ Contact:	linux-iio@vger.kernel.org
 Description:
 		This attribute is used to get/set the integration time in
 		seconds.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_rotquaternion_raw
+KernelVersion:	3.15
+Contact:	linux-iio@vger.kernel.org
+Description:
+		Raw value of quaternion components using a format
+		x:y:z:w. Here x, y, and z component represents the axis about
+		which a rotation will occur and w component represents the
+		amount of rotation.
-- 
1.8.3.2


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

* Re: [PATCH v1 1/5] IIO: core: Introduce read_raw_multi
  2014-02-23 19:50 [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Srinivas Pandruvada
                   ` (3 preceding siblings ...)
  2014-02-23 19:50 ` [PATCH v1 5/5] iio: Added ABI description for quaternion Srinivas Pandruvada
@ 2014-03-29 11:15 ` Jonathan Cameron
  2014-04-04 21:38   ` Srinivas Pandruvada
  4 siblings, 1 reply; 15+ messages in thread
From: Jonathan Cameron @ 2014-03-29 11:15 UTC (permalink / raw)
  To: Srinivas Pandruvada; +Cc: linux-iio

On 23/02/14 19:50, Srinivas Pandruvada wrote:
> This callback is introduced to overcome some limitations of existing
> read_raw callback. The functionality of both existing read_raw and
> read_raw_multi is similar, both are used to request values from the
> device. The current read_raw callback allows only two return values.
> The new read_raw_multi allows returning multiple values. Instead of
> passing just address of val and val2, it passes length and pointer
> to values. Depending on the type and length of passed buffer, iio
> client drivers can return multiple values.
>
> Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Hi Srinivas.

Whilst it wasn't the most in depth of discussions, I think the outcome
of the 'how to handle quaternion sysfs naming' element of the
thread.
[RFC] IIO: New interfaces for 'interesting' channel types.

was that we should do them as a modifier instead of a new channel type.
e.g.

in_rot_quaternion_raw
etc.

Could you convert this driver over and repost?

If you disagree, then feel free to contribute to that discussion and
we'll continue from there.

Sorry this took so long to sort out, I was hoping for a few more inputs
but there we are...

Jonathan
> ---
>   drivers/iio/iio_core.h           |  2 +-
>   drivers/iio/industrialio-core.c  | 51 +++++++++++++++++++++++-----------------
>   drivers/iio/industrialio-event.c |  6 +++--
>   drivers/iio/inkern.c             | 15 ++++++++++--
>   include/linux/iio/iio.h          | 16 +++++++++++++
>   5 files changed, 63 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
> index f6db6af..4172f22 100644
> --- a/drivers/iio/iio_core.h
> +++ b/drivers/iio/iio_core.h
> @@ -35,7 +35,7 @@ int __iio_add_chan_devattr(const char *postfix,
>   			   struct list_head *attr_list);
>   void iio_free_chan_devattr_list(struct list_head *attr_list);
>
> -ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2);
> +ssize_t iio_format_value(char *buf, unsigned int type, int *val);
>
>   /* Event interface flags */
>   #define IIO_BUSY_BIT_POS 1
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index acc911a..b1225a4 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -373,41 +373,42 @@ EXPORT_SYMBOL_GPL(iio_enum_write);
>    * @buf: The buffer to which the formated value gets written
>    * @type: One of the IIO_VAL_... constants. This decides how the val and val2
>    *        parameters are formatted.
> - * @val: First part of the value, exact meaning depends on the type parameter.
> - * @val2: Second part of the value, exact meaning depends on the type parameter.
> + * @vals: pointer to the values, exact meaning depends on the type parameter.
>    */
> -ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2)
> +ssize_t iio_format_value(char *buf, unsigned int type, int *vals)
>   {
>   	unsigned long long tmp;
>   	bool scale_db = false;
>
>   	switch (type) {
>   	case IIO_VAL_INT:
> -		return sprintf(buf, "%d\n", val);
> +		return sprintf(buf, "%d\n", vals[0]);
>   	case IIO_VAL_INT_PLUS_MICRO_DB:
>   		scale_db = true;
>   	case IIO_VAL_INT_PLUS_MICRO:
> -		if (val2 < 0)
> -			return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
> +		if (vals[1] < 0)
> +			return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
> +					-vals[1],
>   				scale_db ? " dB" : "");
>   		else
> -			return sprintf(buf, "%d.%06u%s\n", val, val2,
> +			return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
>   				scale_db ? " dB" : "");
>   	case IIO_VAL_INT_PLUS_NANO:
> -		if (val2 < 0)
> -			return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
> +		if (vals[1] < 0)
> +			return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
> +					-vals[1]);
>   		else
> -			return sprintf(buf, "%d.%09u\n", val, val2);
> +			return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>   	case IIO_VAL_FRACTIONAL:
> -		tmp = div_s64((s64)val * 1000000000LL, val2);
> -		val2 = do_div(tmp, 1000000000LL);
> -		val = tmp;
> -		return sprintf(buf, "%d.%09u\n", val, val2);
> +		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
> +		vals[1] = do_div(tmp, 1000000000LL);
> +		vals[0] = tmp;
> +		return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>   	case IIO_VAL_FRACTIONAL_LOG2:
> -		tmp = (s64)val * 1000000000LL >> val2;
> -		val2 = do_div(tmp, 1000000000LL);
> -		val = tmp;
> -		return sprintf(buf, "%d.%09u\n", val, val2);
> +		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
> +		vals[1] = do_div(tmp, 1000000000LL);
> +		vals[0] = tmp;
> +		return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>   	default:
>   		return 0;
>   	}
> @@ -419,14 +420,20 @@ static ssize_t iio_read_channel_info(struct device *dev,
>   {
>   	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>   	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
> -	int val, val2;
> -	int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
> -					    &val, &val2, this_attr->address);
> +	int vals[INDIO_MAX_RAW_ELEMENTS];
> +	int ret;
> +
> +	if (indio_dev->info->read_raw_multi)
> +		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
> +			    INDIO_MAX_RAW_ELEMENTS, vals, this_attr->address);
> +	else
> +		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
> +				    &vals[0], &vals[1], this_attr->address);
>
>   	if (ret < 0)
>   		return ret;
>
> -	return iio_format_value(buf, ret, val, val2);
> +	return iio_format_value(buf, ret, vals);
>   }
>
>   /**
> diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
> index c9c1419..0f7f78e 100644
> --- a/drivers/iio/industrialio-event.c
> +++ b/drivers/iio/industrialio-event.c
> @@ -272,7 +272,7 @@ static ssize_t iio_ev_value_show(struct device *dev,
>   {
>   	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>   	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
> -	int val, val2;
> +	int val, val2, val_arr[2];
>   	int ret;
>
>   	ret = indio_dev->info->read_event_value(indio_dev,
> @@ -281,7 +281,9 @@ static ssize_t iio_ev_value_show(struct device *dev,
>   		&val, &val2);
>   	if (ret < 0)
>   		return ret;
> -	return iio_format_value(buf, ret, val, val2);
> +	val_arr[0] = val;
> +	val_arr[1] = val2;
> +	return iio_format_value(buf, ret, val_arr);
>   }
>
>   static ssize_t iio_ev_value_store(struct device *dev,
> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> index 0cf5f8e..6a93d57 100644
> --- a/drivers/iio/inkern.c
> +++ b/drivers/iio/inkern.c
> @@ -417,12 +417,23 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
>   	enum iio_chan_info_enum info)
>   {
>   	int unused;
> +	int vals[INDIO_MAX_RAW_ELEMENTS];
> +	int ret;
>
>   	if (val2 == NULL)
>   		val2 = &unused;
>
> -	return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
> -						val, val2, info);
> +	if (chan->indio_dev->info->read_raw_multi) {
> +		ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
> +					chan->channel, INDIO_MAX_RAW_ELEMENTS,
> +					vals, info);
> +		*val = vals[0];
> +		*val2 = vals[1];
> +	} else
> +		ret = chan->indio_dev->info->read_raw(chan->indio_dev,
> +					chan->channel, val, val2, info);
> +
> +	return ret;
>   }
>
>   int iio_read_channel_raw(struct iio_channel *chan, int *val)
> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
> index 75a8a20..ddf9f60 100644
> --- a/include/linux/iio/iio.h
> +++ b/include/linux/iio/iio.h
> @@ -284,6 +284,8 @@ static inline s64 iio_get_time_ns(void)
>   #define INDIO_ALL_BUFFER_MODES					\
>   	(INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
>
> +#define INDIO_MAX_RAW_ELEMENTS		4
> +
>   struct iio_trigger; /* forward declaration */
>   struct iio_dev;
>
> @@ -298,6 +300,14 @@ struct iio_dev;
>    *			the channel in question.  Return value will specify the
>    *			type of value returned by the device. val and val2 will
>    *			contain the elements making up the returned value.
> + * @read_raw_multi:	function to return values from the device.
> + *			mask specifies which value. Note 0 means a reading of
> + *			the channel in question.  Return value will specify the
> + *			type of value returned by the device. vals pointer
> + *			contain the elements making up the returned value.
> + *			val_len specifies maximum number of elements
> + *			vals pointer can contain. In contrast to to read_raw
> + *			device can return multiple values not just two.
>    * @write_raw:		function to write a value to the device.
>    *			Parameters are the same as for read_raw.
>    * @write_raw_get_fmt:	callback function to query the expected
> @@ -324,6 +334,12 @@ struct iio_info {
>   			int *val2,
>   			long mask);
>
> +	int (*read_raw_multi)(struct iio_dev *indio_dev,
> +			struct iio_chan_spec const *chan,
> +			int val_len,
> +			int *vals,
> +			long mask);
> +
>   	int (*write_raw)(struct iio_dev *indio_dev,
>   			 struct iio_chan_spec const *chan,
>   			 int val,
>


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

* Re: [PATCH v1 1/5] IIO: core: Introduce read_raw_multi
  2014-03-29 11:15 ` [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Jonathan Cameron
@ 2014-04-04 21:38   ` Srinivas Pandruvada
  2014-04-05 10:07     ` Jonathan Cameron
  0 siblings, 1 reply; 15+ messages in thread
From: Srinivas Pandruvada @ 2014-04-04 21:38 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: linux-iio

On 03/29/2014 04:15 AM, Jonathan Cameron wrote:
> On 23/02/14 19:50, Srinivas Pandruvada wrote:
>> This callback is introduced to overcome some limitations of existing
>> read_raw callback. The functionality of both existing read_raw and
>> read_raw_multi is similar, both are used to request values from the
>> device. The current read_raw callback allows only two return values.
>> The new read_raw_multi allows returning multiple values. Instead of
>> passing just address of val and val2, it passes length and pointer
>> to values. Depending on the type and length of passed buffer, iio
>> client drivers can return multiple values.
>>
>> Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
> Hi Srinivas.
>
> Whilst it wasn't the most in depth of discussions, I think the outcome
> of the 'how to handle quaternion sysfs naming' element of the
> thread.
> [RFC] IIO: New interfaces for 'interesting' channel types.
>
> was that we should do them as a modifier instead of a new channel type.
> e.g.
>
> in_rot_quaternion_raw
> etc.
>
> Could you convert this driver over and repost?
>
> If you disagree, then feel free to contribute to that discussion and
> we'll continue from there.
>
> Sorry this took so long to sort out, I was hoping for a few more inputs
> but there we are...
>
Hi Jonathan,

I have resubmitted with the modifier for quaternion dropping this patch.
I only allow buffered mode as without this patch representing in raw mode
all four component is not possible. Current applications only is using 
buffer mode.
So I can wait for core support something similar to this patch.

Thanks,
Srinivas




> Jonathan
>> ---
>>   drivers/iio/iio_core.h           |  2 +-
>>   drivers/iio/industrialio-core.c  | 51 
>> +++++++++++++++++++++++-----------------
>>   drivers/iio/industrialio-event.c |  6 +++--
>>   drivers/iio/inkern.c             | 15 ++++++++++--
>>   include/linux/iio/iio.h          | 16 +++++++++++++
>>   5 files changed, 63 insertions(+), 27 deletions(-)
>>
>> diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
>> index f6db6af..4172f22 100644
>> --- a/drivers/iio/iio_core.h
>> +++ b/drivers/iio/iio_core.h
>> @@ -35,7 +35,7 @@ int __iio_add_chan_devattr(const char *postfix,
>>                  struct list_head *attr_list);
>>   void iio_free_chan_devattr_list(struct list_head *attr_list);
>>
>> -ssize_t iio_format_value(char *buf, unsigned int type, int val, int 
>> val2);
>> +ssize_t iio_format_value(char *buf, unsigned int type, int *val);
>>
>>   /* Event interface flags */
>>   #define IIO_BUSY_BIT_POS 1
>> diff --git a/drivers/iio/industrialio-core.c 
>> b/drivers/iio/industrialio-core.c
>> index acc911a..b1225a4 100644
>> --- a/drivers/iio/industrialio-core.c
>> +++ b/drivers/iio/industrialio-core.c
>> @@ -373,41 +373,42 @@ EXPORT_SYMBOL_GPL(iio_enum_write);
>>    * @buf: The buffer to which the formated value gets written
>>    * @type: One of the IIO_VAL_... constants. This decides how the 
>> val and val2
>>    *        parameters are formatted.
>> - * @val: First part of the value, exact meaning depends on the type 
>> parameter.
>> - * @val2: Second part of the value, exact meaning depends on the 
>> type parameter.
>> + * @vals: pointer to the values, exact meaning depends on the type 
>> parameter.
>>    */
>> -ssize_t iio_format_value(char *buf, unsigned int type, int val, int 
>> val2)
>> +ssize_t iio_format_value(char *buf, unsigned int type, int *vals)
>>   {
>>       unsigned long long tmp;
>>       bool scale_db = false;
>>
>>       switch (type) {
>>       case IIO_VAL_INT:
>> -        return sprintf(buf, "%d\n", val);
>> +        return sprintf(buf, "%d\n", vals[0]);
>>       case IIO_VAL_INT_PLUS_MICRO_DB:
>>           scale_db = true;
>>       case IIO_VAL_INT_PLUS_MICRO:
>> -        if (val2 < 0)
>> -            return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
>> +        if (vals[1] < 0)
>> +            return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
>> +                    -vals[1],
>>                   scale_db ? " dB" : "");
>>           else
>> -            return sprintf(buf, "%d.%06u%s\n", val, val2,
>> +            return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
>>                   scale_db ? " dB" : "");
>>       case IIO_VAL_INT_PLUS_NANO:
>> -        if (val2 < 0)
>> -            return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
>> +        if (vals[1] < 0)
>> +            return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
>> +                    -vals[1]);
>>           else
>> -            return sprintf(buf, "%d.%09u\n", val, val2);
>> +            return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>       case IIO_VAL_FRACTIONAL:
>> -        tmp = div_s64((s64)val * 1000000000LL, val2);
>> -        val2 = do_div(tmp, 1000000000LL);
>> -        val = tmp;
>> -        return sprintf(buf, "%d.%09u\n", val, val2);
>> +        tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
>> +        vals[1] = do_div(tmp, 1000000000LL);
>> +        vals[0] = tmp;
>> +        return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>       case IIO_VAL_FRACTIONAL_LOG2:
>> -        tmp = (s64)val * 1000000000LL >> val2;
>> -        val2 = do_div(tmp, 1000000000LL);
>> -        val = tmp;
>> -        return sprintf(buf, "%d.%09u\n", val, val2);
>> +        tmp = (s64)vals[0] * 1000000000LL >> vals[1];
>> +        vals[1] = do_div(tmp, 1000000000LL);
>> +        vals[0] = tmp;
>> +        return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>       default:
>>           return 0;
>>       }
>> @@ -419,14 +420,20 @@ static ssize_t iio_read_channel_info(struct 
>> device *dev,
>>   {
>>       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>>       struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
>> -    int val, val2;
>> -    int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
>> -                        &val, &val2, this_attr->address);
>> +    int vals[INDIO_MAX_RAW_ELEMENTS];
>> +    int ret;
>> +
>> +    if (indio_dev->info->read_raw_multi)
>> +        ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
>> +                INDIO_MAX_RAW_ELEMENTS, vals, this_attr->address);
>> +    else
>> +        ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
>> +                    &vals[0], &vals[1], this_attr->address);
>>
>>       if (ret < 0)
>>           return ret;
>>
>> -    return iio_format_value(buf, ret, val, val2);
>> +    return iio_format_value(buf, ret, vals);
>>   }
>>
>>   /**
>> diff --git a/drivers/iio/industrialio-event.c 
>> b/drivers/iio/industrialio-event.c
>> index c9c1419..0f7f78e 100644
>> --- a/drivers/iio/industrialio-event.c
>> +++ b/drivers/iio/industrialio-event.c
>> @@ -272,7 +272,7 @@ static ssize_t iio_ev_value_show(struct device *dev,
>>   {
>>       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>>       struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
>> -    int val, val2;
>> +    int val, val2, val_arr[2];
>>       int ret;
>>
>>       ret = indio_dev->info->read_event_value(indio_dev,
>> @@ -281,7 +281,9 @@ static ssize_t iio_ev_value_show(struct device *dev,
>>           &val, &val2);
>>       if (ret < 0)
>>           return ret;
>> -    return iio_format_value(buf, ret, val, val2);
>> +    val_arr[0] = val;
>> +    val_arr[1] = val2;
>> +    return iio_format_value(buf, ret, val_arr);
>>   }
>>
>>   static ssize_t iio_ev_value_store(struct device *dev,
>> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
>> index 0cf5f8e..6a93d57 100644
>> --- a/drivers/iio/inkern.c
>> +++ b/drivers/iio/inkern.c
>> @@ -417,12 +417,23 @@ static int iio_channel_read(struct iio_channel 
>> *chan, int *val, int *val2,
>>       enum iio_chan_info_enum info)
>>   {
>>       int unused;
>> +    int vals[INDIO_MAX_RAW_ELEMENTS];
>> +    int ret;
>>
>>       if (val2 == NULL)
>>           val2 = &unused;
>>
>> -    return chan->indio_dev->info->read_raw(chan->indio_dev, 
>> chan->channel,
>> -                        val, val2, info);
>> +    if (chan->indio_dev->info->read_raw_multi) {
>> +        ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
>> +                    chan->channel, INDIO_MAX_RAW_ELEMENTS,
>> +                    vals, info);
>> +        *val = vals[0];
>> +        *val2 = vals[1];
>> +    } else
>> +        ret = chan->indio_dev->info->read_raw(chan->indio_dev,
>> +                    chan->channel, val, val2, info);
>> +
>> +    return ret;
>>   }
>>
>>   int iio_read_channel_raw(struct iio_channel *chan, int *val)
>> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
>> index 75a8a20..ddf9f60 100644
>> --- a/include/linux/iio/iio.h
>> +++ b/include/linux/iio/iio.h
>> @@ -284,6 +284,8 @@ static inline s64 iio_get_time_ns(void)
>>   #define INDIO_ALL_BUFFER_MODES                    \
>>       (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
>>
>> +#define INDIO_MAX_RAW_ELEMENTS        4
>> +
>>   struct iio_trigger; /* forward declaration */
>>   struct iio_dev;
>>
>> @@ -298,6 +300,14 @@ struct iio_dev;
>>    *            the channel in question.  Return value will specify the
>>    *            type of value returned by the device. val and val2 will
>>    *            contain the elements making up the returned value.
>> + * @read_raw_multi:    function to return values from the device.
>> + *            mask specifies which value. Note 0 means a reading of
>> + *            the channel in question.  Return value will specify the
>> + *            type of value returned by the device. vals pointer
>> + *            contain the elements making up the returned value.
>> + *            val_len specifies maximum number of elements
>> + *            vals pointer can contain. In contrast to to read_raw
>> + *            device can return multiple values not just two.
>>    * @write_raw:        function to write a value to the device.
>>    *            Parameters are the same as for read_raw.
>>    * @write_raw_get_fmt:    callback function to query the expected
>> @@ -324,6 +334,12 @@ struct iio_info {
>>               int *val2,
>>               long mask);
>>
>> +    int (*read_raw_multi)(struct iio_dev *indio_dev,
>> +            struct iio_chan_spec const *chan,
>> +            int val_len,
>> +            int *vals,
>> +            long mask);
>> +
>>       int (*write_raw)(struct iio_dev *indio_dev,
>>                struct iio_chan_spec const *chan,
>>                int val,
>>
>
>


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

* Re: [PATCH v1 1/5] IIO: core: Introduce read_raw_multi
  2014-04-04 21:38   ` Srinivas Pandruvada
@ 2014-04-05 10:07     ` Jonathan Cameron
  2014-04-07 16:05       ` [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver gmane
  0 siblings, 1 reply; 15+ messages in thread
From: Jonathan Cameron @ 2014-04-05 10:07 UTC (permalink / raw)
  To: Srinivas Pandruvada; +Cc: linux-iio

On 04/04/14 22:38, Srinivas Pandruvada wrote:
> On 03/29/2014 04:15 AM, Jonathan Cameron wrote:
>> On 23/02/14 19:50, Srinivas Pandruvada wrote:
>>> This callback is introduced to overcome some limitations of existing
>>> read_raw callback. The functionality of both existing read_raw and
>>> read_raw_multi is similar, both are used to request values from the
>>> device. The current read_raw callback allows only two return values.
>>> The new read_raw_multi allows returning multiple values. Instead of
>>> passing just address of val and val2, it passes length and pointer
>>> to values. Depending on the type and length of passed buffer, iio
>>> client drivers can return multiple values.
>>>
>>> Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
>> Hi Srinivas.
>>
>> Whilst it wasn't the most in depth of discussions, I think the outcome
>> of the 'how to handle quaternion sysfs naming' element of the
>> thread.
>> [RFC] IIO: New interfaces for 'interesting' channel types.
>>
>> was that we should do them as a modifier instead of a new channel type.
>> e.g.
>>
>> in_rot_quaternion_raw
>> etc.
>>
>> Could you convert this driver over and repost?
>>
>> If you disagree, then feel free to contribute to that discussion and
>> we'll continue from there.
>>
>> Sorry this took so long to sort out, I was hoping for a few more inputs
>> but there we are...
>>
> Hi Jonathan,
>
> I have resubmitted with the modifier for quaternion dropping this patch.
> I only allow buffered mode as without this patch representing in raw mode
> all four component is not possible. Current applications only is using buffer mode.
> So I can wait for core support something similar to this patch.
>
> Thanks,
> Srinivas
Hi Srinivas,

I'm a bit lost on this.  I was quite happy with this patch and can't immediately
see why the change to using a modifier requires dropping it?

It just makes for slightly different handling in the read_raw_multi call inside
the driver itself...

or am I missing something...

Jonathan
>
>
>
>
>> Jonathan
>>> ---
>>>   drivers/iio/iio_core.h           |  2 +-
>>>   drivers/iio/industrialio-core.c  | 51 +++++++++++++++++++++++-----------------
>>>   drivers/iio/industrialio-event.c |  6 +++--
>>>   drivers/iio/inkern.c             | 15 ++++++++++--
>>>   include/linux/iio/iio.h          | 16 +++++++++++++
>>>   5 files changed, 63 insertions(+), 27 deletions(-)
>>>
>>> diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
>>> index f6db6af..4172f22 100644
>>> --- a/drivers/iio/iio_core.h
>>> +++ b/drivers/iio/iio_core.h
>>> @@ -35,7 +35,7 @@ int __iio_add_chan_devattr(const char *postfix,
>>>                  struct list_head *attr_list);
>>>   void iio_free_chan_devattr_list(struct list_head *attr_list);
>>>
>>> -ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2);
>>> +ssize_t iio_format_value(char *buf, unsigned int type, int *val);
>>>
>>>   /* Event interface flags */
>>>   #define IIO_BUSY_BIT_POS 1
>>> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
>>> index acc911a..b1225a4 100644
>>> --- a/drivers/iio/industrialio-core.c
>>> +++ b/drivers/iio/industrialio-core.c
>>> @@ -373,41 +373,42 @@ EXPORT_SYMBOL_GPL(iio_enum_write);
>>>    * @buf: The buffer to which the formated value gets written
>>>    * @type: One of the IIO_VAL_... constants. This decides how the val and val2
>>>    *        parameters are formatted.
>>> - * @val: First part of the value, exact meaning depends on the type parameter.
>>> - * @val2: Second part of the value, exact meaning depends on the type parameter.
>>> + * @vals: pointer to the values, exact meaning depends on the type parameter.
>>>    */
>>> -ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2)
>>> +ssize_t iio_format_value(char *buf, unsigned int type, int *vals)
>>>   {
>>>       unsigned long long tmp;
>>>       bool scale_db = false;
>>>
>>>       switch (type) {
>>>       case IIO_VAL_INT:
>>> -        return sprintf(buf, "%d\n", val);
>>> +        return sprintf(buf, "%d\n", vals[0]);
>>>       case IIO_VAL_INT_PLUS_MICRO_DB:
>>>           scale_db = true;
>>>       case IIO_VAL_INT_PLUS_MICRO:
>>> -        if (val2 < 0)
>>> -            return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
>>> +        if (vals[1] < 0)
>>> +            return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
>>> +                    -vals[1],
>>>                   scale_db ? " dB" : "");
>>>           else
>>> -            return sprintf(buf, "%d.%06u%s\n", val, val2,
>>> +            return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
>>>                   scale_db ? " dB" : "");
>>>       case IIO_VAL_INT_PLUS_NANO:
>>> -        if (val2 < 0)
>>> -            return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
>>> +        if (vals[1] < 0)
>>> +            return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
>>> +                    -vals[1]);
>>>           else
>>> -            return sprintf(buf, "%d.%09u\n", val, val2);
>>> +            return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>>       case IIO_VAL_FRACTIONAL:
>>> -        tmp = div_s64((s64)val * 1000000000LL, val2);
>>> -        val2 = do_div(tmp, 1000000000LL);
>>> -        val = tmp;
>>> -        return sprintf(buf, "%d.%09u\n", val, val2);
>>> +        tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
>>> +        vals[1] = do_div(tmp, 1000000000LL);
>>> +        vals[0] = tmp;
>>> +        return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>>       case IIO_VAL_FRACTIONAL_LOG2:
>>> -        tmp = (s64)val * 1000000000LL >> val2;
>>> -        val2 = do_div(tmp, 1000000000LL);
>>> -        val = tmp;
>>> -        return sprintf(buf, "%d.%09u\n", val, val2);
>>> +        tmp = (s64)vals[0] * 1000000000LL >> vals[1];
>>> +        vals[1] = do_div(tmp, 1000000000LL);
>>> +        vals[0] = tmp;
>>> +        return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
>>>       default:
>>>           return 0;
>>>       }
>>> @@ -419,14 +420,20 @@ static ssize_t iio_read_channel_info(struct device *dev,
>>>   {
>>>       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>>>       struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
>>> -    int val, val2;
>>> -    int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
>>> -                        &val, &val2, this_attr->address);
>>> +    int vals[INDIO_MAX_RAW_ELEMENTS];
>>> +    int ret;
>>> +
>>> +    if (indio_dev->info->read_raw_multi)
>>> +        ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
>>> +                INDIO_MAX_RAW_ELEMENTS, vals, this_attr->address);
>>> +    else
>>> +        ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
>>> +                    &vals[0], &vals[1], this_attr->address);
>>>
>>>       if (ret < 0)
>>>           return ret;
>>>
>>> -    return iio_format_value(buf, ret, val, val2);
>>> +    return iio_format_value(buf, ret, vals);
>>>   }
>>>
>>>   /**
>>> diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
>>> index c9c1419..0f7f78e 100644
>>> --- a/drivers/iio/industrialio-event.c
>>> +++ b/drivers/iio/industrialio-event.c
>>> @@ -272,7 +272,7 @@ static ssize_t iio_ev_value_show(struct device *dev,
>>>   {
>>>       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>>>       struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
>>> -    int val, val2;
>>> +    int val, val2, val_arr[2];
>>>       int ret;
>>>
>>>       ret = indio_dev->info->read_event_value(indio_dev,
>>> @@ -281,7 +281,9 @@ static ssize_t iio_ev_value_show(struct device *dev,
>>>           &val, &val2);
>>>       if (ret < 0)
>>>           return ret;
>>> -    return iio_format_value(buf, ret, val, val2);
>>> +    val_arr[0] = val;
>>> +    val_arr[1] = val2;
>>> +    return iio_format_value(buf, ret, val_arr);
>>>   }
>>>
>>>   static ssize_t iio_ev_value_store(struct device *dev,
>>> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
>>> index 0cf5f8e..6a93d57 100644
>>> --- a/drivers/iio/inkern.c
>>> +++ b/drivers/iio/inkern.c
>>> @@ -417,12 +417,23 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
>>>       enum iio_chan_info_enum info)
>>>   {
>>>       int unused;
>>> +    int vals[INDIO_MAX_RAW_ELEMENTS];
>>> +    int ret;
>>>
>>>       if (val2 == NULL)
>>>           val2 = &unused;
>>>
>>> -    return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
>>> -                        val, val2, info);
>>> +    if (chan->indio_dev->info->read_raw_multi) {
>>> +        ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
>>> +                    chan->channel, INDIO_MAX_RAW_ELEMENTS,
>>> +                    vals, info);
>>> +        *val = vals[0];
>>> +        *val2 = vals[1];
>>> +    } else
>>> +        ret = chan->indio_dev->info->read_raw(chan->indio_dev,
>>> +                    chan->channel, val, val2, info);
>>> +
>>> +    return ret;
>>>   }
>>>
>>>   int iio_read_channel_raw(struct iio_channel *chan, int *val)
>>> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
>>> index 75a8a20..ddf9f60 100644
>>> --- a/include/linux/iio/iio.h
>>> +++ b/include/linux/iio/iio.h
>>> @@ -284,6 +284,8 @@ static inline s64 iio_get_time_ns(void)
>>>   #define INDIO_ALL_BUFFER_MODES                    \
>>>       (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
>>>
>>> +#define INDIO_MAX_RAW_ELEMENTS        4
>>> +
>>>   struct iio_trigger; /* forward declaration */
>>>   struct iio_dev;
>>>
>>> @@ -298,6 +300,14 @@ struct iio_dev;
>>>    *            the channel in question.  Return value will specify the
>>>    *            type of value returned by the device. val and val2 will
>>>    *            contain the elements making up the returned value.
>>> + * @read_raw_multi:    function to return values from the device.
>>> + *            mask specifies which value. Note 0 means a reading of
>>> + *            the channel in question.  Return value will specify the
>>> + *            type of value returned by the device. vals pointer
>>> + *            contain the elements making up the returned value.
>>> + *            val_len specifies maximum number of elements
>>> + *            vals pointer can contain. In contrast to to read_raw
>>> + *            device can return multiple values not just two.
>>>    * @write_raw:        function to write a value to the device.
>>>    *            Parameters are the same as for read_raw.
>>>    * @write_raw_get_fmt:    callback function to query the expected
>>> @@ -324,6 +334,12 @@ struct iio_info {
>>>               int *val2,
>>>               long mask);
>>>
>>> +    int (*read_raw_multi)(struct iio_dev *indio_dev,
>>> +            struct iio_chan_spec const *chan,
>>> +            int val_len,
>>> +            int *vals,
>>> +            long mask);
>>> +
>>>       int (*write_raw)(struct iio_dev *indio_dev,
>>>                struct iio_chan_spec const *chan,
>>>                int val,
>>>
>>
>>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


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

* Re: [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver
  2014-04-05 10:07     ` Jonathan Cameron
@ 2014-04-07 16:05       ` gmane
  2014-04-07 16:26         ` Jonathan Cameron
  0 siblings, 1 reply; 15+ messages in thread
From: gmane @ 2014-04-07 16:05 UTC (permalink / raw)
  To: linux-iio; +Cc: Jonathan Cameron, pmeerw

Hi,

I just stumbled across this [1] and was wondering if this patch made it 
into some tree.

Regards,

Robert

[1] http://thread.gmane.org/gmane.linux.kernel.iio/10876

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

* Re: [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver
  2014-04-07 16:05       ` [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver gmane
@ 2014-04-07 16:26         ` Jonathan Cameron
  2014-04-07 16:32           ` Peter Meerwald
  0 siblings, 1 reply; 15+ messages in thread
From: Jonathan Cameron @ 2014-04-07 16:26 UTC (permalink / raw)
  To: gmane, linux-iio; +Cc: pmeerw

Nope. I guess Peter never had a chance to get back to it. I had forgotten about it entirely...

On April 7, 2014 5:05:36 PM GMT+01:00, gmane@reliableembeddedsystems.com wrote:
>Hi,
>
>I just stumbled across this [1] and was wondering if this patch made it
>
>into some tree.
>
>Regards,
>
>Robert
>
>[1] http://thread.gmane.org/gmane.linux.kernel.iio/10876
>--
>To unsubscribe from this list: send the line "unsubscribe linux-iio" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

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

* Re: [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver
  2014-04-07 16:26         ` Jonathan Cameron
@ 2014-04-07 16:32           ` Peter Meerwald
  0 siblings, 0 replies; 15+ messages in thread
From: Peter Meerwald @ 2014-04-07 16:32 UTC (permalink / raw)
  To: Jonathan Cameron; +Cc: gmane, linux-iio

Hi,

> Nope. I guess Peter never had a chance to get back to it. I had forgotten about it entirely...

I think there were some minor comments; I'll try to clean them up and 
resubmit shortly

regards, p.

> >I just stumbled across this [1] and was wondering if this patch made it
> >
> >into some tree.
> >
> >Regards,
> >
> >Robert
> >
> >[1] http://thread.gmane.org/gmane.linux.kernel.iio/10876
> >--
> >To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> >the body of a message to majordomo@vger.kernel.org
> >More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> 

-- 

Peter Meerwald
+43-664-2444418 (mobile)

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

* Re: [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver
  2014-05-02  8:51 ` [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver Peter Meerwald
@ 2014-05-03 11:13   ` Jonathan Cameron
  0 siblings, 0 replies; 15+ messages in thread
From: Jonathan Cameron @ 2014-05-03 11:13 UTC (permalink / raw)
  To: Peter Meerwald, linux-iio

On 02/05/14 09:51, Peter Meerwald wrote:
> 3-axis accelerometer sensor (2/4/8 g) with 12-bit resolution
> and I2C interface
>
> many extra features are unsupported (freefall detection, orientation
> change, autosleep)
>
> datasheet is here:
> http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf
>
> v2: (thanks to Jonathan Cameron)
> * use ARRAY_SIZE()
>
> Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
Fair enough on your responses in the cover letter.
Applied to the togreg branch of iio.git

Thanks,

Jonathan
> ---
>   drivers/iio/accel/Kconfig   |  12 ++
>   drivers/iio/accel/Makefile  |   1 +
>   drivers/iio/accel/mma8452.c | 439 ++++++++++++++++++++++++++++++++++++++++++++
>   3 files changed, 452 insertions(+)
>   create mode 100644 drivers/iio/accel/mma8452.c
>
> diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
> index e23e5085..1e120fa 100644
> --- a/drivers/iio/accel/Kconfig
> +++ b/drivers/iio/accel/Kconfig
> @@ -65,4 +65,16 @@ config KXSD9
>   	  Say yes here to build support for the Kionix KXSD9 accelerometer.
>   	  Currently this only supports the device via an SPI interface.
>
> +config MMA8452
> +	tristate "Freescale MMA8452Q Accelerometer Driver"
> +	depends on I2C
> +	select IIO_BUFFER
> +	select IIO_TRIGGERED_BUFFER
> +	help
> +	  Say yes here to build support for the Freescale MMA8452Q 3-axis
> +	  accelerometer.
> +
> +	  To compile this driver as a module, choose M here: the module
> +	  will be called mma8452.
> +
>   endmenu
> diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
> index c48d15f..dc0e379 100644
> --- a/drivers/iio/accel/Makefile
> +++ b/drivers/iio/accel/Makefile
> @@ -6,6 +6,7 @@
>   obj-$(CONFIG_BMA180) += bma180.o
>   obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
>   obj-$(CONFIG_KXSD9)	+= kxsd9.o
> +obj-$(CONFIG_MMA8452)	+= mma8452.o
>
>   obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
>   st_accel-y := st_accel_core.o
> diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
> new file mode 100644
> index 0000000..17aeea1
> --- /dev/null
> +++ b/drivers/iio/accel/mma8452.c
> @@ -0,0 +1,439 @@
> +/*
> + * mma8452.c - Support for Freescale MMA8452Q 3-axis 12-bit accelerometer
> + *
> + * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
> + *
> + * This file is subject to the terms and conditions of version 2 of
> + * the GNU General Public License.  See the file COPYING in the main
> + * directory of this archive for more details.
> + *
> + * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
> + *
> + * TODO: interrupt, thresholding, orientation / freefall events, autosleep
> + */
> +
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/delay.h>
> +
> +#define MMA8452_STATUS 0x00
> +#define MMA8452_OUT_X 0x01 /* MSB first, 12-bit  */
> +#define MMA8452_OUT_Y 0x03
> +#define MMA8452_OUT_Z 0x05
> +#define MMA8452_WHO_AM_I 0x0d
> +#define MMA8452_DATA_CFG 0x0e
> +#define MMA8452_OFF_X 0x2f
> +#define MMA8452_OFF_Y 0x30
> +#define MMA8452_OFF_Z 0x31
> +#define MMA8452_CTRL_REG1 0x2a
> +#define MMA8452_CTRL_REG2 0x2b
> +
> +#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
> +
> +#define MMA8452_CTRL_DR_MASK (BIT(5) | BIT(4) | BIT(3))
> +#define MMA8452_CTRL_DR_SHIFT 3
> +#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
> +#define MMA8452_CTRL_ACTIVE BIT(0)
> +
> +#define MMA8452_DATA_CFG_FS_MASK (BIT(1) | BIT(0))
> +#define MMA8452_DATA_CFG_FS_2G 0
> +#define MMA8452_DATA_CFG_FS_4G 1
> +#define MMA8452_DATA_CFG_FS_8G 2
> +
> +#define MMA8452_DEVICE_ID 0x2a
> +
> +struct mma8452_data {
> +	struct i2c_client *client;
> +	struct mutex lock;
> +	u8 ctrl_reg1;
> +	u8 data_cfg;
> +};
> +
> +static int mma8452_drdy(struct mma8452_data *data)
> +{
> +	int tries = 150;
> +
> +	while (tries-- > 0) {
> +		int ret = i2c_smbus_read_byte_data(data->client,
> +			MMA8452_STATUS);
> +		if (ret < 0)
> +			return ret;
> +		if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
> +			return 0;
> +		msleep(20);
> +	}
> +
> +	dev_err(&data->client->dev, "data not ready\n");
> +	return -EIO;
> +}
> +
> +static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
> +{
> +	int ret = mma8452_drdy(data);
> +	if (ret < 0)
> +		return ret;
> +	return i2c_smbus_read_i2c_block_data(data->client,
> +		MMA8452_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
> +}
> +
> +static ssize_t mma8452_show_int_plus_micros(char *buf,
> +	const int (*vals)[2], int n)
> +{
> +	size_t len = 0;
> +
> +	while (n-- > 0)
> +		len += scnprintf(buf + len, PAGE_SIZE - len,
> +			"%d.%06d ", vals[n][0], vals[n][1]);
> +
> +	/* replace trailing space by newline */
> +	buf[len - 1] = '\n';
> +
> +	return len;
> +}
> +
> +static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
> +					int val, int val2)
> +{
> +	while (n-- > 0)
> +		if (val == vals[n][0] && val2 == vals[n][1])
> +			return n;
> +
> +	return -EINVAL;
> +}
> +
> +static const int mma8452_samp_freq[8][2] = {
> +	{800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
> +	{6, 250000}, {1, 560000}
> +};
> +
> +static const int mma8452_scales[3][2] = {
> +	{0, 977}, {0, 1953}, {0, 3906}
> +};
> +
> +static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
> +				struct device_attribute *attr, char *buf)
> +{
> +	return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
> +		ARRAY_SIZE(mma8452_samp_freq));
> +}
> +
> +static ssize_t mma8452_show_scale_avail(struct device *dev,
> +				struct device_attribute *attr, char *buf)
> +{
> +	return mma8452_show_int_plus_micros(buf, mma8452_scales,
> +		ARRAY_SIZE(mma8452_scales));
> +}
> +
> +static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
> +static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
> +	mma8452_show_scale_avail, NULL, 0);
> +
> +static int mma8452_get_samp_freq_index(struct mma8452_data *data,
> +	int val, int val2)
> +{
> +	return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
> +		ARRAY_SIZE(mma8452_samp_freq), val, val2);
> +}
> +
> +static int mma8452_get_scale_index(struct mma8452_data *data,
> +	int val, int val2)
> +{
> +	return mma8452_get_int_plus_micros_index(mma8452_scales,
> +		ARRAY_SIZE(mma8452_scales), val, val2);
> +}
> +
> +static int mma8452_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan,
> +			    int *val, int *val2, long mask)
> +{
> +	struct mma8452_data *data = iio_priv(indio_dev);
> +	__be16 buffer[3];
> +	int i, ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		if (iio_buffer_enabled(indio_dev))
> +			return -EBUSY;
> +
> +		mutex_lock(&data->lock);
> +		ret = mma8452_read(data, buffer);
> +		mutex_unlock(&data->lock);
> +		if (ret < 0)
> +			return ret;
> +		*val = sign_extend32(
> +			be16_to_cpu(buffer[chan->scan_index]) >> 4, 11);
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_SCALE:
> +		i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
> +		*val = mma8452_scales[i][0];
> +		*val2 = mma8452_scales[i][1];
> +		return IIO_VAL_INT_PLUS_MICRO;
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		i = (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
> +			MMA8452_CTRL_DR_SHIFT;
> +		*val = mma8452_samp_freq[i][0];
> +		*val2 = mma8452_samp_freq[i][1];
> +		return IIO_VAL_INT_PLUS_MICRO;
> +	case IIO_CHAN_INFO_CALIBBIAS:
> +		ret = i2c_smbus_read_byte_data(data->client, MMA8452_OFF_X +
> +			chan->scan_index);
> +		if (ret < 0)
> +			return ret;
> +		*val = sign_extend32(ret, 7);
> +		return IIO_VAL_INT;
> +	}
> +	return -EINVAL;
> +}
> +
> +static int mma8452_standby(struct mma8452_data *data)
> +{
> +	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
> +		data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
> +}
> +
> +static int mma8452_active(struct mma8452_data *data)
> +{
> +	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
> +		data->ctrl_reg1);
> +}
> +
> +static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
> +{
> +	int ret;
> +
> +	mutex_lock(&data->lock);
> +
> +	/* config can only be changed when in standby */
> +	ret = mma8452_standby(data);
> +	if (ret < 0)
> +		goto fail;
> +
> +	ret = i2c_smbus_write_byte_data(data->client, reg, val);
> +	if (ret < 0)
> +		goto fail;
> +
> +	ret = mma8452_active(data);
> +	if (ret < 0)
> +		goto fail;
> +
> +	ret = 0;
> +fail:
> +	mutex_unlock(&data->lock);
> +	return ret;
> +}
> +
> +static int mma8452_write_raw(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan,
> +			     int val, int val2, long mask)
> +{
> +	struct mma8452_data *data = iio_priv(indio_dev);
> +	int i;
> +
> +	if (iio_buffer_enabled(indio_dev))
> +		return -EBUSY;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		i = mma8452_get_samp_freq_index(data, val, val2);
> +		if (i < 0)
> +			return -EINVAL;
> +
> +		data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
> +		data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
> +		return mma8452_change_config(data, MMA8452_CTRL_REG1,
> +			data->ctrl_reg1);
> +	case IIO_CHAN_INFO_SCALE:
> +		i = mma8452_get_scale_index(data, val, val2);
> +		if (i < 0)
> +			return -EINVAL;
> +		data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
> +		data->data_cfg |= i;
> +		return mma8452_change_config(data, MMA8452_DATA_CFG,
> +			data->data_cfg);
> +	case IIO_CHAN_INFO_CALIBBIAS:
> +		if (val < -128 || val > 127)
> +			return -EINVAL;
> +		return mma8452_change_config(data, MMA8452_OFF_X +
> +			chan->scan_index, val);
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static irqreturn_t mma8452_trigger_handler(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct mma8452_data *data = iio_priv(indio_dev);
> +	u8 buffer[16]; /* 3 16-bit channels + padding + ts */
> +	int ret;
> +
> +	ret = mma8452_read(data, (__be16 *) buffer);
> +	if (ret < 0)
> +		goto done;
> +
> +	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
> +		iio_get_time_ns());
> +
> +done:
> +	iio_trigger_notify_done(indio_dev->trig);
> +	return IRQ_HANDLED;
> +}
> +
> +#define MMA8452_CHANNEL(axis, idx) { \
> +	.type = IIO_ACCEL, \
> +	.modified = 1, \
> +	.channel2 = IIO_MOD_##axis, \
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> +		BIT(IIO_CHAN_INFO_CALIBBIAS), \
> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> +		BIT(IIO_CHAN_INFO_SCALE), \
> +	.scan_index = idx, \
> +	.scan_type = { \
> +		.sign = 's', \
> +		.realbits = 12, \
> +		.storagebits = 16, \
> +		.shift = 4, \
> +		.endianness = IIO_BE, \
> +	}, \
> +}
> +
> +static const struct iio_chan_spec mma8452_channels[] = {
> +	MMA8452_CHANNEL(X, 0),
> +	MMA8452_CHANNEL(Y, 1),
> +	MMA8452_CHANNEL(Z, 2),
> +	IIO_CHAN_SOFT_TIMESTAMP(3),
> +};
> +
> +static struct attribute *mma8452_attributes[] = {
> +	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
> +	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
> +	NULL
> +};
> +
> +static const struct attribute_group mma8452_group = {
> +	.attrs = mma8452_attributes,
> +};
> +
> +static const struct iio_info mma8452_info = {
> +	.attrs = &mma8452_group,
> +	.read_raw = &mma8452_read_raw,
> +	.write_raw = &mma8452_write_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static const unsigned long mma8452_scan_masks[] = {0x7, 0};
> +
> +static int mma8452_probe(struct i2c_client *client,
> +			 const struct i2c_device_id *id)
> +{
> +	struct mma8452_data *data;
> +	struct iio_dev *indio_dev;
> +	int ret;
> +
> +	ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
> +	if (ret < 0)
> +		return ret;
> +	if (ret != MMA8452_DEVICE_ID)
> +		return -ENODEV;
> +
> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	data = iio_priv(indio_dev);
> +	data->client = client;
> +	mutex_init(&data->lock);
> +
> +	i2c_set_clientdata(client, indio_dev);
> +	indio_dev->info = &mma8452_info;
> +	indio_dev->name = id->name;
> +	indio_dev->dev.parent = &client->dev;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->channels = mma8452_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
> +	indio_dev->available_scan_masks = mma8452_scan_masks;
> +
> +	data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
> +		(MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
> +	ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
> +		data->ctrl_reg1);
> +	if (ret < 0)
> +		return ret;
> +
> +	data->data_cfg = MMA8452_DATA_CFG_FS_2G;
> +	ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
> +		data->data_cfg);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = iio_triggered_buffer_setup(indio_dev, NULL,
> +		mma8452_trigger_handler, NULL);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret < 0)
> +		goto buffer_cleanup;
> +	return 0;
> +
> +buffer_cleanup:
> +	iio_triggered_buffer_cleanup(indio_dev);
> +	return ret;
> +}
> +
> +static int mma8452_remove(struct i2c_client *client)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> +
> +	iio_device_unregister(indio_dev);
> +	iio_triggered_buffer_cleanup(indio_dev);
> +	mma8452_standby(iio_priv(indio_dev));
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mma8452_suspend(struct device *dev)
> +{
> +	return mma8452_standby(iio_priv(i2c_get_clientdata(
> +		to_i2c_client(dev))));
> +}
> +
> +static int mma8452_resume(struct device *dev)
> +{
> +	return mma8452_active(iio_priv(i2c_get_clientdata(
> +		to_i2c_client(dev))));
> +}
> +
> +static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
> +#define MMA8452_PM_OPS (&mma8452_pm_ops)
> +#else
> +#define MMA8452_PM_OPS NULL
> +#endif
> +
> +static const struct i2c_device_id mma8452_id[] = {
> +	{ "mma8452", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, mma8452_id);
> +
> +static struct i2c_driver mma8452_driver = {
> +	.driver = {
> +		.name	= "mma8452",
> +		.pm	= MMA8452_PM_OPS,
> +	},
> +	.probe = mma8452_probe,
> +	.remove = mma8452_remove,
> +	.id_table = mma8452_id,
> +};
> +module_i2c_driver(mma8452_driver);
> +
> +MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
> +MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
> +MODULE_LICENSE("GPL");
>


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

* [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver
  2014-05-02  8:51 [PATCH] iio: mma8452 driver Peter Meerwald
@ 2014-05-02  8:51 ` Peter Meerwald
  2014-05-03 11:13   ` Jonathan Cameron
  0 siblings, 1 reply; 15+ messages in thread
From: Peter Meerwald @ 2014-05-02  8:51 UTC (permalink / raw)
  To: linux-iio; +Cc: jic23, Peter Meerwald

3-axis accelerometer sensor (2/4/8 g) with 12-bit resolution
and I2C interface

many extra features are unsupported (freefall detection, orientation
change, autosleep)

datasheet is here:
http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf

v2: (thanks to Jonathan Cameron)
* use ARRAY_SIZE()

Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
---
 drivers/iio/accel/Kconfig   |  12 ++
 drivers/iio/accel/Makefile  |   1 +
 drivers/iio/accel/mma8452.c | 439 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 452 insertions(+)
 create mode 100644 drivers/iio/accel/mma8452.c

diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index e23e5085..1e120fa 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -65,4 +65,16 @@ config KXSD9
 	  Say yes here to build support for the Kionix KXSD9 accelerometer.
 	  Currently this only supports the device via an SPI interface.
 
+config MMA8452
+	tristate "Freescale MMA8452Q Accelerometer Driver"
+	depends on I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say yes here to build support for the Freescale MMA8452Q 3-axis
+	  accelerometer.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called mma8452.
+
 endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index c48d15f..dc0e379 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -6,6 +6,7 @@
 obj-$(CONFIG_BMA180) += bma180.o
 obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
 obj-$(CONFIG_KXSD9)	+= kxsd9.o
+obj-$(CONFIG_MMA8452)	+= mma8452.o
 
 obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
 st_accel-y := st_accel_core.o
diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
new file mode 100644
index 0000000..17aeea1
--- /dev/null
+++ b/drivers/iio/accel/mma8452.c
@@ -0,0 +1,439 @@
+/*
+ * mma8452.c - Support for Freescale MMA8452Q 3-axis 12-bit accelerometer
+ *
+ * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
+ *
+ * TODO: interrupt, thresholding, orientation / freefall events, autosleep
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/delay.h>
+
+#define MMA8452_STATUS 0x00
+#define MMA8452_OUT_X 0x01 /* MSB first, 12-bit  */
+#define MMA8452_OUT_Y 0x03
+#define MMA8452_OUT_Z 0x05
+#define MMA8452_WHO_AM_I 0x0d
+#define MMA8452_DATA_CFG 0x0e
+#define MMA8452_OFF_X 0x2f
+#define MMA8452_OFF_Y 0x30
+#define MMA8452_OFF_Z 0x31
+#define MMA8452_CTRL_REG1 0x2a
+#define MMA8452_CTRL_REG2 0x2b
+
+#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
+
+#define MMA8452_CTRL_DR_MASK (BIT(5) | BIT(4) | BIT(3))
+#define MMA8452_CTRL_DR_SHIFT 3
+#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
+#define MMA8452_CTRL_ACTIVE BIT(0)
+
+#define MMA8452_DATA_CFG_FS_MASK (BIT(1) | BIT(0))
+#define MMA8452_DATA_CFG_FS_2G 0
+#define MMA8452_DATA_CFG_FS_4G 1
+#define MMA8452_DATA_CFG_FS_8G 2
+
+#define MMA8452_DEVICE_ID 0x2a
+
+struct mma8452_data {
+	struct i2c_client *client;
+	struct mutex lock;
+	u8 ctrl_reg1;
+	u8 data_cfg;
+};
+
+static int mma8452_drdy(struct mma8452_data *data)
+{
+	int tries = 150;
+
+	while (tries-- > 0) {
+		int ret = i2c_smbus_read_byte_data(data->client,
+			MMA8452_STATUS);
+		if (ret < 0)
+			return ret;
+		if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
+			return 0;
+		msleep(20);
+	}
+
+	dev_err(&data->client->dev, "data not ready\n");
+	return -EIO;
+}
+
+static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
+{
+	int ret = mma8452_drdy(data);
+	if (ret < 0)
+		return ret;
+	return i2c_smbus_read_i2c_block_data(data->client,
+		MMA8452_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
+}
+
+static ssize_t mma8452_show_int_plus_micros(char *buf,
+	const int (*vals)[2], int n)
+{
+	size_t len = 0;
+
+	while (n-- > 0)
+		len += scnprintf(buf + len, PAGE_SIZE - len,
+			"%d.%06d ", vals[n][0], vals[n][1]);
+
+	/* replace trailing space by newline */
+	buf[len - 1] = '\n';
+
+	return len;
+}
+
+static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
+					int val, int val2)
+{
+	while (n-- > 0)
+		if (val == vals[n][0] && val2 == vals[n][1])
+			return n;
+
+	return -EINVAL;
+}
+
+static const int mma8452_samp_freq[8][2] = {
+	{800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
+	{6, 250000}, {1, 560000}
+};
+
+static const int mma8452_scales[3][2] = {
+	{0, 977}, {0, 1953}, {0, 3906}
+};
+
+static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
+		ARRAY_SIZE(mma8452_samp_freq));
+}
+
+static ssize_t mma8452_show_scale_avail(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return mma8452_show_int_plus_micros(buf, mma8452_scales,
+		ARRAY_SIZE(mma8452_scales));
+}
+
+static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
+static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
+	mma8452_show_scale_avail, NULL, 0);
+
+static int mma8452_get_samp_freq_index(struct mma8452_data *data,
+	int val, int val2)
+{
+	return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
+		ARRAY_SIZE(mma8452_samp_freq), val, val2);
+}
+
+static int mma8452_get_scale_index(struct mma8452_data *data,
+	int val, int val2)
+{
+	return mma8452_get_int_plus_micros_index(mma8452_scales,
+		ARRAY_SIZE(mma8452_scales), val, val2);
+}
+
+static int mma8452_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
+{
+	struct mma8452_data *data = iio_priv(indio_dev);
+	__be16 buffer[3];
+	int i, ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;
+
+		mutex_lock(&data->lock);
+		ret = mma8452_read(data, buffer);
+		mutex_unlock(&data->lock);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(
+			be16_to_cpu(buffer[chan->scan_index]) >> 4, 11);
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
+		*val = mma8452_scales[i][0];
+		*val2 = mma8452_scales[i][1];
+		return IIO_VAL_INT_PLUS_MICRO;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		i = (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
+			MMA8452_CTRL_DR_SHIFT;
+		*val = mma8452_samp_freq[i][0];
+		*val2 = mma8452_samp_freq[i][1];
+		return IIO_VAL_INT_PLUS_MICRO;
+	case IIO_CHAN_INFO_CALIBBIAS:
+		ret = i2c_smbus_read_byte_data(data->client, MMA8452_OFF_X +
+			chan->scan_index);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(ret, 7);
+		return IIO_VAL_INT;
+	}
+	return -EINVAL;
+}
+
+static int mma8452_standby(struct mma8452_data *data)
+{
+	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
+		data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
+}
+
+static int mma8452_active(struct mma8452_data *data)
+{
+	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
+		data->ctrl_reg1);
+}
+
+static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
+{
+	int ret;
+
+	mutex_lock(&data->lock);
+
+	/* config can only be changed when in standby */
+	ret = mma8452_standby(data);
+	if (ret < 0)
+		goto fail;
+
+	ret = i2c_smbus_write_byte_data(data->client, reg, val);
+	if (ret < 0)
+		goto fail;
+
+	ret = mma8452_active(data);
+	if (ret < 0)
+		goto fail;
+
+	ret = 0;
+fail:
+	mutex_unlock(&data->lock);
+	return ret;
+}
+
+static int mma8452_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask)
+{
+	struct mma8452_data *data = iio_priv(indio_dev);
+	int i;
+
+	if (iio_buffer_enabled(indio_dev))
+		return -EBUSY;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		i = mma8452_get_samp_freq_index(data, val, val2);
+		if (i < 0)
+			return -EINVAL;
+
+		data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
+		data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
+		return mma8452_change_config(data, MMA8452_CTRL_REG1,
+			data->ctrl_reg1);
+	case IIO_CHAN_INFO_SCALE:
+		i = mma8452_get_scale_index(data, val, val2);
+		if (i < 0)
+			return -EINVAL;
+		data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
+		data->data_cfg |= i;
+		return mma8452_change_config(data, MMA8452_DATA_CFG,
+			data->data_cfg);
+	case IIO_CHAN_INFO_CALIBBIAS:
+		if (val < -128 || val > 127)
+			return -EINVAL;
+		return mma8452_change_config(data, MMA8452_OFF_X +
+			chan->scan_index, val);
+	default:
+		return -EINVAL;
+	}
+}
+
+static irqreturn_t mma8452_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct mma8452_data *data = iio_priv(indio_dev);
+	u8 buffer[16]; /* 3 16-bit channels + padding + ts */
+	int ret;
+
+	ret = mma8452_read(data, (__be16 *) buffer);
+	if (ret < 0)
+		goto done;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
+		iio_get_time_ns());
+
+done:
+	iio_trigger_notify_done(indio_dev->trig);
+	return IRQ_HANDLED;
+}
+
+#define MMA8452_CHANNEL(axis, idx) { \
+	.type = IIO_ACCEL, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_##axis, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
+		BIT(IIO_CHAN_INFO_CALIBBIAS), \
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
+		BIT(IIO_CHAN_INFO_SCALE), \
+	.scan_index = idx, \
+	.scan_type = { \
+		.sign = 's', \
+		.realbits = 12, \
+		.storagebits = 16, \
+		.shift = 4, \
+		.endianness = IIO_BE, \
+	}, \
+}
+
+static const struct iio_chan_spec mma8452_channels[] = {
+	MMA8452_CHANNEL(X, 0),
+	MMA8452_CHANNEL(Y, 1),
+	MMA8452_CHANNEL(Z, 2),
+	IIO_CHAN_SOFT_TIMESTAMP(3),
+};
+
+static struct attribute *mma8452_attributes[] = {
+	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
+	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
+	NULL
+};
+
+static const struct attribute_group mma8452_group = {
+	.attrs = mma8452_attributes,
+};
+
+static const struct iio_info mma8452_info = {
+	.attrs = &mma8452_group,
+	.read_raw = &mma8452_read_raw,
+	.write_raw = &mma8452_write_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static const unsigned long mma8452_scan_masks[] = {0x7, 0};
+
+static int mma8452_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct mma8452_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
+	if (ret < 0)
+		return ret;
+	if (ret != MMA8452_DEVICE_ID)
+		return -ENODEV;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	data->client = client;
+	mutex_init(&data->lock);
+
+	i2c_set_clientdata(client, indio_dev);
+	indio_dev->info = &mma8452_info;
+	indio_dev->name = id->name;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = mma8452_channels;
+	indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
+	indio_dev->available_scan_masks = mma8452_scan_masks;
+
+	data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
+		(MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
+	ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
+		data->ctrl_reg1);
+	if (ret < 0)
+		return ret;
+
+	data->data_cfg = MMA8452_DATA_CFG_FS_2G;
+	ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
+		data->data_cfg);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_triggered_buffer_setup(indio_dev, NULL,
+		mma8452_trigger_handler, NULL);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0)
+		goto buffer_cleanup;
+	return 0;
+
+buffer_cleanup:
+	iio_triggered_buffer_cleanup(indio_dev);
+	return ret;
+}
+
+static int mma8452_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+	iio_device_unregister(indio_dev);
+	iio_triggered_buffer_cleanup(indio_dev);
+	mma8452_standby(iio_priv(indio_dev));
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int mma8452_suspend(struct device *dev)
+{
+	return mma8452_standby(iio_priv(i2c_get_clientdata(
+		to_i2c_client(dev))));
+}
+
+static int mma8452_resume(struct device *dev)
+{
+	return mma8452_active(iio_priv(i2c_get_clientdata(
+		to_i2c_client(dev))));
+}
+
+static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
+#define MMA8452_PM_OPS (&mma8452_pm_ops)
+#else
+#define MMA8452_PM_OPS NULL
+#endif
+
+static const struct i2c_device_id mma8452_id[] = {
+	{ "mma8452", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, mma8452_id);
+
+static struct i2c_driver mma8452_driver = {
+	.driver = {
+		.name	= "mma8452",
+		.pm	= MMA8452_PM_OPS,
+	},
+	.probe = mma8452_probe,
+	.remove = mma8452_remove,
+	.id_table = mma8452_id,
+};
+module_i2c_driver(mma8452_driver);
+
+MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
+MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
+MODULE_LICENSE("GPL");
-- 
1.9.1


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

* Re: [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver
  2014-01-10 22:02 Peter Meerwald
@ 2014-01-11 16:57 ` Jonathan Cameron
  0 siblings, 0 replies; 15+ messages in thread
From: Jonathan Cameron @ 2014-01-11 16:57 UTC (permalink / raw)
  To: Peter Meerwald, linux-iio, Lars-Peter Clausen



On 10/01/14 22:02, Peter Meerwald wrote:
> 3-axis accelerometer sensor (2/4/8 g) with 12-bit resolution
> and I2C interface
>
> many extra features are unsupported (freefall detection, orientation
> change, autosleep)
>
> datasheet is here:
> http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf
>
A few bits inline.

I'd normally expect a device like this to either have a simple driver
with no buffer based access, or to support both buffering and a dataready
trigger (seeing as it has a dataready interrupt).  Here, the device can end
up waiting a very long time for for new data if the trigger is faster than
it should be.   This strikes me as 'interesting'.

Other than this element, the driver looks very nice.

Jonathan
> Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
> ---
>   drivers/iio/accel/Kconfig   |  12 ++
>   drivers/iio/accel/Makefile  |   1 +
>   drivers/iio/accel/mma8452.c | 438 ++++++++++++++++++++++++++++++++++++++++++++
>   3 files changed, 451 insertions(+)
>   create mode 100644 drivers/iio/accel/mma8452.c
>
> diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
> index e23e508..1e120fa 100644
> --- a/drivers/iio/accel/Kconfig
> +++ b/drivers/iio/accel/Kconfig
> @@ -65,4 +65,16 @@ config KXSD9
>   	  Say yes here to build support for the Kionix KXSD9 accelerometer.
>   	  Currently this only supports the device via an SPI interface.
>
> +config MMA8452
> +	tristate "Freescale MMA8452Q Accelerometer Driver"
> +	depends on I2C
> +	select IIO_BUFFER
> +	select IIO_TRIGGERED_BUFFER
> +	help
> +	  Say yes here to build support for the Freescale MMA8452Q 3-axis
> +	  accelerometer.
> +
> +	  To compile this driver as a module, choose M here: the module
> +	  will be called mma8452.
> +
>   endmenu
> diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
> index c48d15f..dc0e379 100644
> --- a/drivers/iio/accel/Makefile
> +++ b/drivers/iio/accel/Makefile
> @@ -6,6 +6,7 @@
>   obj-$(CONFIG_BMA180) += bma180.o
>   obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
>   obj-$(CONFIG_KXSD9)	+= kxsd9.o
> +obj-$(CONFIG_MMA8452)	+= mma8452.o
>
>   obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
>   st_accel-y := st_accel_core.o
> diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
> new file mode 100644
> index 0000000..a45628c
> --- /dev/null
> +++ b/drivers/iio/accel/mma8452.c
> @@ -0,0 +1,438 @@
> +/*
> + * mma8452.c - Support for Freescale MMA8452Q 3-axis 12-bit accelerometer
> + *
> + * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
> + *
> + * This file is subject to the terms and conditions of version 2 of
> + * the GNU General Public License.  See the file COPYING in the main
> + * directory of this archive for more details.
> + *
> + * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
> + *
> + * TODO: interrupt, thresholding, orientation / freefall events, autosleep
> + */
> +
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/trigger_consumer.h>
> +#include <linux/iio/buffer.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/delay.h>
> +
> +#define MMA8452_STATUS 0x00
> +#define MMA8452_OUT_X 0x01 /* MSB first, 12-bit  */
> +#define MMA8452_OUT_Y 0x03
> +#define MMA8452_OUT_Z 0x05
> +#define MMA8452_WHO_AM_I 0x0d
> +#define MMA8452_DATA_CFG 0x0e
> +#define MMA8452_OFF_X 0x2f
> +#define MMA8452_OFF_Y 0x30
> +#define MMA8452_OFF_Z 0x31
> +#define MMA8452_CTRL_REG1 0x2a
> +#define MMA8452_CTRL_REG2 0x2b
> +
> +#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
> +
> +#define MMA8452_CTRL_DR_MASK (BIT(5) | BIT(4) | BIT(3))
> +#define MMA8452_CTRL_DR_SHIFT 3
> +#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
> +#define MMA8452_CTRL_ACTIVE BIT(0)
> +
> +#define MMA8452_DATA_CFG_FS_MASK (BIT(1) | BIT(0))
> +#define MMA8452_DATA_CFG_FS_2G 0
> +#define MMA8452_DATA_CFG_FS_4G 1
> +#define MMA8452_DATA_CFG_FS_8G 2
> +
> +#define MMA8452_DEVICE_ID 0x2a
> +
> +/* Each client has this additional data */
> +struct mma8452_data {
> +	struct i2c_client *client;
> +	struct mutex lock;
> +	u8 ctrl_reg1;
> +	u8 data_cfg;
> +};
> +
> +static int mma8452_drdy(struct mma8452_data *data)
> +{
> +	int tries = 150;
> +
> +	while (tries-- > 0) {
> +		int ret = i2c_smbus_read_byte_data(data->client,
> +			MMA8452_STATUS);
> +		if (ret < 0)
> +			return ret;
> +		if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
> +			return 0;
> +		msleep(20);
> +	}
> +
> +	dev_err(&data->client->dev, "data not ready\n");
> +	return -EIO;
> +}
> +
> +static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
> +{
> +	int ret = mma8452_drdy(data);
> +	if (ret < 0)
> +		return ret;
> +	return i2c_smbus_read_i2c_block_data(data->client,
> +		MMA8452_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
> +}
> +
> +static ssize_t mma8452_show_int_plus_micros(char *buf,
> +	const int (*vals)[2], int n)
> +{
> +	size_t len = 0;
> +
> +	while (n-- > 0)
> +		len += scnprintf(buf + len, PAGE_SIZE - len,
> +			"%d.%06d ", vals[n][0], vals[n][1]);
> +
> +	/* replace trailing space by newline */
> +	buf[len - 1] = '\n';
> +
> +	return len;
> +}
> +
> +static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
> +					int val, int val2)
> +{
> +	while (n-- > 0)
> +		if (val == vals[n][0] && val2 == vals[n][1])
> +			return n;
> +
> +	return -EINVAL;
> +}
> +
> +static const int mma8452_samp_freq[8][2] = {
> +	{800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
> +	{6, 250000}, {1, 560000}
> +};
> +
> +static const int mma8452_scales[3][2] = {
> +	{0, 977}, {0, 1953}, {0, 3906}
> +};
> +
> +static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
> +				struct device_attribute *attr, char *buf)
> +{

I'd prefer to see the 8 done as a ARRAY_SIZE rather than explicitly.
> +	return mma8452_show_int_plus_micros(buf, mma8452_samp_freq, 8);
> +}
> +
> +static ssize_t mma8452_show_scale_avail(struct device *dev,
> +				struct device_attribute *attr, char *buf)
> +{
> +	return mma8452_show_int_plus_micros(buf, mma8452_scales, 3);
> +}
> +
> +static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
> +static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
> +	mma8452_show_scale_avail, NULL, 0);
> +
> +static int mma8452_get_samp_freq_index(struct mma8452_data *data,
> +	int val, int val2)
> +{
> +	return mma8452_get_int_plus_micros_index(mma8452_samp_freq, 8, val,
> +		val2);
> +}
> +
> +static int mma8452_get_scale_index(struct mma8452_data *data,
> +	int val, int val2)
> +{
> +	return mma8452_get_int_plus_micros_index(mma8452_scales, 3, val,
> +		val2);
> +}
> +
> +static int mma8452_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan,
> +			    int *val, int *val2, long mask)
> +{
> +	struct mma8452_data *data = iio_priv(indio_dev);
> +	__be16 buffer[3];
> +	int i, ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		if (iio_buffer_enabled(indio_dev))
> +			return -EBUSY;
> +
This is a little different to normal in that you force it to wait
for new data.  For most sysfs reads where we can't explicitly trigger
a new read, we just use the last valid one. In this case the wait can
be really rather long... Not sure this is a problem, just thought I'd
observe it wasn't what I was expecting.
> +		mutex_lock(&data->lock);
> +		ret = mma8452_read(data, buffer);
> +		mutex_unlock(&data->lock);
> +		if (ret < 0)
> +			return ret;
> +		*val = sign_extend32(
> +			be16_to_cpu(buffer[chan->scan_index]) >> 4, 11);
> +		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_SCALE:
> +		i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
> +		*val = mma8452_scales[i][0];
> +		*val2 = mma8452_scales[i][1];
> +		return IIO_VAL_INT_PLUS_MICRO;
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		i = (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
> +			MMA8452_CTRL_DR_SHIFT;
> +		*val = mma8452_samp_freq[i][0];
> +		*val2 = mma8452_samp_freq[i][1];
> +		return IIO_VAL_INT_PLUS_MICRO;
> +	case IIO_CHAN_INFO_CALIBBIAS:
> +		ret = i2c_smbus_read_byte_data(data->client, MMA8452_OFF_X +
> +			chan->scan_index);
> +		if (ret < 0)
> +			return ret;
> +		*val = sign_extend32(ret, 7);
> +		return IIO_VAL_INT;
> +	}
> +	return -EINVAL;
> +}
> +
> +static int mma8452_standby(struct mma8452_data *data)
> +{
> +	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
> +		data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
> +}
> +
> +static int mma8452_active(struct mma8452_data *data)
> +{
> +	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
> +		data->ctrl_reg1);
> +}
> +
> +static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
> +{
> +	int ret;
> +
> +	mutex_lock(&data->lock);
> +
> +	/* config can only be changed when in standby */
> +	ret = mma8452_standby(data);
> +	if (ret < 0)
> +		goto fail;
> +
> +	ret = i2c_smbus_write_byte_data(data->client, reg, val);
> +	if (ret < 0)
> +		goto fail;
> +
> +	ret = mma8452_active(data);
> +	if (ret < 0)
> +		goto fail;
> +
> +	ret = 0;
> +fail:
> +	mutex_unlock(&data->lock);
> +	return ret;
> +}
> +
> +static int mma8452_write_raw(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan,
> +			     int val, int val2, long mask)
> +{
> +	struct mma8452_data *data = iio_priv(indio_dev);
> +	int i;
> +
> +	if (iio_buffer_enabled(indio_dev))
> +		return -EBUSY;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		i = mma8452_get_samp_freq_index(data, val, val2);
> +		if (i < 0)
> +			return -EINVAL;
> +
> +		data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
> +		data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
> +		return mma8452_change_config(data, MMA8452_CTRL_REG1,
> +			data->ctrl_reg1);
> +	case IIO_CHAN_INFO_SCALE:
> +		i = mma8452_get_scale_index(data, val, val2);
> +		if (i < 0)
> +			return -EINVAL;
> +		data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
> +		data->data_cfg |= i;
> +		return mma8452_change_config(data, MMA8452_DATA_CFG,
> +			data->data_cfg);
> +	case IIO_CHAN_INFO_CALIBBIAS:
> +		if (val < -128 || val > 127)
> +			return -EINVAL;
> +		return mma8452_change_config(data, MMA8452_OFF_X +
> +			chan->scan_index, val);
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static irqreturn_t mma8452_trigger_handler(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct mma8452_data *data = iio_priv(indio_dev);
> +	u8 buffer[16]; /* 3 16-bit channels + padding + ts */
> +	int ret;
> +
> +	ret = mma8452_read(data, (__be16 *) buffer);
> +	if (ret < 0)
> +		goto done;
> +
> +	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
> +		iio_get_time_ns());
> +
> +done:
> +	iio_trigger_notify_done(indio_dev->trig);
> +	return IRQ_HANDLED;
> +}
> +
> +#define MMA8452_CHANNEL(axis, idx) { \
> +	.type = IIO_ACCEL, \
> +	.modified = 1, \
> +	.channel2 = IIO_MOD_##axis, \
> +	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
> +		BIT(IIO_CHAN_INFO_CALIBBIAS), \

Always an awkward interaction, but would the sampling frequency be
better as an attribute of a dataready based trigger rather than here?
I suppose we can have both with the two interacting though.

> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
> +		BIT(IIO_CHAN_INFO_SCALE), \
> +	.scan_index = idx, \
> +	.scan_type = { \
> +		.sign = 's', \
> +		.realbits = 12, \
> +		.storagebits = 16, \
> +		.shift = 4, \
> +		.endianness = IIO_BE, \
> +	}, \
> +}
> +
> +static const struct iio_chan_spec mma8452_channels[] = {
> +	MMA8452_CHANNEL(X, 0),
> +	MMA8452_CHANNEL(Y, 1),
> +	MMA8452_CHANNEL(Z, 2),
> +	IIO_CHAN_SOFT_TIMESTAMP(3),
> +};
> +
> +static struct attribute *mma8452_attributes[] = {
> +	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
> +	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
> +	NULL
> +};
> +
> +static const struct attribute_group mma8452_group = {
> +	.attrs = mma8452_attributes,
> +};
> +
> +static const struct iio_info mma8452_info = {
> +	.attrs = &mma8452_group,
> +	.read_raw = &mma8452_read_raw,
> +	.write_raw = &mma8452_write_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static const unsigned long mma8452_scan_masks[] = {0x7, 0};
> +
> +static int mma8452_probe(struct i2c_client *client,
> +			 const struct i2c_device_id *id)
> +{
> +	struct mma8452_data *data;
> +	struct iio_dev *indio_dev;
> +	int ret;
> +
> +	ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
> +	if (ret < 0)
> +		return ret;
> +	if (ret != MMA8452_DEVICE_ID)
> +		return -ENODEV;
> +
> +	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	data = iio_priv(indio_dev);
> +	data->client = client;
> +	mutex_init(&data->lock);
> +
> +	i2c_set_clientdata(client, indio_dev);
> +	indio_dev->info = &mma8452_info;
> +	indio_dev->name = id->name;
> +	indio_dev->dev.parent = &client->dev;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->channels = mma8452_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
> +	indio_dev->available_scan_masks = mma8452_scan_masks;
> +
> +	data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
> +		(MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
> +	ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
> +		data->ctrl_reg1);
> +	if (ret < 0)
> +		return ret;
> +
> +	data->data_cfg = MMA8452_DATA_CFG_FS_2G;
> +	ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
> +		data->data_cfg);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = iio_triggered_buffer_setup(indio_dev, NULL,
> +		mma8452_trigger_handler, NULL);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret < 0)
> +		goto buffer_cleanup;
> +	return 0;
> +
> +buffer_cleanup:
> +	iio_triggered_buffer_cleanup(indio_dev);
> +	return ret;
> +}
> +
> +static int mma8452_remove(struct i2c_client *client)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> +
> +	iio_device_unregister(indio_dev);
> +	iio_triggered_buffer_cleanup(indio_dev);
> +	mma8452_standby(iio_priv(indio_dev));
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int mma8452_suspend(struct device *dev)
> +{
> +	return mma8452_standby(iio_priv(i2c_get_clientdata(
> +		to_i2c_client(dev))));
> +}
> +
> +static int mma8452_resume(struct device *dev)
> +{
> +	return mma8452_active(iio_priv(i2c_get_clientdata(
> +		to_i2c_client(dev))));
> +}
> +
> +static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
> +#define MMA8452_PM_OPS (&mma8452_pm_ops)
> +#else
> +#define MMA8452_PM_OPS NULL
> +#endif
> +
> +static const struct i2c_device_id mma8452_id[] = {
> +	{ "mma8452", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, mma8452_id);
> +
> +static struct i2c_driver mma8452_driver = {
> +	.driver = {
> +		.name	= "mma8452",
> +		.pm	= MMA8452_PM_OPS,
> +	},
> +	.probe = mma8452_probe,
> +	.remove = mma8452_remove,
> +	.id_table = mma8452_id,
> +};
> +module_i2c_driver(mma8452_driver);
> +
> +MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
> +MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
> +MODULE_LICENSE("GPL");
>

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

* [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver
@ 2014-01-10 22:02 Peter Meerwald
  2014-01-11 16:57 ` Jonathan Cameron
  0 siblings, 1 reply; 15+ messages in thread
From: Peter Meerwald @ 2014-01-10 22:02 UTC (permalink / raw)
  To: linux-iio; +Cc: Peter Meerwald

3-axis accelerometer sensor (2/4/8 g) with 12-bit resolution
and I2C interface

many extra features are unsupported (freefall detection, orientation
change, autosleep)

datasheet is here:
http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf

Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
---
 drivers/iio/accel/Kconfig   |  12 ++
 drivers/iio/accel/Makefile  |   1 +
 drivers/iio/accel/mma8452.c | 438 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 451 insertions(+)
 create mode 100644 drivers/iio/accel/mma8452.c

diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index e23e508..1e120fa 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -65,4 +65,16 @@ config KXSD9
 	  Say yes here to build support for the Kionix KXSD9 accelerometer.
 	  Currently this only supports the device via an SPI interface.
 
+config MMA8452
+	tristate "Freescale MMA8452Q Accelerometer Driver"
+	depends on I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say yes here to build support for the Freescale MMA8452Q 3-axis
+	  accelerometer.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called mma8452.
+
 endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index c48d15f..dc0e379 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -6,6 +6,7 @@
 obj-$(CONFIG_BMA180) += bma180.o
 obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
 obj-$(CONFIG_KXSD9)	+= kxsd9.o
+obj-$(CONFIG_MMA8452)	+= mma8452.o
 
 obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
 st_accel-y := st_accel_core.o
diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
new file mode 100644
index 0000000..a45628c
--- /dev/null
+++ b/drivers/iio/accel/mma8452.c
@@ -0,0 +1,438 @@
+/*
+ * mma8452.c - Support for Freescale MMA8452Q 3-axis 12-bit accelerometer
+ *
+ * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
+ *
+ * This file is subject to the terms and conditions of version 2 of
+ * the GNU General Public License.  See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
+ *
+ * TODO: interrupt, thresholding, orientation / freefall events, autosleep
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/delay.h>
+
+#define MMA8452_STATUS 0x00
+#define MMA8452_OUT_X 0x01 /* MSB first, 12-bit  */
+#define MMA8452_OUT_Y 0x03
+#define MMA8452_OUT_Z 0x05
+#define MMA8452_WHO_AM_I 0x0d
+#define MMA8452_DATA_CFG 0x0e
+#define MMA8452_OFF_X 0x2f
+#define MMA8452_OFF_Y 0x30
+#define MMA8452_OFF_Z 0x31
+#define MMA8452_CTRL_REG1 0x2a
+#define MMA8452_CTRL_REG2 0x2b
+
+#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
+
+#define MMA8452_CTRL_DR_MASK (BIT(5) | BIT(4) | BIT(3))
+#define MMA8452_CTRL_DR_SHIFT 3
+#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
+#define MMA8452_CTRL_ACTIVE BIT(0)
+
+#define MMA8452_DATA_CFG_FS_MASK (BIT(1) | BIT(0))
+#define MMA8452_DATA_CFG_FS_2G 0
+#define MMA8452_DATA_CFG_FS_4G 1
+#define MMA8452_DATA_CFG_FS_8G 2
+
+#define MMA8452_DEVICE_ID 0x2a
+
+/* Each client has this additional data */
+struct mma8452_data {
+	struct i2c_client *client;
+	struct mutex lock;
+	u8 ctrl_reg1;
+	u8 data_cfg;
+};
+
+static int mma8452_drdy(struct mma8452_data *data)
+{
+	int tries = 150;
+
+	while (tries-- > 0) {
+		int ret = i2c_smbus_read_byte_data(data->client,
+			MMA8452_STATUS);
+		if (ret < 0)
+			return ret;
+		if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
+			return 0;
+		msleep(20);
+	}
+
+	dev_err(&data->client->dev, "data not ready\n");
+	return -EIO;
+}
+
+static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
+{
+	int ret = mma8452_drdy(data);
+	if (ret < 0)
+		return ret;
+	return i2c_smbus_read_i2c_block_data(data->client,
+		MMA8452_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
+}
+
+static ssize_t mma8452_show_int_plus_micros(char *buf,
+	const int (*vals)[2], int n)
+{
+	size_t len = 0;
+
+	while (n-- > 0)
+		len += scnprintf(buf + len, PAGE_SIZE - len,
+			"%d.%06d ", vals[n][0], vals[n][1]);
+
+	/* replace trailing space by newline */
+	buf[len - 1] = '\n';
+
+	return len;
+}
+
+static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
+					int val, int val2)
+{
+	while (n-- > 0)
+		if (val == vals[n][0] && val2 == vals[n][1])
+			return n;
+
+	return -EINVAL;
+}
+
+static const int mma8452_samp_freq[8][2] = {
+	{800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
+	{6, 250000}, {1, 560000}
+};
+
+static const int mma8452_scales[3][2] = {
+	{0, 977}, {0, 1953}, {0, 3906}
+};
+
+static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return mma8452_show_int_plus_micros(buf, mma8452_samp_freq, 8);
+}
+
+static ssize_t mma8452_show_scale_avail(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return mma8452_show_int_plus_micros(buf, mma8452_scales, 3);
+}
+
+static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
+static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
+	mma8452_show_scale_avail, NULL, 0);
+
+static int mma8452_get_samp_freq_index(struct mma8452_data *data,
+	int val, int val2)
+{
+	return mma8452_get_int_plus_micros_index(mma8452_samp_freq, 8, val,
+		val2);
+}
+
+static int mma8452_get_scale_index(struct mma8452_data *data,
+	int val, int val2)
+{
+	return mma8452_get_int_plus_micros_index(mma8452_scales, 3, val,
+		val2);
+}
+
+static int mma8452_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long mask)
+{
+	struct mma8452_data *data = iio_priv(indio_dev);
+	__be16 buffer[3];
+	int i, ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;
+
+		mutex_lock(&data->lock);
+		ret = mma8452_read(data, buffer);
+		mutex_unlock(&data->lock);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(
+			be16_to_cpu(buffer[chan->scan_index]) >> 4, 11);
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
+		*val = mma8452_scales[i][0];
+		*val2 = mma8452_scales[i][1];
+		return IIO_VAL_INT_PLUS_MICRO;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		i = (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
+			MMA8452_CTRL_DR_SHIFT;
+		*val = mma8452_samp_freq[i][0];
+		*val2 = mma8452_samp_freq[i][1];
+		return IIO_VAL_INT_PLUS_MICRO;
+	case IIO_CHAN_INFO_CALIBBIAS:
+		ret = i2c_smbus_read_byte_data(data->client, MMA8452_OFF_X +
+			chan->scan_index);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(ret, 7);
+		return IIO_VAL_INT;
+	}
+	return -EINVAL;
+}
+
+static int mma8452_standby(struct mma8452_data *data)
+{
+	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
+		data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
+}
+
+static int mma8452_active(struct mma8452_data *data)
+{
+	return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
+		data->ctrl_reg1);
+}
+
+static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
+{
+	int ret;
+
+	mutex_lock(&data->lock);
+
+	/* config can only be changed when in standby */
+	ret = mma8452_standby(data);
+	if (ret < 0)
+		goto fail;
+
+	ret = i2c_smbus_write_byte_data(data->client, reg, val);
+	if (ret < 0)
+		goto fail;
+
+	ret = mma8452_active(data);
+	if (ret < 0)
+		goto fail;
+
+	ret = 0;
+fail:
+	mutex_unlock(&data->lock);
+	return ret;
+}
+
+static int mma8452_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask)
+{
+	struct mma8452_data *data = iio_priv(indio_dev);
+	int i;
+
+	if (iio_buffer_enabled(indio_dev))
+		return -EBUSY;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		i = mma8452_get_samp_freq_index(data, val, val2);
+		if (i < 0)
+			return -EINVAL;
+
+		data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
+		data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
+		return mma8452_change_config(data, MMA8452_CTRL_REG1,
+			data->ctrl_reg1);
+	case IIO_CHAN_INFO_SCALE:
+		i = mma8452_get_scale_index(data, val, val2);
+		if (i < 0)
+			return -EINVAL;
+		data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
+		data->data_cfg |= i;
+		return mma8452_change_config(data, MMA8452_DATA_CFG,
+			data->data_cfg);
+	case IIO_CHAN_INFO_CALIBBIAS:
+		if (val < -128 || val > 127)
+			return -EINVAL;
+		return mma8452_change_config(data, MMA8452_OFF_X +
+			chan->scan_index, val);
+	default:
+		return -EINVAL;
+	}
+}
+
+static irqreturn_t mma8452_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct mma8452_data *data = iio_priv(indio_dev);
+	u8 buffer[16]; /* 3 16-bit channels + padding + ts */
+	int ret;
+
+	ret = mma8452_read(data, (__be16 *) buffer);
+	if (ret < 0)
+		goto done;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
+		iio_get_time_ns());
+
+done:
+	iio_trigger_notify_done(indio_dev->trig);
+	return IRQ_HANDLED;
+}
+
+#define MMA8452_CHANNEL(axis, idx) { \
+	.type = IIO_ACCEL, \
+	.modified = 1, \
+	.channel2 = IIO_MOD_##axis, \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
+		BIT(IIO_CHAN_INFO_CALIBBIAS), \
+	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
+		BIT(IIO_CHAN_INFO_SCALE), \
+	.scan_index = idx, \
+	.scan_type = { \
+		.sign = 's', \
+		.realbits = 12, \
+		.storagebits = 16, \
+		.shift = 4, \
+		.endianness = IIO_BE, \
+	}, \
+}
+
+static const struct iio_chan_spec mma8452_channels[] = {
+	MMA8452_CHANNEL(X, 0),
+	MMA8452_CHANNEL(Y, 1),
+	MMA8452_CHANNEL(Z, 2),
+	IIO_CHAN_SOFT_TIMESTAMP(3),
+};
+
+static struct attribute *mma8452_attributes[] = {
+	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
+	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
+	NULL
+};
+
+static const struct attribute_group mma8452_group = {
+	.attrs = mma8452_attributes,
+};
+
+static const struct iio_info mma8452_info = {
+	.attrs = &mma8452_group,
+	.read_raw = &mma8452_read_raw,
+	.write_raw = &mma8452_write_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static const unsigned long mma8452_scan_masks[] = {0x7, 0};
+
+static int mma8452_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct mma8452_data *data;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
+	if (ret < 0)
+		return ret;
+	if (ret != MMA8452_DEVICE_ID)
+		return -ENODEV;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	data->client = client;
+	mutex_init(&data->lock);
+
+	i2c_set_clientdata(client, indio_dev);
+	indio_dev->info = &mma8452_info;
+	indio_dev->name = id->name;
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = mma8452_channels;
+	indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
+	indio_dev->available_scan_masks = mma8452_scan_masks;
+
+	data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
+		(MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
+	ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
+		data->ctrl_reg1);
+	if (ret < 0)
+		return ret;
+
+	data->data_cfg = MMA8452_DATA_CFG_FS_2G;
+	ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
+		data->data_cfg);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_triggered_buffer_setup(indio_dev, NULL,
+		mma8452_trigger_handler, NULL);
+	if (ret < 0)
+		return ret;
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0)
+		goto buffer_cleanup;
+	return 0;
+
+buffer_cleanup:
+	iio_triggered_buffer_cleanup(indio_dev);
+	return ret;
+}
+
+static int mma8452_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+
+	iio_device_unregister(indio_dev);
+	iio_triggered_buffer_cleanup(indio_dev);
+	mma8452_standby(iio_priv(indio_dev));
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int mma8452_suspend(struct device *dev)
+{
+	return mma8452_standby(iio_priv(i2c_get_clientdata(
+		to_i2c_client(dev))));
+}
+
+static int mma8452_resume(struct device *dev)
+{
+	return mma8452_active(iio_priv(i2c_get_clientdata(
+		to_i2c_client(dev))));
+}
+
+static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
+#define MMA8452_PM_OPS (&mma8452_pm_ops)
+#else
+#define MMA8452_PM_OPS NULL
+#endif
+
+static const struct i2c_device_id mma8452_id[] = {
+	{ "mma8452", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, mma8452_id);
+
+static struct i2c_driver mma8452_driver = {
+	.driver = {
+		.name	= "mma8452",
+		.pm	= MMA8452_PM_OPS,
+	},
+	.probe = mma8452_probe,
+	.remove = mma8452_remove,
+	.id_table = mma8452_id,
+};
+module_i2c_driver(mma8452_driver);
+
+MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
+MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
+MODULE_LICENSE("GPL");
-- 
1.8.3.2


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

end of thread, other threads:[~2014-05-03 11:12 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-23 19:50 [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Srinivas Pandruvada
2014-02-23 19:50 ` [PATCH v1 2/5] IIO: core: Modify scan element type Srinivas Pandruvada
2014-02-23 19:50 ` [PATCH v1 3/5] IIO: core: Introduce Quaternion types Srinivas Pandruvada
2014-02-23 19:50 ` [PATCH v1 4/5] iio: hid-sensors: Added device rotation support Srinivas Pandruvada
2014-02-23 19:50 ` [PATCH v1 5/5] iio: Added ABI description for quaternion Srinivas Pandruvada
2014-03-29 11:15 ` [PATCH v1 1/5] IIO: core: Introduce read_raw_multi Jonathan Cameron
2014-04-04 21:38   ` Srinivas Pandruvada
2014-04-05 10:07     ` Jonathan Cameron
2014-04-07 16:05       ` [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver gmane
2014-04-07 16:26         ` Jonathan Cameron
2014-04-07 16:32           ` Peter Meerwald
  -- strict thread matches above, loose matches on Subject: below --
2014-05-02  8:51 [PATCH] iio: mma8452 driver Peter Meerwald
2014-05-02  8:51 ` [PATCH] iio: Add Freescale MMA8452Q 3-axis accelerometer driver Peter Meerwald
2014-05-03 11:13   ` Jonathan Cameron
2014-01-10 22:02 Peter Meerwald
2014-01-11 16:57 ` Jonathan Cameron

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.