Skip to contentSkip to conversation

iio: hid-sensors: Convert units and exponent

PATCH Applied Srinivas Pandruvada 2 participants 41 messages Apr 18, 2014 – May 5, 2014 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Srinivas Pandruvada

author maintainer
#0 lore raw

PATCH 1/16 iio: hid-sensors: Convert units and exponent

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

---
 .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 +++++++++++++++++++++
 include/linux/hid-sensor-hub.h                     |   4 +
 2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +114 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 
+struct {
+	u32 usage_id;
+	int unit; /* 0 for default others from HID sensor spec */
+	int scale_val0; /* scale, whole number */
+	int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+	{HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+	{HID_USAGE_SENSOR_ACCEL_3D,
+		HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+	{HID_USAGE_SENSOR_ACCEL_3D,
+		HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+	{HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+	{HID_USAGE_SENSOR_GYRO_3D,
+		HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+	{HID_USAGE_SENSOR_GYRO_3D,
+		HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+	{HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+	{HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+	{HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+	{HID_USAGE_SENSOR_INCLINOMETER_3D,
+		HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+	{HID_USAGE_SENSOR_INCLINOMETER_3D,
+		HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+	{HID_USAGE_SENSOR_ALS, 0, 1, 0},
+	{HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+	{HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+	{HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
 static int pow_10(unsigned power)
 {
 	int i;
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
 }
 EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.
+ * For example:
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+				  int scale1, int exp)
+{
+	int i;
+	int x;
+	int res;
+	int rem;
+
+	if (exp > 0) {
+		*val0 = scale0 * pow_10(exp);
+		res = 0;
+		if (exp > 6) {
+			*val1 = 0;
+			return;
+		}
+		for (i = 0; i < exp; ++i) {
+			x = scale1 / pow_10(5 - i);
+			res += (pow_10(exp - 1 - i) * x);
+			scale1 = scale1 % pow_10(5 - i);
+		}
+		*val0 += res;
+			*val1 = scale1 * pow_10(exp);
+	} else if (exp < 0) {
+		exp = abs(exp);
+		if (exp > 6) {
+			*val0 = *val1 = 0;
+			return;
+		}
+		*val0 = scale0 / pow_10(exp);
+		rem = scale0 % pow_10(exp);
+		res = 0;
+		for (i = 0; i < (6 - exp); ++i) {
+			x = scale1 / pow_10(5 - i);
+			res += (pow_10(5 - exp - i) * x);
+			scale1 = scale1 % pow_10(5 - i);
+		}
+		*val1 = rem * pow_10(6 - exp) + res;
+	} else {
+		*val0 = scale0;
+		*val1 = scale1;
+	}
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+			struct hid_sensor_hub_attribute_info *attr_info,
+			int *val0, int *val1)
+{
+	int i;
+	int exp;
+
+	*val0 = 1;
+	*val1 = 0;
+
+	for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+		if (unit_conversion[i].usage_id == usage_id &&
+			unit_conversion[i].unit == attr_info->units) {
+			exp  = hid_sensor_convert_exponent(
+						attr_info->unit_expo);
+			adjust_exponent_micro(val0, val1,
+					unit_conversion[i].scale_val0,
+					unit_conversion[i].scale_val1, exp);
+			break;
+		}
+	}
+
+	return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
 					u32 usage_id,
 					struct hid_sensor_common *st)
include/linux/hid-sensor-hub.h +4 -0
index b70cfd7..89626b2 100644
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
 				u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+			    struct hid_sensor_hub_attribute_info *attr_info,
+			    int *val0, int *val1);
+
 #endif
Signature · 1 line
1.7.11.7
author maintainer
#1 lore raw

PATCH 2/16 iio: hid-sensors: Add api to get poll value

Added interface to get poll value in milli-seconds. This value is changed by changing sampling frequency. This API allows clients to wait for atleast some poll milli seconds before reading a new sample.

---
 .../iio/common/hid-sensors/hid-sensor-attributes.c   | 20 ++++++++++++++++++++
 include/linux/hid-sensor-hub.h                       |  2 ++
 2 files changed, 22 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +20 -0
index 451a95b..4a533b8 100644
@@ -147,6 +147,26 @@ static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
 	return value;
 }
 
+s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
+{
+	s32 value = 0;
+	int ret;
+
+	ret = sensor_hub_get_feature(st->hsdev,
+		st->poll.report_id,
+		st->poll.index, &value);
+
+	if (ret < 0 || value < 0) {
+		return -EINVAL;
+	} else {
+		if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
+			value = value * 1000;
+	}
+
+	return value;
+}
+EXPORT_SYMBOL(hid_sensor_read_poll_value);
+
 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
 				int *val1, int *val2)
 {
include/linux/hid-sensor-hub.h +2 -0
index 89626b2..88d8d63 100644
@@ -227,4 +227,6 @@ int hid_sensor_format_scale(u32 usage_id,
 			    struct hid_sensor_hub_attribute_info *attr_info,
 			    int *val0, int *val1);
 
+s32 hid_sensor_read_poll_value(struct hid_sensor_common *st);
+
 #endif
Signature · 1 line
1.7.11.7
maintainer Applied
#2 lore raw

Re: [PATCH 02/16] iio: hid-sensors: Add api to get poll value

Quoted 5 lines · Srinivas Pandruvada

Added interface to get poll value in milli-seconds. This value is changed by changing sampling frequency. This API allows clients to wait for atleast some poll milli seconds before reading a new sample.

applied to the togreg branch of iio.git

Quoted 48 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c   | 20 ++++++++++++++++++++
  include/linux/hid-sensor-hub.h                       |  2 ++
  2 files changed, 22 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +20 -0
index 451a95b..4a533b8 100644
@@ -147,6 +147,26 @@ static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
  	return value;
  }
 
+s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
+{
+	s32 value = 0;
+	int ret;
+
+	ret = sensor_hub_get_feature(st->hsdev,
+		st->poll.report_id,
+		st->poll.index, &value);
+
+	if (ret < 0 || value < 0) {
+		return -EINVAL;
+	} else {
+		if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
+			value = value * 1000;
+	}
+
+	return value;
+}
+EXPORT_SYMBOL(hid_sensor_read_poll_value);
+
  int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  				int *val1, int *val2)
  {
include/linux/hid-sensor-hub.h +2 -0
index 89626b2..88d8d63 100644
@@ -227,4 +227,6 @@ int hid_sensor_format_scale(u32 usage_id,
  			    struct hid_sensor_hub_attribute_info *attr_info,
  			    int *val0, int *val1);
 
+s32 hid_sensor_read_poll_value(struct hid_sensor_common *st);
+
  #endif
author maintainer
#3 lore raw

PATCH 3/16 iio: hid-sensors: Accelerometer 3D: adjust scale and offset

In reply to Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

---
 drivers/iio/accel/hid-sensor-accel-3d.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/accel/hid-sensor-accel-3d.c +13 -4
index 3dcdbad..ca50a91 100644
@@ -42,6 +42,10 @@ struct accel_3d_state {
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
 	u32 accel_val[ACCEL_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
 };
 
 static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
@@ -123,12 +127,12 @@ static int accel_3d_read_raw(struct iio_dev *indio_dev,
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
-		*val = accel_state->accel[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = accel_state->scale_pre_decml;
+		*val2 = accel_state->scale_post_decml;
+		ret_type = accel_state->scale_precision;
 		break;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			accel_state->accel[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = accel_state->value_offset;
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -262,6 +266,11 @@ static int accel_3d_parse_report(struct platform_device *pdev,
 			st->accel[1].index, st->accel[1].report_id,
 			st->accel[2].index, st->accel[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_ACCEL_3D,
+				&st->accel[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
 	/* Set Sensitivity field ids, when there is no individual modifier */
 	if (st->common_attributes.sensitivity.index < 0) {
 		sensor_hub_input_get_attribute_info(hsdev,
Signature · 1 line
1.7.11.7
maintainer Applied
#4 lore raw

Re: [PATCH 03/16] iio: hid-sensors: Accelerometer 3D: adjust scale and offset

Quoted 4 lines · Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

Applied to the togreg branch of iio.git
Note I'll push this out as testing as normal, to allow the autobuilders to play.

Quoted 49 lines
---
  drivers/iio/accel/hid-sensor-accel-3d.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/accel/hid-sensor-accel-3d.c +13 -4
index 3dcdbad..ca50a91 100644
@@ -42,6 +42,10 @@ struct accel_3d_state {
  	struct hid_sensor_common common_attributes;
  	struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
  	u32 accel_val[ACCEL_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
  };
 
  static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
@@ -123,12 +127,12 @@ static int accel_3d_read_raw(struct iio_dev *indio_dev,
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
-		*val = accel_state->accel[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = accel_state->scale_pre_decml;
+		*val2 = accel_state->scale_post_decml;
+		ret_type = accel_state->scale_precision;
  		break;
  	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			accel_state->accel[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = accel_state->value_offset;
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -262,6 +266,11 @@ static int accel_3d_parse_report(struct platform_device *pdev,
  			st->accel[1].index, st->accel[1].report_id,
  			st->accel[2].index, st->accel[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_ACCEL_3D,
+				&st->accel[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
  	/* Set Sensitivity field ids, when there is no individual modifier */
  	if (st->common_attributes.sensitivity.index < 0) {
  		sensor_hub_input_get_attribute_info(hsdev,
author maintainer
#5 lore raw

PATCH 4/16 iio: hid-sensors: Gyro 3D : adjust scale and offset

In reply to Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

---
 drivers/iio/gyro/hid-sensor-gyro-3d.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/gyro/hid-sensor-gyro-3d.c +13 -4
index 59d6bc3..53ac060 100644
@@ -42,6 +42,10 @@ struct gyro_3d_state {
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
 	u32 gyro_val[GYRO_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
 };
 
 static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
@@ -123,12 +127,12 @@ static int gyro_3d_read_raw(struct iio_dev *indio_dev,
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
-		*val = gyro_state->gyro[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = gyro_state->scale_pre_decml;
+		*val2 = gyro_state->scale_post_decml;
+		ret_type = gyro_state->scale_precision;
 		break;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			gyro_state->gyro[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = gyro_state->value_offset;
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -262,6 +266,11 @@ static int gyro_3d_parse_report(struct platform_device *pdev,
 			st->gyro[1].index, st->gyro[1].report_id,
 			st->gyro[2].index, st->gyro[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_GYRO_3D,
+				&st->gyro[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
 	/* Set Sensitivity field ids, when there is no individual modifier */
 	if (st->common_attributes.sensitivity.index < 0) {
 		sensor_hub_input_get_attribute_info(hsdev,
Signature · 1 line
1.7.11.7
maintainer Applied
#6 lore raw

Re: [PATCH 04/16] iio: hid-sensors: Gyro 3D : adjust scale and offset

Quoted 4 lines · Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

Applied to the togreg branch of iio.git

Thanks,

Quoted 49 lines
---
  drivers/iio/gyro/hid-sensor-gyro-3d.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/gyro/hid-sensor-gyro-3d.c +13 -4
index 59d6bc3..53ac060 100644
@@ -42,6 +42,10 @@ struct gyro_3d_state {
  	struct hid_sensor_common common_attributes;
  	struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
  	u32 gyro_val[GYRO_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
  };
 
  static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
@@ -123,12 +127,12 @@ static int gyro_3d_read_raw(struct iio_dev *indio_dev,
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
-		*val = gyro_state->gyro[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = gyro_state->scale_pre_decml;
+		*val2 = gyro_state->scale_post_decml;
+		ret_type = gyro_state->scale_precision;
  		break;
  	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			gyro_state->gyro[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = gyro_state->value_offset;
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -262,6 +266,11 @@ static int gyro_3d_parse_report(struct platform_device *pdev,
  			st->gyro[1].index, st->gyro[1].report_id,
  			st->gyro[2].index, st->gyro[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_GYRO_3D,
+				&st->gyro[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
  	/* Set Sensitivity field ids, when there is no individual modifier */
  	if (st->common_attributes.sensitivity.index < 0) {
  		sensor_hub_input_get_attribute_info(hsdev,
author maintainer
#7 lore raw

PATCH 5/16 iio: hid-sensors: ALS: adjust scale and offset

In reply to Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

---
 drivers/iio/light/hid-sensor-als.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/light/hid-sensor-als.c +13 -4
index 621541f..39b50be 100644
@@ -37,6 +37,10 @@ struct als_state {
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info als_illum;
 	u32 illum;
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
 };
 
 /* Channel definitions */
@@ -102,12 +106,12 @@ static int als_read_raw(struct iio_dev *indio_dev,
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
-		*val = als_state->als_illum.units;
-		ret_type = IIO_VAL_INT;
+		*val = als_state->scale_pre_decml;
+		*val2 = als_state->scale_post_decml;
+		ret_type = als_state->scale_precision;
 		break;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-				als_state->als_illum.unit_expo);
+		*val = als_state->value_offset;
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -229,6 +233,11 @@ static int als_parse_report(struct platform_device *pdev,
 	dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
 			st->als_illum.report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_ALS,
+				&st->als_illum,
+				&st->scale_pre_decml, &st->scale_post_decml);
+
 	/* Set Sensitivity field ids, when there is no individual modifier */
 	if (st->common_attributes.sensitivity.index < 0) {
 		sensor_hub_input_get_attribute_info(hsdev,
Signature · 1 line
1.7.11.7
maintainer Applied
#8 lore raw

Re: [PATCH 05/16] iio: hid-sensors: ALS: adjust scale and offset

Quoted 4 lines · Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

Applied to the togreg branch of iio.git

Thanks,

Quoted 49 lines
---
  drivers/iio/light/hid-sensor-als.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/light/hid-sensor-als.c +13 -4
index 621541f..39b50be 100644
@@ -37,6 +37,10 @@ struct als_state {
  	struct hid_sensor_common common_attributes;
  	struct hid_sensor_hub_attribute_info als_illum;
  	u32 illum;
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
  };
 
  /* Channel definitions */
@@ -102,12 +106,12 @@ static int als_read_raw(struct iio_dev *indio_dev,
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
-		*val = als_state->als_illum.units;
-		ret_type = IIO_VAL_INT;
+		*val = als_state->scale_pre_decml;
+		*val2 = als_state->scale_post_decml;
+		ret_type = als_state->scale_precision;
  		break;
  	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-				als_state->als_illum.unit_expo);
+		*val = als_state->value_offset;
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -229,6 +233,11 @@ static int als_parse_report(struct platform_device *pdev,
  	dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
  			st->als_illum.report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_ALS,
+				&st->als_illum,
+				&st->scale_pre_decml, &st->scale_post_decml);
+
  	/* Set Sensitivity field ids, when there is no individual modifier */
  	if (st->common_attributes.sensitivity.index < 0) {
  		sensor_hub_input_get_attribute_info(hsdev,
author maintainer
#9 lore raw

PATCH 6/16 iio: hid-sensors: Compass 3D: adjust scale and offset

In reply to Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

---
 drivers/iio/magnetometer/hid-sensor-magn-3d.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/magnetometer/hid-sensor-magn-3d.c +13 -4
index 6d162b7..131ced0 100644
@@ -42,6 +42,10 @@ struct magn_3d_state {
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
 	u32 magn_val[MAGN_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
 };
 
 static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
@@ -124,12 +128,12 @@ static int magn_3d_read_raw(struct iio_dev *indio_dev,
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
-		*val = magn_state->magn[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = magn_state->scale_pre_decml;
+		*val2 = magn_state->scale_post_decml;
+		ret_type = magn_state->scale_precision;
 		break;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			magn_state->magn[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = magn_state->value_offset;
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -263,6 +267,11 @@ static int magn_3d_parse_report(struct platform_device *pdev,
 			st->magn[1].index, st->magn[1].report_id,
 			st->magn[2].index, st->magn[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_COMPASS_3D,
+				&st->magn[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
 	/* Set Sensitivity field ids, when there is no individual modifier */
 	if (st->common_attributes.sensitivity.index < 0) {
 		sensor_hub_input_get_attribute_info(hsdev,
Signature · 1 line
1.7.11.7
maintainer Applied
#10 lore raw

Re: [PATCH 06/16] iio: hid-sensors: Compass 3D: adjust scale and offset

Quoted 4 lines · Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

Applied to the togreg branch of iio.git

Thanks,

Quoted 49 lines
---
  drivers/iio/magnetometer/hid-sensor-magn-3d.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/magnetometer/hid-sensor-magn-3d.c +13 -4
index 6d162b7..131ced0 100644
@@ -42,6 +42,10 @@ struct magn_3d_state {
  	struct hid_sensor_common common_attributes;
  	struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
  	u32 magn_val[MAGN_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
  };
 
  static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
@@ -124,12 +128,12 @@ static int magn_3d_read_raw(struct iio_dev *indio_dev,
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
-		*val = magn_state->magn[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = magn_state->scale_pre_decml;
+		*val2 = magn_state->scale_post_decml;
+		ret_type = magn_state->scale_precision;
  		break;
  	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			magn_state->magn[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = magn_state->value_offset;
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -263,6 +267,11 @@ static int magn_3d_parse_report(struct platform_device *pdev,
  			st->magn[1].index, st->magn[1].report_id,
  			st->magn[2].index, st->magn[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_COMPASS_3D,
+				&st->magn[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
  	/* Set Sensitivity field ids, when there is no individual modifier */
  	if (st->common_attributes.sensitivity.index < 0) {
  		sensor_hub_input_get_attribute_info(hsdev,
author maintainer
#11 lore raw

PATCH 7/16 iio: hid-sensors: Inclinometer 3D: adjust scale and offset

In reply to Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

---
 drivers/iio/orientation/hid-sensor-incl-3d.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/orientation/hid-sensor-incl-3d.c +13 -4
index 070feab..f0c465c 100644
@@ -42,6 +42,10 @@ struct incl_3d_state {
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX];
 	u32 incl_val[INCLI_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
 };
 
 static const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = {
@@ -125,12 +129,12 @@ static int incl_3d_read_raw(struct iio_dev *indio_dev,
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
-		*val = incl_state->incl[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = incl_state->scale_pre_decml;
+		*val2 = incl_state->scale_post_decml;
+		ret_type = incl_state->scale_precision;
 		break;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			incl_state->incl[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = incl_state->value_offset;
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -279,6 +283,11 @@ static int incl_3d_parse_report(struct platform_device *pdev,
 			st->incl[1].index, st->incl[1].report_id,
 			st->incl[2].index, st->incl[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_INCLINOMETER_3D,
+				&st->incl[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
 	/* Set Sensitivity field ids, when there is no individual modifier */
 	if (st->common_attributes.sensitivity.index < 0) {
 		sensor_hub_input_get_attribute_info(hsdev,
Signature · 1 line
1.7.11.7
maintainer Applied
#12 lore raw

Re: [PATCH 07/16] iio: hid-sensors: Inclinometer 3D: adjust scale and offset

Quoted 4 lines · Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

Applied to the togreg branch of iio.git

Thanks,

Quoted 49 lines
---
  drivers/iio/orientation/hid-sensor-incl-3d.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/orientation/hid-sensor-incl-3d.c +13 -4
index 070feab..f0c465c 100644
@@ -42,6 +42,10 @@ struct incl_3d_state {
  	struct hid_sensor_common common_attributes;
  	struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX];
  	u32 incl_val[INCLI_3D_CHANNEL_MAX];
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
  };
 
  static const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = {
@@ -125,12 +129,12 @@ static int incl_3d_read_raw(struct iio_dev *indio_dev,
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
-		*val = incl_state->incl[CHANNEL_SCAN_INDEX_X].units;
-		ret_type = IIO_VAL_INT;
+		*val = incl_state->scale_pre_decml;
+		*val2 = incl_state->scale_post_decml;
+		ret_type = incl_state->scale_precision;
  		break;
  	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-			incl_state->incl[CHANNEL_SCAN_INDEX_X].unit_expo);
+		*val = incl_state->value_offset;
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -279,6 +283,11 @@ static int incl_3d_parse_report(struct platform_device *pdev,
  			st->incl[1].index, st->incl[1].report_id,
  			st->incl[2].index, st->incl[2].report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_INCLINOMETER_3D,
+				&st->incl[CHANNEL_SCAN_INDEX_X],
+				&st->scale_pre_decml, &st->scale_post_decml);
+
  	/* Set Sensitivity field ids, when there is no individual modifier */
  	if (st->common_attributes.sensitivity.index < 0) {
  		sensor_hub_input_get_attribute_info(hsdev,
author maintainer
#13 lore raw

PATCH 8/16 iio: hid-sensors: Pressure: adjust scale and offset

In reply to Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

---
 drivers/iio/pressure/hid-sensor-press.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/pressure/hid-sensor-press.c +13 -4
index e0e6409..ff69da4 100644
@@ -36,6 +36,10 @@ struct press_state {
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info press_attr;
 	u32 press_data;
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
 };
 
 /* Channel definitions */
@@ -102,12 +106,12 @@ static int press_read_raw(struct iio_dev *indio_dev,
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
-		*val = press_state->press_attr.units;
-		ret_type = IIO_VAL_INT;
+		*val = press_state->scale_pre_decml;
+		*val2 = press_state->scale_post_decml;
+		ret_type = press_state->scale_precision;
 		break;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-				press_state->press_attr.unit_expo);
+		*val = press_state->value_offset;
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -229,6 +233,11 @@ static int press_parse_report(struct platform_device *pdev,
 	dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
 			st->press_attr.report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_PRESSURE,
+				&st->press_attr,
+				&st->scale_pre_decml, &st->scale_post_decml);
+
 	/* Set Sensitivity field ids, when there is no individual modifier */
 	if (st->common_attributes.sensitivity.index < 0) {
 		sensor_hub_input_get_attribute_info(hsdev,
Signature · 1 line
1.7.11.7
maintainer Applied
#14 lore raw

Re: [PATCH 08/16] iio: hid-sensors: Pressure: adjust scale and offset

Quoted 4 lines · Srinivas Pandruvada

Using units and unit exponent to calculate scale which is compliant to IIO ABI.

Applied

Quoted 49 lines
---
  drivers/iio/pressure/hid-sensor-press.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/pressure/hid-sensor-press.c +13 -4
index e0e6409..ff69da4 100644
@@ -36,6 +36,10 @@ struct press_state {
  	struct hid_sensor_common common_attributes;
  	struct hid_sensor_hub_attribute_info press_attr;
  	u32 press_data;
+	int scale_pre_decml;
+	int scale_post_decml;
+	int scale_precision;
+	int value_offset;
  };
 
  /* Channel definitions */
@@ -102,12 +106,12 @@ static int press_read_raw(struct iio_dev *indio_dev,
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
-		*val = press_state->press_attr.units;
-		ret_type = IIO_VAL_INT;
+		*val = press_state->scale_pre_decml;
+		*val2 = press_state->scale_post_decml;
+		ret_type = press_state->scale_precision;
  		break;
  	case IIO_CHAN_INFO_OFFSET:
-		*val = hid_sensor_convert_exponent(
-				press_state->press_attr.unit_expo);
+		*val = press_state->value_offset;
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -229,6 +233,11 @@ static int press_parse_report(struct platform_device *pdev,
  	dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
  			st->press_attr.report_id);
 
+	st->scale_precision = hid_sensor_format_scale(
+				HID_USAGE_SENSOR_PRESSURE,
+				&st->press_attr,
+				&st->scale_pre_decml, &st->scale_post_decml);
+
  	/* Set Sensitivity field ids, when there is no individual modifier */
  	if (st->common_attributes.sensitivity.index < 0) {
  		sensor_hub_input_get_attribute_info(hsdev,
author maintainer
#15 lore raw

PATCH 9/16 iio: hid-sensors: Add API to power on/off

In reply to Srinivas Pandruvada

Added an API to allow client drivers to turn ON and OFF sensors for quick read. Added data_read as counting varaible instead of boolean, so that sensor is powered off only when last user released it.

---
 drivers/iio/accel/hid-sensor-accel-3d.c             |  7 +++----
 drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 17 +++++++++++++----
 drivers/iio/common/hid-sensors/hid-sensor-trigger.h |  1 +
 drivers/iio/gyro/hid-sensor-gyro-3d.c               |  7 +++----
 drivers/iio/light/hid-sensor-als.c                  |  7 +++----
 drivers/iio/light/hid-sensor-prox.c                 |  7 +++----
 drivers/iio/magnetometer/hid-sensor-magn-3d.c       |  7 +++----
 drivers/iio/orientation/hid-sensor-incl-3d.c        |  7 +++----
 drivers/iio/orientation/hid-sensor-rotation.c       |  8 +++-----
 drivers/iio/pressure/hid-sensor-press.c             |  7 +++----
 include/linux/hid-sensor-hub.h                      |  2 +-
 11 files changed, 39 insertions(+), 38 deletions(-)
drivers/iio/accel/hid-sensor-accel-3d.c +3 -4
index ca50a91..cf61c87 100644
@@ -201,9 +201,8 @@ static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "accel_3d_proc_event [%d]\n",
-				accel_state->common_attributes.data_ready);
-	if (accel_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
+	if (atomic_read(&accel_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				accel_state->accel_val,
 				sizeof(accel_state->accel_val));
@@ -342,7 +341,7 @@ static int hid_accel_3d_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		goto error_free_dev_mem;
 	}
-	accel_state->common_attributes.data_ready = false;
+	atomic_set(&accel_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 					&accel_state->common_attributes);
 	if (ret < 0) {
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +13 -4
index dbefbda..73282ce 100644
@@ -28,16 +28,17 @@
 #include <linux/iio/sysfs.h>
 #include "hid-sensor-trigger.h"
 
-static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
-						bool state)
+int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 {
-	struct hid_sensor_common *st = iio_trigger_get_drvdata(trig);
 	int state_val;
 	int report_val;
 
 	if (state) {
 		if (sensor_hub_device_open(st->hsdev))
 			return -EIO;
+
+		atomic_inc(&st->data_ready);
+
 		state_val = hid_sensor_get_usage_index(st->hsdev,
 			st->power_state.report_id,
 			st->power_state.index,
@@ -47,6 +48,8 @@ static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
 			st->report_state.index,
 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
 	} else {
+		if (!atomic_dec_and_test(&st->data_ready))
+			return 0;
 		sensor_hub_device_close(st->hsdev);
 		state_val = hid_sensor_get_usage_index(st->hsdev,
 			st->power_state.report_id,
@@ -57,7 +60,6 @@ static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
 			st->report_state.index,
 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
 	}
-	st->data_ready = state;
 
 	if (state_val >= 0) {
 		state_val += st->power_state.logical_minimum;
@@ -75,6 +77,13 @@ static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
 
 	return 0;
 }
+EXPORT_SYMBOL(hid_sensor_power_state);
+
+static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
+						bool state)
+{
+	return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
+}
 
 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
 {
drivers/iio/common/hid-sensors/hid-sensor-trigger.h +1 -0
index ca02f78..0f8e78c 100644
@@ -22,5 +22,6 @@
 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
 				struct hid_sensor_common *attrb);
 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb);
+int hid_sensor_power_state(struct hid_sensor_common *st, bool state);
 
 #endif
drivers/iio/gyro/hid-sensor-gyro-3d.c +3 -4
index 53ac060..392c30b 100644
@@ -201,9 +201,8 @@ static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "gyro_3d_proc_event [%d]\n",
-				gyro_state->common_attributes.data_ready);
-	if (gyro_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
+	if (atomic_read(&gyro_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				gyro_state->gyro_val,
 				sizeof(gyro_state->gyro_val));
@@ -339,7 +338,7 @@ static int hid_gyro_3d_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		goto error_free_dev_mem;
 	}
-	gyro_state->common_attributes.data_ready = false;
+	atomic_set(&gyro_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 					&gyro_state->common_attributes);
 	if (ret < 0) {
drivers/iio/light/hid-sensor-als.c +3 -4
index 39b50be..e124b39 100644
@@ -180,9 +180,8 @@ static int als_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct als_state *als_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "als_proc_event [%d]\n",
-				als_state->common_attributes.data_ready);
-	if (als_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "als_proc_event\n");
+	if (atomic_read(&als_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				&als_state->illum,
 				sizeof(als_state->illum));
@@ -305,7 +304,7 @@ static int hid_als_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		goto error_free_dev_mem;
 	}
-	als_state->common_attributes.data_ready = false;
+	atomic_set(&als_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 				&als_state->common_attributes);
 	if (ret < 0) {
drivers/iio/light/hid-sensor-prox.c +3 -4
index 1894ab1..07e98ec8 100644
@@ -176,9 +176,8 @@ static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct prox_state *prox_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "prox_proc_event [%d]\n",
-				prox_state->common_attributes.data_ready);
-	if (prox_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "prox_proc_event\n");
+	if (atomic_read(&prox_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				&prox_state->human_presence,
 				sizeof(prox_state->human_presence));
@@ -297,7 +296,7 @@ static int hid_prox_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		goto error_free_dev_mem;
 	}
-	prox_state->common_attributes.data_ready = false;
+	atomic_set(&prox_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 				&prox_state->common_attributes);
 	if (ret) {
drivers/iio/magnetometer/hid-sensor-magn-3d.c +3 -4
index 131ced0..54eea6a 100644
@@ -202,9 +202,8 @@ static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "magn_3d_proc_event [%d]\n",
-				magn_state->common_attributes.data_ready);
-	if (magn_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n");
+	if (atomic_read(&magn_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				magn_state->magn_val,
 				sizeof(magn_state->magn_val));
@@ -343,7 +342,7 @@ static int hid_magn_3d_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		goto error_free_dev_mem;
 	}
-	magn_state->common_attributes.data_ready = false;
+	atomic_set(&magn_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 					&magn_state->common_attributes);
 	if (ret < 0) {
drivers/iio/orientation/hid-sensor-incl-3d.c +3 -4
index f0c465c..bf11678 100644
@@ -200,9 +200,8 @@ static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct incl_3d_state *incl_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "incl_3d_proc_event [%d]\n",
-				incl_state->common_attributes.data_ready);
-	if (incl_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "incl_3d_proc_event\n");
+	if (atomic_read(&incl_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				(u8 *)incl_state->incl_val,
 				sizeof(incl_state->incl_val));
@@ -358,7 +357,7 @@ static int hid_incl_3d_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		goto error_free_dev_mem;
 	}
-	incl_state->common_attributes.data_ready = false;
+	atomic_set(&incl_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 					&incl_state->common_attributes);
 	if (ret) {
drivers/iio/orientation/hid-sensor-rotation.c +3 -5
index 51387bb..dccf848 100644
@@ -145,10 +145,8 @@ static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
 	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)
+	dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
+	if (atomic_read(&rot_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				(u8 *)rot_state->sampled_vals,
 				sizeof(rot_state->sampled_vals));
@@ -272,7 +270,7 @@ static int hid_dev_rot_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		return ret;
 	}
-	rot_state->common_attributes.data_ready = false;
+	atomic_set(&rot_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 					&rot_state->common_attributes);
 	if (ret) {
drivers/iio/pressure/hid-sensor-press.c +3 -4
index ff69da4..39df50c 100644
@@ -180,9 +180,8 @@ static int press_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct press_state *press_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "press_proc_event [%d]\n",
-				press_state->common_attributes.data_ready);
-	if (press_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "press_proc_event\n");
+	if (atomic_read(&press_state->common_attributes.data_ready))
 		hid_sensor_push_data(indio_dev,
 				&press_state->press_data,
 				sizeof(press_state->press_data));
@@ -307,7 +306,7 @@ static int hid_press_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 		goto error_free_dev_mem;
 	}
-	press_state->common_attributes.data_ready = false;
+	atomic_set(&press_state->common_attributes.data_ready, 0);
 	ret = hid_sensor_setup_trigger(indio_dev, name,
 				&press_state->common_attributes);
 	if (ret) {
include/linux/hid-sensor-hub.h +1 -1
index 88d8d63..51f7cca 100644
@@ -189,7 +189,7 @@ struct hid_sensor_common {
 	struct hid_sensor_hub_device *hsdev;
 	struct platform_device *pdev;
 	unsigned usage_id;
-	bool data_ready;
+	atomic_t data_ready;
 	struct iio_trigger *trigger;
 	struct hid_sensor_hub_attribute_info poll;
 	struct hid_sensor_hub_attribute_info report_state;
Signature · 1 line
1.7.11.7
maintainer Applied
#16 lore raw

Re: [PATCH 09/16] iio: hid-sensors: Add API to power on/off

Quoted 5 lines · Srinivas Pandruvada

Added an API to allow client drivers to turn ON and OFF sensors for quick read. Added data_read as counting varaible instead of boolean, so that sensor is powered off only when last user released it.

Applied to the togreg branch of iio.git

Thanks,

Quoted 296 lines
---
  drivers/iio/accel/hid-sensor-accel-3d.c             |  7 +++----
  drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 17 +++++++++++++----
  drivers/iio/common/hid-sensors/hid-sensor-trigger.h |  1 +
  drivers/iio/gyro/hid-sensor-gyro-3d.c               |  7 +++----
  drivers/iio/light/hid-sensor-als.c                  |  7 +++----
  drivers/iio/light/hid-sensor-prox.c                 |  7 +++----
  drivers/iio/magnetometer/hid-sensor-magn-3d.c       |  7 +++----
  drivers/iio/orientation/hid-sensor-incl-3d.c        |  7 +++----
  drivers/iio/orientation/hid-sensor-rotation.c       |  8 +++-----
  drivers/iio/pressure/hid-sensor-press.c             |  7 +++----
  include/linux/hid-sensor-hub.h                      |  2 +-
  11 files changed, 39 insertions(+), 38 deletions(-)
drivers/iio/accel/hid-sensor-accel-3d.c +3 -4
index ca50a91..cf61c87 100644
@@ -201,9 +201,8 @@ static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
  	struct accel_3d_state *accel_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "accel_3d_proc_event [%d]\n",
-				accel_state->common_attributes.data_ready);
-	if (accel_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
+	if (atomic_read(&accel_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				accel_state->accel_val,
  				sizeof(accel_state->accel_val));
@@ -342,7 +341,7 @@ static int hid_accel_3d_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		goto error_free_dev_mem;
  	}
-	accel_state->common_attributes.data_ready = false;
+	atomic_set(&accel_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  					&accel_state->common_attributes);
  	if (ret < 0) {
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +13 -4
index dbefbda..73282ce 100644
@@ -28,16 +28,17 @@
  #include <linux/iio/sysfs.h>
  #include "hid-sensor-trigger.h"
 
-static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
-						bool state)
+int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
  {
-	struct hid_sensor_common *st = iio_trigger_get_drvdata(trig);
  	int state_val;
  	int report_val;
 
  	if (state) {
  		if (sensor_hub_device_open(st->hsdev))
  			return -EIO;
+
+		atomic_inc(&st->data_ready);
+
  		state_val = hid_sensor_get_usage_index(st->hsdev,
  			st->power_state.report_id,
  			st->power_state.index,
@@ -47,6 +48,8 @@ static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
  			st->report_state.index,
  			HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
  	} else {
+		if (!atomic_dec_and_test(&st->data_ready))
+			return 0;
  		sensor_hub_device_close(st->hsdev);
  		state_val = hid_sensor_get_usage_index(st->hsdev,
  			st->power_state.report_id,
@@ -57,7 +60,6 @@ static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
  			st->report_state.index,
  			HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
  	}
-	st->data_ready = state;
 
  	if (state_val >= 0) {
  		state_val += st->power_state.logical_minimum;
@@ -75,6 +77,13 @@ static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
 
  	return 0;
  }
+EXPORT_SYMBOL(hid_sensor_power_state);
+
+static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
+						bool state)
+{
+	return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
+}
 
  void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
  {
drivers/iio/common/hid-sensors/hid-sensor-trigger.h +1 -0
index ca02f78..0f8e78c 100644
@@ -22,5 +22,6 @@
  int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
  				struct hid_sensor_common *attrb);
  void hid_sensor_remove_trigger(struct hid_sensor_common *attrb);
+int hid_sensor_power_state(struct hid_sensor_common *st, bool state);
 
  #endif
drivers/iio/gyro/hid-sensor-gyro-3d.c +3 -4
index 53ac060..392c30b 100644
@@ -201,9 +201,8 @@ static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
  	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "gyro_3d_proc_event [%d]\n",
-				gyro_state->common_attributes.data_ready);
-	if (gyro_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
+	if (atomic_read(&gyro_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				gyro_state->gyro_val,
  				sizeof(gyro_state->gyro_val));
@@ -339,7 +338,7 @@ static int hid_gyro_3d_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		goto error_free_dev_mem;
  	}
-	gyro_state->common_attributes.data_ready = false;
+	atomic_set(&gyro_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  					&gyro_state->common_attributes);
  	if (ret < 0) {
drivers/iio/light/hid-sensor-als.c +3 -4
index 39b50be..e124b39 100644
@@ -180,9 +180,8 @@ static int als_proc_event(struct hid_sensor_hub_device *hsdev,
  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
  	struct als_state *als_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "als_proc_event [%d]\n",
-				als_state->common_attributes.data_ready);
-	if (als_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "als_proc_event\n");
+	if (atomic_read(&als_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				&als_state->illum,
  				sizeof(als_state->illum));
@@ -305,7 +304,7 @@ static int hid_als_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		goto error_free_dev_mem;
  	}
-	als_state->common_attributes.data_ready = false;
+	atomic_set(&als_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  				&als_state->common_attributes);
  	if (ret < 0) {
drivers/iio/light/hid-sensor-prox.c +3 -4
index 1894ab1..07e98ec8 100644
@@ -176,9 +176,8 @@ static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
  	struct prox_state *prox_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "prox_proc_event [%d]\n",
-				prox_state->common_attributes.data_ready);
-	if (prox_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "prox_proc_event\n");
+	if (atomic_read(&prox_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				&prox_state->human_presence,
  				sizeof(prox_state->human_presence));
@@ -297,7 +296,7 @@ static int hid_prox_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		goto error_free_dev_mem;
  	}
-	prox_state->common_attributes.data_ready = false;
+	atomic_set(&prox_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  				&prox_state->common_attributes);
  	if (ret) {
drivers/iio/magnetometer/hid-sensor-magn-3d.c +3 -4
index 131ced0..54eea6a 100644
@@ -202,9 +202,8 @@ static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
  	struct magn_3d_state *magn_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "magn_3d_proc_event [%d]\n",
-				magn_state->common_attributes.data_ready);
-	if (magn_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n");
+	if (atomic_read(&magn_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				magn_state->magn_val,
  				sizeof(magn_state->magn_val));
@@ -343,7 +342,7 @@ static int hid_magn_3d_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		goto error_free_dev_mem;
  	}
-	magn_state->common_attributes.data_ready = false;
+	atomic_set(&magn_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  					&magn_state->common_attributes);
  	if (ret < 0) {
drivers/iio/orientation/hid-sensor-incl-3d.c +3 -4
index f0c465c..bf11678 100644
@@ -200,9 +200,8 @@ static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
  	struct incl_3d_state *incl_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "incl_3d_proc_event [%d]\n",
-				incl_state->common_attributes.data_ready);
-	if (incl_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "incl_3d_proc_event\n");
+	if (atomic_read(&incl_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				(u8 *)incl_state->incl_val,
  				sizeof(incl_state->incl_val));
@@ -358,7 +357,7 @@ static int hid_incl_3d_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		goto error_free_dev_mem;
  	}
-	incl_state->common_attributes.data_ready = false;
+	atomic_set(&incl_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  					&incl_state->common_attributes);
  	if (ret) {
drivers/iio/orientation/hid-sensor-rotation.c +3 -5
index 51387bb..dccf848 100644
@@ -145,10 +145,8 @@ static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
  	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)
+	dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
+	if (atomic_read(&rot_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				(u8 *)rot_state->sampled_vals,
  				sizeof(rot_state->sampled_vals));
@@ -272,7 +270,7 @@ static int hid_dev_rot_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		return ret;
  	}
-	rot_state->common_attributes.data_ready = false;
+	atomic_set(&rot_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  					&rot_state->common_attributes);
  	if (ret) {
drivers/iio/pressure/hid-sensor-press.c +3 -4
index ff69da4..39df50c 100644
@@ -180,9 +180,8 @@ static int press_proc_event(struct hid_sensor_hub_device *hsdev,
  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
  	struct press_state *press_state = iio_priv(indio_dev);
 
-	dev_dbg(&indio_dev->dev, "press_proc_event [%d]\n",
-				press_state->common_attributes.data_ready);
-	if (press_state->common_attributes.data_ready)
+	dev_dbg(&indio_dev->dev, "press_proc_event\n");
+	if (atomic_read(&press_state->common_attributes.data_ready))
  		hid_sensor_push_data(indio_dev,
  				&press_state->press_data,
  				sizeof(press_state->press_data));
@@ -307,7 +306,7 @@ static int hid_press_probe(struct platform_device *pdev)
  		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  		goto error_free_dev_mem;
  	}
-	press_state->common_attributes.data_ready = false;
+	atomic_set(&press_state->common_attributes.data_ready, 0);
  	ret = hid_sensor_setup_trigger(indio_dev, name,
  				&press_state->common_attributes);
  	if (ret) {
include/linux/hid-sensor-hub.h +1 -1
index 88d8d63..51f7cca 100644
@@ -189,7 +189,7 @@ struct hid_sensor_common {
  	struct hid_sensor_hub_device *hsdev;
  	struct platform_device *pdev;
  	unsigned usage_id;
-	bool data_ready;
+	atomic_t data_ready;
  	struct iio_trigger *trigger;
  	struct hid_sensor_hub_attribute_info poll;
  	struct hid_sensor_hub_attribute_info report_state;
author maintainer
#17 lore raw

PATCH 10/16 iio: hid-sensors: Accelerometer 3D: Raw read support

In reply to Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

---
 drivers/iio/accel/hid-sensor-accel-3d.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/iio/accel/hid-sensor-accel-3d.c +18 -3
index cf61c87..69abf91 100644
@@ -22,6 +22,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/slab.h>
+#include <linux/delay.h>
 #include <linux/hid-sensor-hub.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -60,6 +61,7 @@ static const struct iio_chan_spec accel_3d_channels[] = {
 		.type = IIO_ACCEL,
 		.modified = 1,
 		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -69,6 +71,7 @@ static const struct iio_chan_spec accel_3d_channels[] = {
 		.type = IIO_ACCEL,
 		.modified = 1,
 		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -78,6 +81,7 @@ static const struct iio_chan_spec accel_3d_channels[] = {
 		.type = IIO_ACCEL,
 		.modified = 1,
 		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -108,22 +112,33 @@ static int accel_3d_read_raw(struct iio_dev *indio_dev,
 	u32 address;
 	int ret;
 	int ret_type;
+	s32 poll_value;
 
 	*val = 0;
 	*val2 = 0;
 	switch (mask) {
 	case 0:
+		poll_value = hid_sensor_read_poll_value(
+					&accel_state->common_attributes);
+		if (poll_value < 0)
+			return -EINVAL;
+
+		hid_sensor_power_state(&accel_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
 		report_id = accel_state->accel[chan->scan_index].report_id;
 		address = accel_3d_addresses[chan->scan_index];
 		if (report_id >= 0)
 			*val = sensor_hub_input_attr_get_raw_value(
-				accel_state->common_attributes.hsdev,
-				HID_USAGE_SENSOR_ACCEL_3D, address,
-				report_id);
+					accel_state->common_attributes.hsdev,
+					HID_USAGE_SENSOR_ACCEL_3D, address,
+					report_id);
 		else {
 			*val = 0;
+			hid_sensor_power_state(&accel_state->common_attributes,
+						 false);
 			return -EINVAL;
 		}
+		hid_sensor_power_state(&accel_state->common_attributes, false);
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
1.7.11.7
maintainer Applied
#18 lore raw

Re: [PATCH 10/16] iio: hid-sensors: Accelerometer 3D: Raw read support

Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

Applied to the togreg branch of iio.git

Thanks,

Quoted 78 lines
---
  drivers/iio/accel/hid-sensor-accel-3d.c | 21 ++++++++++++++++++---
  1 file changed, 18 insertions(+), 3 deletions(-)
drivers/iio/accel/hid-sensor-accel-3d.c +18 -3
index cf61c87..69abf91 100644
@@ -22,6 +22,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -60,6 +61,7 @@ static const struct iio_chan_spec accel_3d_channels[] = {
  		.type = IIO_ACCEL,
  		.modified = 1,
  		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -69,6 +71,7 @@ static const struct iio_chan_spec accel_3d_channels[] = {
  		.type = IIO_ACCEL,
  		.modified = 1,
  		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -78,6 +81,7 @@ static const struct iio_chan_spec accel_3d_channels[] = {
  		.type = IIO_ACCEL,
  		.modified = 1,
  		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -108,22 +112,33 @@ static int accel_3d_read_raw(struct iio_dev *indio_dev,
  	u32 address;
  	int ret;
  	int ret_type;
+	s32 poll_value;
 
  	*val = 0;
  	*val2 = 0;
  	switch (mask) {
  	case 0:
+		poll_value = hid_sensor_read_poll_value(
+					&accel_state->common_attributes);
+		if (poll_value < 0)
+			return -EINVAL;
+
+		hid_sensor_power_state(&accel_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
  		report_id = accel_state->accel[chan->scan_index].report_id;
  		address = accel_3d_addresses[chan->scan_index];
  		if (report_id >= 0)
  			*val = sensor_hub_input_attr_get_raw_value(
-				accel_state->common_attributes.hsdev,
-				HID_USAGE_SENSOR_ACCEL_3D, address,
-				report_id);
+					accel_state->common_attributes.hsdev,
+					HID_USAGE_SENSOR_ACCEL_3D, address,
+					report_id);
  		else {
  			*val = 0;
+			hid_sensor_power_state(&accel_state->common_attributes,
+						 false);
  			return -EINVAL;
  		}
+		hid_sensor_power_state(&accel_state->common_attributes, false);
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
author maintainer
#19 lore raw

PATCH 11/16 iio: hid-sensors: Gyro 3D: Raw read support

In reply to Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

---
 drivers/iio/gyro/hid-sensor-gyro-3d.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/iio/gyro/hid-sensor-gyro-3d.c +18 -3
index 392c30b..40f4e49 100644
@@ -22,6 +22,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/slab.h>
+#include <linux/delay.h>
 #include <linux/hid-sensor-hub.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -60,6 +61,7 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
 		.type = IIO_ANGL_VEL,
 		.modified = 1,
 		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -69,6 +71,7 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
 		.type = IIO_ANGL_VEL,
 		.modified = 1,
 		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -78,6 +81,7 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
 		.type = IIO_ANGL_VEL,
 		.modified = 1,
 		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -108,22 +112,33 @@ static int gyro_3d_read_raw(struct iio_dev *indio_dev,
 	u32 address;
 	int ret;
 	int ret_type;
+	s32 poll_value;
 
 	*val = 0;
 	*val2 = 0;
 	switch (mask) {
 	case 0:
+		poll_value = hid_sensor_read_poll_value(
+					&gyro_state->common_attributes);
+		if (poll_value < 0)
+			return -EINVAL;
+
+		hid_sensor_power_state(&gyro_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
 		report_id = gyro_state->gyro[chan->scan_index].report_id;
 		address = gyro_3d_addresses[chan->scan_index];
 		if (report_id >= 0)
 			*val = sensor_hub_input_attr_get_raw_value(
-				gyro_state->common_attributes.hsdev,
-				HID_USAGE_SENSOR_GYRO_3D, address,
-				report_id);
+					gyro_state->common_attributes.hsdev,
+					HID_USAGE_SENSOR_GYRO_3D, address,
+					report_id);
 		else {
 			*val = 0;
+			hid_sensor_power_state(&gyro_state->common_attributes,
+						false);
 			return -EINVAL;
 		}
+		hid_sensor_power_state(&gyro_state->common_attributes, false);
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
1.7.11.7
maintainer Applied
#20 lore raw

Re: [PATCH 11/16] iio: hid-sensors: Gyro 3D: Raw read support

Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

Applied to the togreg branch of iio.git

Thanks,

Quoted 78 lines
---
  drivers/iio/gyro/hid-sensor-gyro-3d.c | 21 ++++++++++++++++++---
  1 file changed, 18 insertions(+), 3 deletions(-)
drivers/iio/gyro/hid-sensor-gyro-3d.c +18 -3
index 392c30b..40f4e49 100644
@@ -22,6 +22,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -60,6 +61,7 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
  		.type = IIO_ANGL_VEL,
  		.modified = 1,
  		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -69,6 +71,7 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
  		.type = IIO_ANGL_VEL,
  		.modified = 1,
  		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -78,6 +81,7 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
  		.type = IIO_ANGL_VEL,
  		.modified = 1,
  		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -108,22 +112,33 @@ static int gyro_3d_read_raw(struct iio_dev *indio_dev,
  	u32 address;
  	int ret;
  	int ret_type;
+	s32 poll_value;
 
  	*val = 0;
  	*val2 = 0;
  	switch (mask) {
  	case 0:
+		poll_value = hid_sensor_read_poll_value(
+					&gyro_state->common_attributes);
+		if (poll_value < 0)
+			return -EINVAL;
+
+		hid_sensor_power_state(&gyro_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
  		report_id = gyro_state->gyro[chan->scan_index].report_id;
  		address = gyro_3d_addresses[chan->scan_index];
  		if (report_id >= 0)
  			*val = sensor_hub_input_attr_get_raw_value(
-				gyro_state->common_attributes.hsdev,
-				HID_USAGE_SENSOR_GYRO_3D, address,
-				report_id);
+					gyro_state->common_attributes.hsdev,
+					HID_USAGE_SENSOR_GYRO_3D, address,
+					report_id);
  		else {
  			*val = 0;
+			hid_sensor_power_state(&gyro_state->common_attributes,
+						false);
  			return -EINVAL;
  		}
+		hid_sensor_power_state(&gyro_state->common_attributes, false);
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
author maintainer
#21 lore raw

PATCH 12/16 iio: hid-sensors: ALS: Raw read support

In reply to Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

---
 drivers/iio/light/hid-sensor-als.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/iio/light/hid-sensor-als.c +19 -5
index e124b39..f34c943 100644
@@ -22,6 +22,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/slab.h>
+#include <linux/delay.h>
 #include <linux/hid-sensor-hub.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -49,6 +50,7 @@ static const struct iio_chan_spec als_channels[] = {
 		.type = IIO_INTENSITY,
 		.modified = 1,
 		.channel2 = IIO_MOD_LIGHT_BOTH,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -79,6 +81,7 @@ static int als_read_raw(struct iio_dev *indio_dev,
 	u32 address;
 	int ret;
 	int ret_type;
+	s32 poll_value;
 
 	*val = 0;
 	*val2 = 0;
@@ -94,12 +97,23 @@ static int als_read_raw(struct iio_dev *indio_dev,
 			report_id = -1;
 			break;
 		}
-		if (report_id >= 0)
+		if (report_id >= 0) {
+			poll_value = hid_sensor_read_poll_value(
+						&als_state->common_attributes);
+			if (poll_value < 0)
+				return -EINVAL;
+
+			hid_sensor_power_state(&als_state->common_attributes,
+						true);
+			msleep_interruptible(poll_value * 2);
+
 			*val = sensor_hub_input_attr_get_raw_value(
-				als_state->common_attributes.hsdev,
-				HID_USAGE_SENSOR_ALS, address,
-				report_id);
-		else {
+					als_state->common_attributes.hsdev,
+					HID_USAGE_SENSOR_ALS, address,
+					report_id);
+			hid_sensor_power_state(&als_state->common_attributes,
+						false);
+		} else {
 			*val = 0;
 			return -EINVAL;
 		}
Signature · 1 line
1.7.11.7
maintainer Applied
#22 lore raw

Re: [PATCH 12/16] iio: hid-sensors: ALS: Raw read support

Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

Applied to the togreg branch of iio.git

Thanks,

Quoted 62 lines
---
  drivers/iio/light/hid-sensor-als.c | 24 +++++++++++++++++++-----
  1 file changed, 19 insertions(+), 5 deletions(-)
drivers/iio/light/hid-sensor-als.c +19 -5
index e124b39..f34c943 100644
@@ -22,6 +22,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -49,6 +50,7 @@ static const struct iio_chan_spec als_channels[] = {
  		.type = IIO_INTENSITY,
  		.modified = 1,
  		.channel2 = IIO_MOD_LIGHT_BOTH,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -79,6 +81,7 @@ static int als_read_raw(struct iio_dev *indio_dev,
  	u32 address;
  	int ret;
  	int ret_type;
+	s32 poll_value;
 
  	*val = 0;
  	*val2 = 0;
@@ -94,12 +97,23 @@ static int als_read_raw(struct iio_dev *indio_dev,
  			report_id = -1;
  			break;
  		}
-		if (report_id >= 0)
+		if (report_id >= 0) {
+			poll_value = hid_sensor_read_poll_value(
+						&als_state->common_attributes);
+			if (poll_value < 0)
+				return -EINVAL;
+
+			hid_sensor_power_state(&als_state->common_attributes,
+						true);
+			msleep_interruptible(poll_value * 2);
+
  			*val = sensor_hub_input_attr_get_raw_value(
-				als_state->common_attributes.hsdev,
-				HID_USAGE_SENSOR_ALS, address,
-				report_id);
-		else {
+					als_state->common_attributes.hsdev,
+					HID_USAGE_SENSOR_ALS, address,
+					report_id);
+			hid_sensor_power_state(&als_state->common_attributes,
+						false);
+		} else {
  			*val = 0;
  			return -EINVAL;
  		}
author maintainer
#23 lore raw

PATCH 13/16 iio: hid-sensors: Proximity: Raw read support

In reply to Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

---
 drivers/iio/light/hid-sensor-prox.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/iio/light/hid-sensor-prox.c +16 -2
index 07e98ec8..d203ef4 100644
@@ -21,6 +21,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/slab.h>
+#include <linux/delay.h>
 #include <linux/hid-sensor-hub.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -75,6 +76,7 @@ static int prox_read_raw(struct iio_dev *indio_dev,
 	u32 address;
 	int ret;
 	int ret_type;
+	s32 poll_value;
 
 	*val = 0;
 	*val2 = 0;
@@ -90,12 +92,24 @@ static int prox_read_raw(struct iio_dev *indio_dev,
 			report_id = -1;
 			break;
 		}
-		if (report_id >= 0)
+		if (report_id >= 0) {
+			poll_value = hid_sensor_read_poll_value(
+					&prox_state->common_attributes);
+			if (poll_value < 0)
+				return -EINVAL;
+
+			hid_sensor_power_state(&prox_state->common_attributes,
+						true);
+
+			msleep_interruptible(poll_value * 2);
+
 			*val = sensor_hub_input_attr_get_raw_value(
 				prox_state->common_attributes.hsdev,
 				HID_USAGE_SENSOR_PROX, address,
 				report_id);
-		else {
+			hid_sensor_power_state(&prox_state->common_attributes,
+						false);
+		} else {
 			*val = 0;
 			return -EINVAL;
 		}
Signature · 1 line
1.7.11.7
maintainer Applied
#24 lore raw

Re: [PATCH 13/16] iio: hid-sensors: Proximity: Raw read support

Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

Applied.

Quoted 52 lines
---
  drivers/iio/light/hid-sensor-prox.c | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)
drivers/iio/light/hid-sensor-prox.c +16 -2
index 07e98ec8..d203ef4 100644
@@ -21,6 +21,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -75,6 +76,7 @@ static int prox_read_raw(struct iio_dev *indio_dev,
  	u32 address;
  	int ret;
  	int ret_type;
+	s32 poll_value;
 
  	*val = 0;
  	*val2 = 0;
@@ -90,12 +92,24 @@ static int prox_read_raw(struct iio_dev *indio_dev,
  			report_id = -1;
  			break;
  		}
-		if (report_id >= 0)
+		if (report_id >= 0) {
+			poll_value = hid_sensor_read_poll_value(
+					&prox_state->common_attributes);
+			if (poll_value < 0)
+				return -EINVAL;
+
+			hid_sensor_power_state(&prox_state->common_attributes,
+						true);
+
+			msleep_interruptible(poll_value * 2);
+
  			*val = sensor_hub_input_attr_get_raw_value(
  				prox_state->common_attributes.hsdev,
  				HID_USAGE_SENSOR_PROX, address,
  				report_id);
-		else {
+			hid_sensor_power_state(&prox_state->common_attributes,
+						false);
+		} else {
  			*val = 0;
  			return -EINVAL;
  		}
author maintainer
#25 lore raw

PATCH 14/16 iio: hid-sensors: Compass 3D: Raw read support

In reply to Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value

---
 drivers/iio/magnetometer/hid-sensor-magn-3d.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/iio/magnetometer/hid-sensor-magn-3d.c +16 -0
index 54eea6a..41cf29e 100644
@@ -22,6 +22,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/slab.h>
+#include <linux/delay.h>
 #include <linux/hid-sensor-hub.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -60,6 +61,7 @@ static const struct iio_chan_spec magn_3d_channels[] = {
 		.type = IIO_MAGN,
 		.modified = 1,
 		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -69,6 +71,7 @@ static const struct iio_chan_spec magn_3d_channels[] = {
 		.type = IIO_MAGN,
 		.modified = 1,
 		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -78,6 +81,7 @@ static const struct iio_chan_spec magn_3d_channels[] = {
 		.type = IIO_MAGN,
 		.modified = 1,
 		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -108,11 +112,20 @@ static int magn_3d_read_raw(struct iio_dev *indio_dev,
 	u32 address;
 	int ret;
 	int ret_type;
+	s32 poll_value;
 
 	*val = 0;
 	*val2 = 0;
 	switch (mask) {
 	case 0:
+		poll_value = hid_sensor_read_poll_value(
+					&magn_state->common_attributes);
+		if (poll_value < 0)
+				return -EINVAL;
+
+		hid_sensor_power_state(&magn_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
+
 		report_id =
 			magn_state->magn[chan->scan_index].report_id;
 		address = magn_3d_addresses[chan->scan_index];
@@ -123,8 +136,11 @@ static int magn_3d_read_raw(struct iio_dev *indio_dev,
 				report_id);
 		else {
 			*val = 0;
+			hid_sensor_power_state(&magn_state->common_attributes,
+						false);
 			return -EINVAL;
 		}
+		hid_sensor_power_state(&magn_state->common_attributes, false);
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
1.7.11.7
maintainer Applied
#26 lore raw

Re: [PATCH 14/16] iio: hid-sensors: Compass 3D: Raw read support

Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value

Applied to the togreg branch of iio.git

Thanks,

Quoted 74 lines
---
  drivers/iio/magnetometer/hid-sensor-magn-3d.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
drivers/iio/magnetometer/hid-sensor-magn-3d.c +16 -0
index 54eea6a..41cf29e 100644
@@ -22,6 +22,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -60,6 +61,7 @@ static const struct iio_chan_spec magn_3d_channels[] = {
  		.type = IIO_MAGN,
  		.modified = 1,
  		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -69,6 +71,7 @@ static const struct iio_chan_spec magn_3d_channels[] = {
  		.type = IIO_MAGN,
  		.modified = 1,
  		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -78,6 +81,7 @@ static const struct iio_chan_spec magn_3d_channels[] = {
  		.type = IIO_MAGN,
  		.modified = 1,
  		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  		BIT(IIO_CHAN_INFO_SCALE) |
  		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
@@ -108,11 +112,20 @@ static int magn_3d_read_raw(struct iio_dev *indio_dev,
  	u32 address;
  	int ret;
  	int ret_type;
+	s32 poll_value;
 
  	*val = 0;
  	*val2 = 0;
  	switch (mask) {
  	case 0:
+		poll_value = hid_sensor_read_poll_value(
+					&magn_state->common_attributes);
+		if (poll_value < 0)
+				return -EINVAL;
+
+		hid_sensor_power_state(&magn_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
+
  		report_id =
  			magn_state->magn[chan->scan_index].report_id;
  		address = magn_3d_addresses[chan->scan_index];
@@ -123,8 +136,11 @@ static int magn_3d_read_raw(struct iio_dev *indio_dev,
  				report_id);
  		else {
  			*val = 0;
+			hid_sensor_power_state(&magn_state->common_attributes,
+						false);
  			return -EINVAL;
  		}
+		hid_sensor_power_state(&magn_state->common_attributes, false);
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
author maintainer
#27 lore raw

PATCH 15/16 iio: hid-sensors: Inclinometer 3D: Raw read support

In reply to Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

---
 drivers/iio/orientation/hid-sensor-incl-3d.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/orientation/hid-sensor-incl-3d.c +13 -0
index bf11678..2478f6c 100644
@@ -22,6 +22,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/slab.h>
+#include <linux/delay.h>
 #include <linux/hid-sensor-hub.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -110,11 +111,20 @@ static int incl_3d_read_raw(struct iio_dev *indio_dev,
 	int report_id = -1;
 	u32 address;
 	int ret_type;
+	s32 poll_value;
 
 	*val = 0;
 	*val2 = 0;
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		poll_value = hid_sensor_read_poll_value(
+					&incl_state->common_attributes);
+		if (poll_value < 0)
+			return -EINVAL;
+
+		hid_sensor_power_state(&incl_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
+
 		report_id =
 			incl_state->incl[chan->scan_index].report_id;
 		address = incl_3d_addresses[chan->scan_index];
@@ -124,8 +134,11 @@ static int incl_3d_read_raw(struct iio_dev *indio_dev,
 				HID_USAGE_SENSOR_INCLINOMETER_3D, address,
 				report_id);
 		else {
+			hid_sensor_power_state(&incl_state->common_attributes,
+						false);
 			return -EINVAL;
 		}
+		hid_sensor_power_state(&incl_state->common_attributes, false);
 		ret_type = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
1.7.11.7
maintainer Applied
#28 lore raw

Re: [PATCH 15/16] iio: hid-sensors: Inclinometer 3D: Raw read support

Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

Applied to the togreg branch of iio.git

Thanks

Quoted 50 lines
---
  drivers/iio/orientation/hid-sensor-incl-3d.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/iio/orientation/hid-sensor-incl-3d.c +13 -0
index bf11678..2478f6c 100644
@@ -22,6 +22,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -110,11 +111,20 @@ static int incl_3d_read_raw(struct iio_dev *indio_dev,
  	int report_id = -1;
  	u32 address;
  	int ret_type;
+	s32 poll_value;
 
  	*val = 0;
  	*val2 = 0;
  	switch (mask) {
  	case IIO_CHAN_INFO_RAW:
+		poll_value = hid_sensor_read_poll_value(
+					&incl_state->common_attributes);
+		if (poll_value < 0)
+			return -EINVAL;
+
+		hid_sensor_power_state(&incl_state->common_attributes, true);
+		msleep_interruptible(poll_value * 2);
+
  		report_id =
  			incl_state->incl[chan->scan_index].report_id;
  		address = incl_3d_addresses[chan->scan_index];
@@ -124,8 +134,11 @@ static int incl_3d_read_raw(struct iio_dev *indio_dev,
  				HID_USAGE_SENSOR_INCLINOMETER_3D, address,
  				report_id);
  		else {
+			hid_sensor_power_state(&incl_state->common_attributes,
+						false);
  			return -EINVAL;
  		}
+		hid_sensor_power_state(&incl_state->common_attributes, false);
  		ret_type = IIO_VAL_INT;
  		break;
  	case IIO_CHAN_INFO_SCALE:
author maintainer
#29 lore raw

PATCH 16/16 iio: hid-sensors: Pressure: Raw read support

In reply to Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

---
 drivers/iio/pressure/hid-sensor-press.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iio/pressure/hid-sensor-press.c +15 -2
index 39df50c..1cd190c 100644
@@ -21,6 +21,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/slab.h>
+#include <linux/delay.h>
 #include <linux/hid-sensor-hub.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -79,6 +80,7 @@ static int press_read_raw(struct iio_dev *indio_dev,
 	u32 address;
 	int ret;
 	int ret_type;
+	s32 poll_value;
 
 	*val = 0;
 	*val2 = 0;
@@ -94,12 +96,23 @@ static int press_read_raw(struct iio_dev *indio_dev,
 			report_id = -1;
 			break;
 		}
-		if (report_id >= 0)
+		if (report_id >= 0) {
+			poll_value = hid_sensor_read_poll_value(
+					&press_state->common_attributes);
+			if (poll_value < 0)
+				return -EINVAL;
+			hid_sensor_power_state(&press_state->common_attributes,
+						true);
+
+			msleep_interruptible(poll_value * 2);
+
 			*val = sensor_hub_input_attr_get_raw_value(
 				press_state->common_attributes.hsdev,
 				HID_USAGE_SENSOR_PRESSURE, address,
 				report_id);
-		else {
+			hid_sensor_power_state(&press_state->common_attributes,
+						false);
+		} else {
 			*val = 0;
 			return -EINVAL;
 		}
Signature · 1 line
1.7.11.7
maintainer
#30 lore raw

Re: [PATCH 16/16] iio: hid-sensors: Pressure: Raw read support

Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

Hi Srinivas.

Looks like a good patch set. Probably should have been 'formally' broken in two to separate the ABI related fixes and the new stuff.

I'll let this sit for a while to garner opinions on the ABI change.

Quoted 51 lines
---
  drivers/iio/pressure/hid-sensor-press.c | 17 +++++++++++++++--
  1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iio/pressure/hid-sensor-press.c +15 -2
index 39df50c..1cd190c 100644
@@ -21,6 +21,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -79,6 +80,7 @@ static int press_read_raw(struct iio_dev *indio_dev,
  	u32 address;
  	int ret;
  	int ret_type;
+	s32 poll_value;
 
  	*val = 0;
  	*val2 = 0;
@@ -94,12 +96,23 @@ static int press_read_raw(struct iio_dev *indio_dev,
  			report_id = -1;
  			break;
  		}
-		if (report_id >= 0)
+		if (report_id >= 0) {
+			poll_value = hid_sensor_read_poll_value(
+					&press_state->common_attributes);
+			if (poll_value < 0)
+				return -EINVAL;
+			hid_sensor_power_state(&press_state->common_attributes,
+						true);
+
+			msleep_interruptible(poll_value * 2);
+
  			*val = sensor_hub_input_attr_get_raw_value(
  				press_state->common_attributes.hsdev,
  				HID_USAGE_SENSOR_PRESSURE, address,
  				report_id);
-		else {
+			hid_sensor_power_state(&press_state->common_attributes,
+						false);
+		} else {
  			*val = 0;
  			return -EINVAL;
  		}
maintainer
#31 lore raw

Re: [PATCH 16/16] iio: hid-sensors: Pressure: Raw read support

Quoted 11 lines · Jonathan Cameron
Quoted 4 lines · Srinivas Pandruvada

Added support for raw reading of channel. If the sensor is powered off, it will turn on for reading value.

Hi Srinivas.

Looks like a good patch set. Probably should have been 'formally' broken in two to separate the ABI related fixes and the new stuff.

I'll let this sit for a while to garner opinions on the ABI change.

Seems no one cares so lets put it in.

All applied to the togreg branch of iio.git pushed out initially as testing.

Jonathan

Quoted 56 lines
Quoted 41 lines
---
  drivers/iio/pressure/hid-sensor-press.c | 17 +++++++++++++++--
  1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iio/pressure/hid-sensor-press.c +12 -1
index 39df50c..1cd190c 100644
@@ -21,6 +21,7 @@
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/slab.h>
+#include <linux/delay.h>
  #include <linux/hid-sensor-hub.h>
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
@@ -79,6 +80,7 @@ static int press_read_raw(struct iio_dev *indio_dev,
      u32 address;
      int ret;
      int ret_type;
+    s32 poll_value;
 
      *val = 0;
      *val2 = 0;
@@ -94,12 +96,23 @@ static int press_read_raw(struct iio_dev *indio_dev,
              report_id = -1;
              break;
          }
-        if (report_id >= 0)
+        if (report_id >= 0) {
+            poll_value = hid_sensor_read_poll_value(
+                    &press_state->common_attributes);
+            if (poll_value < 0)
+                return -EINVAL;
+            hid_sensor_power_state(&press_state->common_attributes,
+                        true);
+
+            msleep_interruptible(poll_value * 2);
+
              *val = sensor_hub_input_attr_get_raw_value(

drivers/iio/pressure/hid-sensor-press.c static int press_read_raw(struct iio_dev *indio_dev, · line 118

Quoted 10 lines
drivers/iio/pressure/hid-sensor-press.c +3 -1
                  press_state->common_attributes.hsdev,
                  HID_USAGE_SENSOR_PRESSURE, address,
                  report_id);
-        else {
+            hid_sensor_power_state(&press_state->common_attributes,
+                        false);
+        } else {
              *val = 0;
              return -EINVAL;
          }

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#32 lore raw
In reply to Srinivas Pandruvada
Quoted 7 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

Hi Srinivas,

What 'grief' is this abi change likely to cause us?

I know I effectively asked you to fix this, but I'd like to get a handle on just how many people are likely to shout.

The original code is a gross breaking of the published ABI so it probably is reasonable to change it...

I'm too tired to review this properly right now so only the most superficial comments in line.

Quoted 51 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 +++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+	u32 usage_id;
+	int unit; /* 0 for default others from HID sensor spec */
+	int scale_val0; /* scale, whole number */
+	int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+	{HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+	{HID_USAGE_SENSOR_ACCEL_3D,
+		HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+	{HID_USAGE_SENSOR_ACCEL_3D,
+		HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+	{HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+	{HID_USAGE_SENSOR_GYRO_3D,
+		HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+	{HID_USAGE_SENSOR_GYRO_3D,
+		HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+	{HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+	{HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+	{HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+	{HID_USAGE_SENSOR_INCLINOMETER_3D,
+		HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+	{HID_USAGE_SENSOR_INCLINOMETER_3D,
+		HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+	{HID_USAGE_SENSOR_ALS, 0, 1, 0},
+	{HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+	{HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+	{HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
  	int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 247

Quoted 5 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +2 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.

function.

Good examples btw.

Quoted 95 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +78 -0
+ * For example:
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+				  int scale1, int exp)
+{
+	int i;
+	int x;
+	int res;
+	int rem;
+
+	if (exp > 0) {
+		*val0 = scale0 * pow_10(exp);
+		res = 0;
+		if (exp > 6) {
+			*val1 = 0;
+			return;
+		}
+		for (i = 0; i < exp; ++i) {
+			x = scale1 / pow_10(5 - i);
+			res += (pow_10(exp - 1 - i) * x);
+			scale1 = scale1 % pow_10(5 - i);
+		}
+		*val0 += res;
+			*val1 = scale1 * pow_10(exp);
+	} else if (exp < 0) {
+		exp = abs(exp);
+		if (exp > 6) {
+			*val0 = *val1 = 0;
+			return;
+		}
+		*val0 = scale0 / pow_10(exp);
+		rem = scale0 % pow_10(exp);
+		res = 0;
+		for (i = 0; i < (6 - exp); ++i) {
+			x = scale1 / pow_10(5 - i);
+			res += (pow_10(5 - exp - i) * x);
+			scale1 = scale1 % pow_10(5 - i);
+		}
+		*val1 = rem * pow_10(6 - exp) + res;
+	} else {
+		*val0 = scale0;
+		*val1 = scale1;
+	}
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+			struct hid_sensor_hub_attribute_info *attr_info,
+			int *val0, int *val1)
+{
+	int i;
+	int exp;
+
+	*val0 = 1;
+	*val1 = 0;
+
+	for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+		if (unit_conversion[i].usage_id == usage_id &&
+			unit_conversion[i].unit == attr_info->units) {
+			exp  = hid_sensor_convert_exponent(
+						attr_info->unit_expo);
+			adjust_exponent_micro(val0, val1,
+					unit_conversion[i].scale_val0,
+					unit_conversion[i].scale_val1, exp);
+			break;
+		}
+	}
+
+	return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
  					u32 usage_id,
  					struct hid_sensor_common *st)
include/linux/hid-sensor-hub.h +4 -0
index b70cfd7..89626b2 100644
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
  				u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+			    struct hid_sensor_hub_attribute_info *attr_info,
+			    int *val0, int *val1);
+
  #endif
author maintainer
#33 lore raw
Quoted 7 lines · Jonathan Cameron
Quoted 6 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

Quoted 6 lines
Quoted 1 line

Hi Srinivas,

What 'grief' is this abi change likely to cause us?

Hi Jonathan,

Since the products are just released with the hubs, I don't think there is much impact. I have notified few users I knew, who pointed me about this disparity.

Thanks,
Srinivas

Quoted 168 lines

I know I effectively asked you to fix this, but I'd like to get a handle on just how many people are likely to shout.

The original code is a gross breaking of the published ABI so it probably is reasonable to change it...

I'm too tired to review this properly right now so only the most superficial comments in line.

Quoted 52 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 
+++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+    u32 usage_id;
+    int unit; /* 0 for default others from HID sensor spec */
+    int scale_val0; /* scale, whole number */
+    int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+    {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+    {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+    {HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+    {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+    {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+    {HID_USAGE_SENSOR_ALS, 0, 1, 0},
+    {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+    {HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+    {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
      int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct · line 248

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +2 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct 
 hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.

function.

Good examples btw.

Quoted 97 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +78 -0
+ * For example:
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+                  int scale1, int exp)
+{
+    int i;
+    int x;
+    int res;
+    int rem;
+
+    if (exp > 0) {
+        *val0 = scale0 * pow_10(exp);
+        res = 0;
+        if (exp > 6) {
+            *val1 = 0;
+            return;
+        }
+        for (i = 0; i < exp; ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(exp - 1 - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val0 += res;
+            *val1 = scale1 * pow_10(exp);
+    } else if (exp < 0) {
+        exp = abs(exp);
+        if (exp > 6) {
+            *val0 = *val1 = 0;
+            return;
+        }
+        *val0 = scale0 / pow_10(exp);
+        rem = scale0 % pow_10(exp);
+        res = 0;
+        for (i = 0; i < (6 - exp); ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(5 - exp - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val1 = rem * pow_10(6 - exp) + res;
+    } else {
+        *val0 = scale0;
+        *val1 = scale1;
+    }
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+            struct hid_sensor_hub_attribute_info *attr_info,
+            int *val0, int *val1)
+{
+    int i;
+    int exp;
+
+    *val0 = 1;
+    *val1 = 0;
+
+    for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+        if (unit_conversion[i].usage_id == usage_id &&
+            unit_conversion[i].unit == attr_info->units) {
+            exp  = hid_sensor_convert_exponent(
+                        attr_info->unit_expo);
+            adjust_exponent_micro(val0, val1,
+                    unit_conversion[i].scale_val0,
+                    unit_conversion[i].scale_val1, exp);
+            break;
+        }
+    }
+
+    return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device 
 *hsdev,

u32 usage_id,
struct hid_sensor_common *st)

include/linux/hid-sensor-hub.h +4 -0
index b70cfd7..89626b2 100644
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct 
 hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
                  u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+                struct hid_sensor_hub_attribute_info *attr_info,
+                int *val0, int *val1);
+
  #endif
maintainer
#34 lore raw
Quoted 15 lines · Srinivas Pandruvada
Quoted 7 lines · Jonathan Cameron
Quoted 6 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

Quoted 6 lines
Quoted 1 line

Hi Srinivas,

What 'grief' is this abi change likely to cause us?

Quoted 8 lines

Hi Jonathan,

Since the products are just released with the hubs, I don't think there is much impact. I have notified few users I knew, who pointed me about this disparity.

Thanks,
Srinivas

Cool. I'll attach a health warning to the pull request that this 'might cause' grief, but the view is that it is unlikely and we are simply bringing the driver in line with the API that existed at the time. I'll also add that we have users noticing and querying the disrepency.

Quoted 165 lines
Quoted 164 lines

I know I effectively asked you to fix this, but I'd like to get a handle on just how many people are likely to shout.

The original code is a gross breaking of the published ABI so it probably is reasonable to change it...

I'm too tired to review this properly right now so only the most superficial comments in line.

Quoted 51 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 +++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+    u32 usage_id;
+    int unit; /* 0 for default others from HID sensor spec */
+    int scale_val0; /* scale, whole number */
+    int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+    {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+    {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+    {HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+    {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+    {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+    {HID_USAGE_SENSOR_ALS, 0, 1, 0},
+    {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+    {HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+    {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
      int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 247

Quoted 5 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +2 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.

function.

Good examples btw.

Quoted 95 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +78 -0
+ * For example:
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+                  int scale1, int exp)
+{
+    int i;
+    int x;
+    int res;
+    int rem;
+
+    if (exp > 0) {
+        *val0 = scale0 * pow_10(exp);
+        res = 0;
+        if (exp > 6) {
+            *val1 = 0;
+            return;
+        }
+        for (i = 0; i < exp; ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(exp - 1 - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val0 += res;
+            *val1 = scale1 * pow_10(exp);
+    } else if (exp < 0) {
+        exp = abs(exp);
+        if (exp > 6) {
+            *val0 = *val1 = 0;
+            return;
+        }
+        *val0 = scale0 / pow_10(exp);
+        rem = scale0 % pow_10(exp);
+        res = 0;
+        for (i = 0; i < (6 - exp); ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(5 - exp - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val1 = rem * pow_10(6 - exp) + res;
+    } else {
+        *val0 = scale0;
+        *val1 = scale1;
+    }
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+            struct hid_sensor_hub_attribute_info *attr_info,
+            int *val0, int *val1)
+{
+    int i;
+    int exp;
+
+    *val0 = 1;
+    *val1 = 0;
+
+    for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+        if (unit_conversion[i].usage_id == usage_id &&
+            unit_conversion[i].unit == attr_info->units) {
+            exp  = hid_sensor_convert_exponent(
+                        attr_info->unit_expo);
+            adjust_exponent_micro(val0, val1,
+                    unit_conversion[i].scale_val0,
+                    unit_conversion[i].scale_val1, exp);
+            break;
+        }
+    }
+
+    return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
                      u32 usage_id,
                      struct hid_sensor_common *st)
include/linux/hid-sensor-hub.h +4 -0
index b70cfd7..89626b2 100644
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
                  u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+                struct hid_sensor_hub_attribute_info *attr_info,
+                int *val0, int *val1);
+
  #endif
maintainer
#35 lore raw
In reply to Srinivas Pandruvada
Quoted 7 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

see inline.

Quoted 51 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 +++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+	u32 usage_id;
+	int unit; /* 0 for default others from HID sensor spec */
+	int scale_val0; /* scale, whole number */
+	int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+	{HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+	{HID_USAGE_SENSOR_ACCEL_3D,
+		HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+	{HID_USAGE_SENSOR_ACCEL_3D,
+		HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+	{HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+	{HID_USAGE_SENSOR_GYRO_3D,
+		HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+	{HID_USAGE_SENSOR_GYRO_3D,
+		HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+	{HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+	{HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+	{HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+	{HID_USAGE_SENSOR_INCLINOMETER_3D,
+		HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+	{HID_USAGE_SENSOR_INCLINOMETER_3D,
+		HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+	{HID_USAGE_SENSOR_ALS, 0, 1, 0},
+	{HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+	{HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+	{HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
  	int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 248

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +3 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.
+ * For example:

Function

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 255

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]

It took me a while to get what you meant here, perhaps for the examples it is worth showing the actual value directly as well. Maybe. 9.806650 exp:2->980.6650 -> val0[980]val1[665000] (note the above is I think incorrect due to lack of trailing zeros on val1.

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +6 -0
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+				  int scale1, int exp)
+{
+	int i;
+	int x;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 271

Quoted 10 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +10 -0
+	int res;
+	int rem;
+
+	if (exp > 0) {
+		*val0 = scale0 * pow_10(exp);
+		res = 0;
+		if (exp > 6) {
+			*val1 = 0;
+			return;
+		}

so if example 1 above..
val0 = 900 as desired.
i = 0;
sca1e1 = 806650
x = 806650/100000 = 8;
res = 80;
scale1 = 06650
i = 1
x = 06650/10000 = 0;
res = 80;
so 980. Good.

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 278

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+		for (i = 0; i < exp; ++i) {
+			x = scale1 / pow_10(5 - i);
+			res += (pow_10(exp - 1 - i) * x);
+			scale1 = scale1 % pow_10(5 - i);
+		}
+		*val0 += res;
+			*val1 = scale1 * pow_10(exp);

val1 = 665000 which is correct with the trailing zeros in val2. So your code is good but examples need those zeros!

Quoted 64 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +47 -0
+	} else if (exp < 0) {
+		exp = abs(exp);
+		if (exp > 6) {
+			*val0 = *val1 = 0;
+			return;
+		}
+		*val0 = scale0 / pow_10(exp);
+		rem = scale0 % pow_10(exp);
+		res = 0;
+		for (i = 0; i < (6 - exp); ++i) {
+			x = scale1 / pow_10(5 - i);
+			res += (pow_10(5 - exp - i) * x);
+			scale1 = scale1 % pow_10(5 - i);
+		}
+		*val1 = rem * pow_10(6 - exp) + res;
+	} else {
+		*val0 = scale0;
+		*val1 = scale1;
+	}
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+			struct hid_sensor_hub_attribute_info *attr_info,
+			int *val0, int *val1)
+{
+	int i;
+	int exp;
+
+	*val0 = 1;
+	*val1 = 0;
+
+	for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+		if (unit_conversion[i].usage_id == usage_id &&
+			unit_conversion[i].unit == attr_info->units) {
+			exp  = hid_sensor_convert_exponent(
+						attr_info->unit_expo);
+			adjust_exponent_micro(val0, val1,
+					unit_conversion[i].scale_val0,
+					unit_conversion[i].scale_val1, exp);
+			break;
+		}
+	}
+
+	return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
  					u32 usage_id,
  					struct hid_sensor_common *st)
include/linux/hid-sensor-hub.h +4 -0
index b70cfd7..89626b2 100644
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
  				u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+			    struct hid_sensor_hub_attribute_info *attr_info,
+			    int *val0, int *val1);
+
  #endif
author maintainer
#36 lore raw
Quoted 114 lines · Jonathan Cameron
Quoted 7 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

see inline.

Quoted 52 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 
+++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+    u32 usage_id;
+    int unit; /* 0 for default others from HID sensor spec */
+    int scale_val0; /* scale, whole number */
+    int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+    {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+    {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+    {HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+    {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+    {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+    {HID_USAGE_SENSOR_ALS, 0, 1, 0},
+    {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+    {HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+    {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
      int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct · line 249

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +3 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct 
 hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.
+ * For example:

Function

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 255

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]

It took me a while to get what you meant here, perhaps for the examples it is worth showing the actual value directly as well. Maybe. 9.806650 exp:2->980.6650 -> val0[980]val1[665000] (note the above is I think incorrect due to lack of trailing zeros on val1.

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +6 -0
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+                  int scale1, int exp)
+{
+    int i;
+    int x;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 271

Quoted 10 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +10 -0
+    int res;
+    int rem;
+
+    if (exp > 0) {
+        *val0 = scale0 * pow_10(exp);
+        res = 0;
+        if (exp > 6) {
+            *val1 = 0;
+            return;
+        }

so if example 1 above..
val0 = 900 as desired.
i = 0;
sca1e1 = 806650
x = 806650/100000 = 8;
res = 80;
scale1 = 06650
i = 1
x = 06650/10000 = 0;
res = 80;
so 980. Good.

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 278

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+        for (i = 0; i < exp; ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(exp - 1 - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val0 += res;
+            *val1 = scale1 * pow_10(exp);
Quoted 2 lines

val1 = 665000 which is correct with the trailing zeros in val2. So your code is good but examples need those zeros!

Impressive, Absolutely great review. I will correct comments in the next version.

Thanks,
Srinivas

Quoted 73 lines
Quoted 67 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +47 -0
+    } else if (exp < 0) {
+        exp = abs(exp);
+        if (exp > 6) {
+            *val0 = *val1 = 0;
+            return;
+        }
+        *val0 = scale0 / pow_10(exp);
+        rem = scale0 % pow_10(exp);
+        res = 0;
+        for (i = 0; i < (6 - exp); ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(5 - exp - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val1 = rem * pow_10(6 - exp) + res;
+    } else {
+        *val0 = scale0;
+        *val1 = scale1;
+    }
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+            struct hid_sensor_hub_attribute_info *attr_info,
+            int *val0, int *val1)
+{
+    int i;
+    int exp;
+
+    *val0 = 1;
+    *val1 = 0;
+
+    for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+        if (unit_conversion[i].usage_id == usage_id &&
+            unit_conversion[i].unit == attr_info->units) {
+            exp  = hid_sensor_convert_exponent(
+                        attr_info->unit_expo);
+            adjust_exponent_micro(val0, val1,
+                    unit_conversion[i].scale_val0,
+                    unit_conversion[i].scale_val1, exp);
+            break;
+        }
+    }
+
+    return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device 
 *hsdev,

u32 usage_id,
struct hid_sensor_common *st)

include/linux/hid-sensor-hub.h +4 -0
index b70cfd7..89626b2 100644
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct 
 hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
                  u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+                struct hid_sensor_hub_attribute_info *attr_info,
+                int *val0, int *val1);
+
  #endif
Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-iio" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer Applied
#37 lore raw
Quoted 11 lines · Srinivas Pandruvada
Quoted 9 lines · Jonathan Cameron
Quoted 7 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

see inline.

Applied to the togreg branch of iio.git with a tweaks to comments as mentioned (please check I didn't mess them up!)

Quoted 103 lines
Quoted 103 lines
Quoted 51 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 +++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+    u32 usage_id;
+    int unit; /* 0 for default others from HID sensor spec */
+    int scale_val0; /* scale, whole number */
+    int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+    {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+    {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+    {HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+    {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+    {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+    {HID_USAGE_SENSOR_ALS, 0, 1, 0},
+    {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+    {HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+    {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
      int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 248

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +3 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.
+ * For example:

Function

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 255

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]

It took me a while to get what you meant here, perhaps for the examples it is worth showing the actual value directly as well. Maybe. 9.806650 exp:2->980.6650 -> val0[980]val1[665000] (note the above is I think incorrect due to lack of trailing zeros on val1.

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +6 -0
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+                  int scale1, int exp)
+{
+    int i;
+    int x;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 271

Quoted 10 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +10 -0
+    int res;
+    int rem;
+
+    if (exp > 0) {
+        *val0 = scale0 * pow_10(exp);
+        res = 0;
+        if (exp > 6) {
+            *val1 = 0;
+            return;
+        }

so if example 1 above..
val0 = 900 as desired.
i = 0;
sca1e1 = 806650
x = 806650/100000 = 8;
res = 80;
scale1 = 06650
i = 1
x = 06650/10000 = 0;
res = 80;
so 980. Good.

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+        for (i = 0; i < exp; ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(exp - 1 - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val0 += res;
+            *val1 = scale1 * pow_10(exp);
Quoted 3 lines
Quoted 2 lines

val1 = 665000 which is correct with the trailing zeros in val2. So your code is good but examples need those zeros!

Impressive, Absolutely great review. I will correct comments in the next version.

Given this was the only issue in the patch set I'll just fix it up as I merge it.

It's been a couple of weeks an noone else has chipped in on this series, so I'm taking it now.

Quoted 75 lines

Thanks,
Srinivas

Quoted 71 lines
Quoted 57 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +47 -0
+    } else if (exp < 0) {
+        exp = abs(exp);
+        if (exp > 6) {
+            *val0 = *val1 = 0;
+            return;
+        }
+        *val0 = scale0 / pow_10(exp);
+        rem = scale0 % pow_10(exp);
+        res = 0;
+        for (i = 0; i < (6 - exp); ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(5 - exp - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val1 = rem * pow_10(6 - exp) + res;
+    } else {
+        *val0 = scale0;
+        *val1 = scale1;
+    }
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+            struct hid_sensor_hub_attribute_info *attr_info,
+            int *val0, int *val1)
+{
+    int i;
+    int exp;
+
+    *val0 = 1;
+    *val1 = 0;
+
+    for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+        if (unit_conversion[i].usage_id == usage_id &&
+            unit_conversion[i].unit == attr_info->units) {
+            exp  = hid_sensor_convert_exponent(
+                        attr_info->unit_expo);
+            adjust_exponent_micro(val0, val1,
+                    unit_conversion[i].scale_val0,
+                    unit_conversion[i].scale_val1, exp);
+            break;
+        }
+    }
+
+    return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
                      u32 usage_id,
                      struct hid_sensor_common *st)
include/linux/hid-sensor-hub.h +0 -0
index b70cfd7..89626b2 100644

include/linux/hid-sensor-hub.h int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st, · line 230

Quoted 8 lines
include/linux/hid-sensor-hub.h +4 -0
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
                  u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+                struct hid_sensor_hub_attribute_info *attr_info,
+                int *val0, int *val1);
+
  #endif

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#38 lore raw
Quoted 10 lines · Jonathan Cameron
Quoted 9 lines · Srinivas Pandruvada
Quoted 7 lines · Jonathan Cameron
Quoted 6 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

see inline.

Applied to the togreg branch of iio.git with a tweaks to comments as mentioned (please check I didn't mess them up!)

I should let my build tests finish before sending these sorts of emails out. There was a missing static on the unit_conversion structure. Added to the version applied...

Quoted 194 lines
Quoted 104 lines
Quoted 104 lines
Quoted 52 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 +++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+    u32 usage_id;
+    int unit; /* 0 for default others from HID sensor spec */
+    int scale_val0; /* scale, whole number */
+    int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+    {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+    {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+    {HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+    {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
+
+    {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+    {HID_USAGE_SENSOR_ALS, 0, 1, 0},
+    {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+    {HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+    {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
      int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 248

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +3 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.
+ * For example:

Function

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 255

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]

It took me a while to get what you meant here, perhaps for the examples it is worth showing the actual value directly as well. Maybe. 9.806650 exp:2->980.6650 -> val0[980]val1[665000] (note the above is I think incorrect due to lack of trailing zeros on val1.

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +6 -0
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+                  int scale1, int exp)
+{
+    int i;
+    int x;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 271

Quoted 10 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +10 -0
+    int res;
+    int rem;
+
+    if (exp > 0) {
+        *val0 = scale0 * pow_10(exp);
+        res = 0;
+        if (exp > 6) {
+            *val1 = 0;
+            return;
+        }

so if example 1 above..
val0 = 900 as desired.
i = 0;
sca1e1 = 806650
x = 806650/100000 = 8;
res = 80;
scale1 = 06650
i = 1
x = 06650/10000 = 0;
res = 80;
so 980. Good.

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+        for (i = 0; i < exp; ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(exp - 1 - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val0 += res;
+            *val1 = scale1 * pow_10(exp);
Quoted 3 lines
Quoted 2 lines

val1 = 665000 which is correct with the trailing zeros in val2. So your code is good but examples need those zeros!

Impressive, Absolutely great review. I will correct comments in the next version.

Given this was the only issue in the patch set I'll just fix it up as I merge it.

It's been a couple of weeks an noone else has chipped in on this series, so I'm taking it now.

Quoted 75 lines

Thanks,
Srinivas

Quoted 71 lines
Quoted 57 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +47 -0
+    } else if (exp < 0) {
+        exp = abs(exp);
+        if (exp > 6) {
+            *val0 = *val1 = 0;
+            return;
+        }
+        *val0 = scale0 / pow_10(exp);
+        rem = scale0 % pow_10(exp);
+        res = 0;
+        for (i = 0; i < (6 - exp); ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(5 - exp - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val1 = rem * pow_10(6 - exp) + res;
+    } else {
+        *val0 = scale0;
+        *val1 = scale1;
+    }
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+            struct hid_sensor_hub_attribute_info *attr_info,
+            int *val0, int *val1)
+{
+    int i;
+    int exp;
+
+    *val0 = 1;
+    *val1 = 0;
+
+    for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+        if (unit_conversion[i].usage_id == usage_id &&
+            unit_conversion[i].unit == attr_info->units) {
+            exp  = hid_sensor_convert_exponent(
+                        attr_info->unit_expo);
+            adjust_exponent_micro(val0, val1,
+                    unit_conversion[i].scale_val0,
+                    unit_conversion[i].scale_val1, exp);
+            break;
+        }
+    }
+
+    return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
                      u32 usage_id,
                      struct hid_sensor_common *st)
include/linux/hid-sensor-hub.h +0 -0
index b70cfd7..89626b2 100644

include/linux/hid-sensor-hub.h int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st, · line 230

Quoted 8 lines
include/linux/hid-sensor-hub.h +4 -0
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
                  u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+                struct hid_sensor_hub_attribute_info *attr_info,
+                int *val0, int *val1);
+
  #endif

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#39 lore raw
In reply to Jonathan Cameron

Hi Jonathan,

Somebody pointed me some error in magnetometer calculation, I have a correction, I will submit as another patch. I was about to push second version of these patches.

Thanks,
Srinivas

Quoted 210 lines · Jonathan Cameron
Quoted 11 lines · Srinivas Pandruvada
Quoted 9 lines · Jonathan Cameron
Quoted 7 lines · Srinivas Pandruvada

HID sensor hub specify a default unit and alternative units. This along with unit exponent can be used adjust scale. This change change HID sensor data units to IIO defined units for each sensor type. So in this way user space can use a simply use:
"(data + offset) * scale" to get final result.

see inline.

Applied to the togreg branch of iio.git with a tweaks to comments as mentioned (please check I didn't mess them up!)

Quoted 106 lines
Quoted 106 lines
Quoted 53 lines
---
  .../iio/common/hid-sensors/hid-sensor-attributes.c | 114 
+++++++++++++++++++++
  include/linux/hid-sensor-hub.h                     |   4 +
  2 files changed, 118 insertions(+)
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +34 -0
index 75b5473..451a95b 100644
@@ -26,6 +26,40 @@
  #include <linux/iio/iio.h>
  #include <linux/iio/sysfs.h>
 
+struct {
+    u32 usage_id;
+    int unit; /* 0 for default others from HID sensor spec */
+    int scale_val0; /* scale, whole number */
+    int scale_val1; /* scale, fraction in micros */
+} unit_conversion[] = {
+    {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
+    {HID_USAGE_SENSOR_ACCEL_3D,
+        HID_USAGE_SENSOR_UNITS_G, 9, 806650},
+
+    {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
+    {HID_USAGE_SENSOR_GYRO_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453},
+
+    {HID_USAGE_SENSOR_COMPASS_3D, 0, 1000, 0},
+    {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 
 0},
+
+    {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453},
+    {HID_USAGE_SENSOR_INCLINOMETER_3D,
+        HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
+
+    {HID_USAGE_SENSOR_ALS, 0, 1, 0},
+    {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
+
+    {HID_USAGE_SENSOR_PRESSURE, 0, 100000, 0},
+    {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0},
+};
+
  static int pow_10(unsigned power)
  {
      int i;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct · line 249

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +3 -0
@@ -209,6 +243,86 @@ int hid_sensor_write_raw_hyst_value(struct 
 hid_sensor_common *st,
  }
  EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
 
+/*
+ * This fuction applies the unit exponent to the scale.
+ * For example:

Function

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 255

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+ * 9.806650 ->exp:2-> val0[980]val1[6650]
+ * 9.000806 ->exp:2-> val0[900]val1[806]
+ * 0.174535 ->exp:2-> val0[17]val1[4535]
+ * 1.001745 ->exp:0-> val0[1]val1[1745]
+ * 1.001745 ->exp:2-> val0[100]val1[1745]
+ * 1.001745 ->exp:4-> val0[10017]val1[45]
+ * 9.806650 ->exp:-2-> val0[0]val1[98066]

It took me a while to get what you meant here, perhaps for the examples it is worth showing the actual value directly as well. Maybe. 9.806650 exp:2->980.6650 -> val0[980]val1[665000] (note the above is I think incorrect due to lack of trailing zeros on val1.

Quoted 6 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +6 -0
+ */
+static void adjust_exponent_micro(int *val0, int *val1, int scale0,
+                  int scale1, int exp)
+{
+    int i;
+    int x;

drivers/iio/common/hid-sensors/hid-sensor-attributes.c int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, · line 271

Quoted 10 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +10 -0
+    int res;
+    int rem;
+
+    if (exp > 0) {
+        *val0 = scale0 * pow_10(exp);
+        res = 0;
+        if (exp > 6) {
+            *val1 = 0;
+            return;
+        }

so if example 1 above..
val0 = 900 as desired.
i = 0;
sca1e1 = 806650
x = 806650/100000 = 8;
res = 80;
scale1 = 06650
i = 1
x = 06650/10000 = 0;
res = 80;
so 980. Good.

Quoted 7 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +7 -0
+        for (i = 0; i < exp; ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(exp - 1 - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val0 += res;
+            *val1 = scale1 * pow_10(exp);
Quoted 3 lines
Quoted 2 lines

val1 = 665000 which is correct with the trailing zeros in val2. So your code is good but examples need those zeros!

Impressive, Absolutely great review. I will correct comments in the next version.

Given this was the only issue in the patch set I'll just fix it up as I merge it.

It's been a couple of weeks an noone else has chipped in on this series, so I'm taking it now.

Quoted 77 lines

Thanks,
Srinivas

Quoted 73 lines
Quoted 67 lines
drivers/iio/common/hid-sensors/hid-sensor-attributes.c +47 -0
+    } else if (exp < 0) {
+        exp = abs(exp);
+        if (exp > 6) {
+            *val0 = *val1 = 0;
+            return;
+        }
+        *val0 = scale0 / pow_10(exp);
+        rem = scale0 % pow_10(exp);
+        res = 0;
+        for (i = 0; i < (6 - exp); ++i) {
+            x = scale1 / pow_10(5 - i);
+            res += (pow_10(5 - exp - i) * x);
+            scale1 = scale1 % pow_10(5 - i);
+        }
+        *val1 = rem * pow_10(6 - exp) + res;
+    } else {
+        *val0 = scale0;
+        *val1 = scale1;
+    }
+}
+
+int hid_sensor_format_scale(u32 usage_id,
+            struct hid_sensor_hub_attribute_info *attr_info,
+            int *val0, int *val1)
+{
+    int i;
+    int exp;
+
+    *val0 = 1;
+    *val1 = 0;
+
+    for (i = 0; ARRAY_SIZE(unit_conversion); ++i) {
+        if (unit_conversion[i].usage_id == usage_id &&
+            unit_conversion[i].unit == attr_info->units) {
+            exp  = hid_sensor_convert_exponent(
+                        attr_info->unit_expo);
+            adjust_exponent_micro(val0, val1,
+                    unit_conversion[i].scale_val0,
+                    unit_conversion[i].scale_val1, exp);
+            break;
+        }
+    }
+
+    return IIO_VAL_INT_PLUS_MICRO;
+}
+EXPORT_SYMBOL(hid_sensor_format_scale);
+
  int hid_sensor_parse_common_attributes(struct 
 hid_sensor_hub_device *hsdev,

u32 usage_id,
struct hid_sensor_common *st)

include/linux/hid-sensor-hub.h +4 -0
index b70cfd7..89626b2 100644
@@ -223,4 +223,8 @@ int hid_sensor_read_samp_freq_value(struct 
 hid_sensor_common *st,
  int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
                  u32 report_id, int field_index, u32 usage_id);
 
+int hid_sensor_format_scale(u32 usage_id,
+                struct hid_sensor_hub_attribute_info *attr_info,
+                int *val0, int *val1);
+
  #endif
Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-iio" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

author maintainer
#41 lore raw

Hi Jonathan,

I have just sent you one patch, which can be applied to iio git tree on top of the currently applied patches.

Thanks,
Srinivas

Quoted 183 lines · Jonathan Cameron

Cool. I will hold them for now then. Had a bored moment and not much else pending so jumped the gun!

On May 4, 2014 3:49:38 PM GMT+01:00, Srinivas Pandruvada <[email protected]> wrote:

    Hi Jonathan,

    Somebody pointed me some error in magnetometer calculation, I have a
    correction,
    I will submit as another patch. I was about to push second version of
    these patches.

    Thanks,
    Srinivas

    On 05/03/2014 12:24 PM, Jonathan Cameron wrote:

        On 26/04/14 20:34, Srinivas Pandruvada wrote:

            On 04/25/2014 11:30 AM, Jonathan Cameron wrote:

                On 19/04/14 00:22, Srinivas Pandruvada wrote:

                    HID sensor hub specify a default unit and
                    alternative units. This along with unit exponent
                    can be used adjust scale. This change change HID
                    sensor data units to IIO defined units for each
                    sensor type. So in this way user space can use a
                    simply use: "(data + offset) * scale" to get final
                    result. Signed-off-by: Srinivas Pandruvada
                    <[email protected]> 

                see inline. 

        Applied to the togreg branch of iio.git with a tweaks to
        comments as mentioned (please check I didn't mess them up!)

                    ---
                    .../iio/common/hid-sensors/hid-sensor-attributes.c
                    | 114 +++++++++++++++++++++
                    include/linux/hid-sensor-hub.h | 4 + 2 files
                    changed, 118 insertions(+) diff --git
                    a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
                    b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
                    index 75b5473..451a95b 100644 ---
                    a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
                    +++
                    b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
                    @@ -26,6 +26,40 @@ #include <linux/iio/iio.h>
                    #include <linux/iio/sysfs.h> +struct { + u32
                    usage_id; + int unit; /* 0 for default others from
                    HID sensor spec */ + int scale_val0; /* scale,
                    whole number */ + int scale_val1; /* scale,
                    fraction in micros */ +} unit_conversion[] = { +
                    {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650}, +
                    {HID_USAGE_SENSOR_ACCEL_3D, +
                    HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
                    + {HID_USAGE_SENSOR_ACCEL_3D, +
                    HID_USAGE_SENSOR_UNITS_G, 9, 806650}, + +
                    {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453}, +
                    {HID_USAGE_SENSOR_GYRO_3D, +
                    HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
                    + {HID_USAGE_SENSOR_GYRO_3D, +
                    HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0,
                    17453}, + + {HID_USAGE_SENSOR_COMPASS_3D, 0, 1000,
                    0}, + {HID_USAGE_SENSOR_COMPASS_3D,
                    HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0}, + +
                    {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 17453, 0}, +
                    {HID_USAGE_SENSOR_INCLINOMETER_3D, +
                    HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453}, +
                    {HID_USAGE_SENSOR_INCLINOMETER_3D, +
                    HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0}, + +
                    {HID_USAGE_SENSOR_ALS, 0, 1, 0}, +
                    {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX,
                    1, 0}, + + {HID_USAGE_SENSOR_PRESSURE, 0, 100000,
                    0}, + {HID_USAGE_SENSOR_PRESSURE,
                    HID_USAGE_SENSOR_UNITS_PASCAL, 1, 0}, +}; + static
                    int pow_10(unsigned power) { int i; @@ -209,6
                    +243,86 @@ int
                    hid_sensor_write_raw_hyst_value(struct
                    hid_sensor_common *st, }
                    EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
                    +/* + * This fuction applies the unit exponent to
                    the scale. + * For example: 

                Function

                    + * 9.806650 ->exp:2-> val0[980]val1[6650] + *
                    9.000806 ->exp:2-> val0[900]val1[806] + * 0.174535
                    ->exp:2-> val0[17]val1[4535] + * 1.001745
                    ->exp:0-> val0[1]val1[1745] + * 1.001745 ->exp:2->
                    val0[100]val1[1745] + * 1.001745 ->exp:4->
                    val0[10017]val1[45] + * 9.806650 ->exp:-2->
                    val0[0]val1[98066] 

                It took me a while to get what you meant here, perhaps
                for the examples it is worth showing the actual value
                directly as well. Maybe. 9.806650 exp:2->980.6650 ->
                val0[980]val1[665000] (note the above is I think
                incorrect due to lack of trailing zeros on val1.

                    + */ +static void adjust_exponent_micro(int *val0,
                    int *val1, int scale0, + int scale1, int exp) +{ +
                    int i; + int x; + int res; + int rem; + + if (exp
                    > 0) { + *val0 = scale0 * pow_10(exp); + res = 0;
                    + if (exp > 6) { + *val1 = 0; + return; + } 

                so if example 1 above.. val0 = 900 as desired. i = 0;
                sca1e1 = 806650 x = 806650/100000 = 8; res = 80;
                scale1 = 06650 i = 1 x = 06650/10000 = 0; res = 80; so
                980. Good.

                    + for (i = 0; i < exp; ++i) { + x = scale1 /
                    pow_10(5 - i); + res += (pow_10(exp - 1 - i) * x);
                    + scale1 = scale1 % pow_10(5 - i); + } + *val0 +=
                    res; + *val1 = scale1 * pow_10(exp); 

                val1 = 665000 which is correct with the trailing zeros
                in val2. So your code is good but examples need those
                zeros! 

            Impressive, Absolutely great review. I will correct
            comments in the next version. 

        Given this was the only issue in the patch set I'll just fix
        it up as I merge it. It's been a couple of weeks an noone else
        has chipped in on this series, so I'm taking it now.

            Thanks, Srinivas

                    + } else if (exp < 0) { + exp = abs(exp); + if
                    (exp > 6) { + *val0 = *val1 = 0; + return; + } +
                    *val0 = scale0 / pow_10(exp); + rem = scale0 %
                    pow_10(exp); + res = 0; + for (i = 0; i < (6 -
                    exp); ++i) { + x = scale1 / pow_10(5 - i); + res
                    += (pow_10(5 - exp - i) * x); + scale1 = scale1 %
                    pow_10(5 - i); + } + *val1 = rem * pow_10(6 - exp)
                    + res; + } else { + *val0 = scale0; + *val1 =
                    scale1; + } +} + +int hid_sensor_format_scale(u32
                    usage_id, + struct hid_sensor_hub_attribute_info
                    *attr_info, + int *val0, int *val1) +{ + int i; +
                    int exp; + + *val0 = 1; + *val1 = 0; + + for (i =
                    0; ARRAY_SIZE(unit_conversion); ++i) { + if
                    (unit_conversion[i].usage_id == usage_id && +
                    unit_conversion[i].unit == attr_info->units) { +
                    exp = hid_sensor_convert_exponent( +
                    attr_info->unit_expo); +
                    adjust_exponent_micro(val0, val1, +
                    unit_conversion[i].scale_val0, +
                    unit_conversion[i].scale_val1, exp); + break; + }
                    + } + + return IIO_VAL_INT_PLUS_MICRO; +}
                    +EXPORT_SYMBOL(hid_sensor_format_scale); + int
                    hid_sensor_parse_common_attributes(struct
                    hid_sensor_hub_device *hsdev, u32 usage_id, struct
                    hid_sensor_common *st) diff --git
                    a/include/linux/hid-sensor-hub.h
                    b/include/linux/hid-sensor-hub.h index
                    b70cfd7..89626b2 100644 ---
                    a/include/linux/hid-sensor-hub.h +++
                    b/include/linux/hid-sensor-hub.h @@ -223,4 +223,8
                    @@ int hid_sensor_read_samp_freq_value(struct
                    hid_sensor_common *st, int
                    hid_sensor_get_usage_index(struct
                    hid_sensor_hub_device *hsdev, u32 report_id, int
                    field_index, u32 usage_id); +int
                    hid_sensor_format_scale(u32 usage_id, + struct
                    hid_sensor_hub_attribute_info *attr_info, + int
                    *val0, int *val1); + #endif

                -- To unsubscribe from this list: send the line
                "unsubscribe linux-iio" in the body of a message to
                [email protected] More majordomo info at
                http://vger.kernel.org/majordomo-info.html



    --
    To unsubscribe from this list: send the line "unsubscribe linux-iio" in
    the body of a message to [email protected]
    More majordomo info athttp://vger.kernel.org/majordomo-info.html
Signature · 1 line
Sent from my Android phone with K-9 Mail. Please excuse my brevity.