linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/6] add timestamp channel for hid-sensors
@ 2021-01-05  7:21 Ye Xiang
  2021-01-05  7:21 ` [PATCH v3 1/6] iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor Ye Xiang
                   ` (6 more replies)
  0 siblings, 7 replies; 11+ messages in thread
From: Ye Xiang @ 2021-01-05  7:21 UTC (permalink / raw)
  To: jikos, jic23, srinivas.pandruvada
  Cc: linux-input, linux-iio, linux-kernel, Ye Xiang

This patch series add a timestamp channel for hid sensors,
including gravity sensor, gyro sensor, magnetometer sensor,
ambient light sensor, inclinometer sensor, and rotation sensor.

With this patch series, user can get the time when sensor yield
a sample.

---
v3:
  - hid-sensor-magn-3d: fix iio_val buffer len issue.
  - hid-sensor-accel-3d: refine commit message

v2:
  - remove unrelated changes.

Ye Xiang (6):
  iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor
  iio: hid-sensor-gyro-3d: Add timestamp channel
  iio: hid-sensor-als: Add timestamp channel
  iio: hid-sensor-magn-3d: Add timestamp channel
  iio: hid-sensor-incl-3d: Add timestamp channel
  iio: hid-sensor-rotation: Add timestamp channel

 drivers/iio/accel/hid-sensor-accel-3d.c       |  6 ++-
 drivers/iio/gyro/hid-sensor-gyro-3d.c         | 40 +++++++++-------
 drivers/iio/light/hid-sensor-als.c            | 39 ++++++++-------
 drivers/iio/magnetometer/hid-sensor-magn-3d.c | 48 ++++++++++++-------
 drivers/iio/orientation/hid-sensor-incl-3d.c  | 43 ++++++++++-------
 drivers/iio/orientation/hid-sensor-rotation.c | 46 ++++++++++--------
 6 files changed, 134 insertions(+), 88 deletions(-)

-- 
2.17.1


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

* [PATCH v3 1/6] iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor
  2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
@ 2021-01-05  7:21 ` Ye Xiang
  2021-01-05  7:21 ` [PATCH v3 2/6] iio: hid-sensor-gyro-3d: Add timestamp channel Ye Xiang
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Ye Xiang @ 2021-01-05  7:21 UTC (permalink / raw)
  To: jikos, jic23, srinivas.pandruvada
  Cc: linux-input, linux-iio, linux-kernel, Ye Xiang

The accel_3d sensor already has a timestamp channel, this patch just
replicate that for gravity sensor.

Signed-off-by: Ye Xiang <xiang.ye@intel.com>
---
 drivers/iio/accel/hid-sensor-accel-3d.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/accel/hid-sensor-accel-3d.c b/drivers/iio/accel/hid-sensor-accel-3d.c
index 589b7d8a36f2..7cd3cd81b1a7 100644
--- a/drivers/iio/accel/hid-sensor-accel-3d.c
+++ b/drivers/iio/accel/hid-sensor-accel-3d.c
@@ -23,6 +23,7 @@ enum accel_3d_channel {
 	ACCEL_3D_CHANNEL_MAX,
 };
 
+#define CHANNEL_SCAN_INDEX_TIMESTAMP ACCEL_3D_CHANNEL_MAX
 struct accel_3d_state {
 	struct hid_sensor_hub_callbacks callbacks;
 	struct hid_sensor_common common_attributes;
@@ -82,7 +83,7 @@ static const struct iio_chan_spec accel_3d_channels[] = {
 		BIT(IIO_CHAN_INFO_HYSTERESIS),
 		.scan_index = CHANNEL_SCAN_INDEX_Z,
 	},
-	IIO_CHAN_SOFT_TIMESTAMP(3)
+	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
 };
 
 /* Channel definitions */
@@ -117,7 +118,8 @@ static const struct iio_chan_spec gravity_channels[] = {
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
 		BIT(IIO_CHAN_INFO_HYSTERESIS),
 		.scan_index = CHANNEL_SCAN_INDEX_Z,
-	}
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP),
 };
 
 /* Adjust channel real bits based on report descriptor */
-- 
2.17.1


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

* [PATCH v3 2/6] iio: hid-sensor-gyro-3d: Add timestamp channel
  2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
  2021-01-05  7:21 ` [PATCH v3 1/6] iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor Ye Xiang
@ 2021-01-05  7:21 ` Ye Xiang
  2021-01-05  7:22 ` [PATCH v3 3/6] iio: hid-sensor-als: " Ye Xiang
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Ye Xiang @ 2021-01-05  7:21 UTC (permalink / raw)
  To: jikos, jic23, srinivas.pandruvada
  Cc: linux-input, linux-iio, linux-kernel, Ye Xiang

Each sample has a timestamp field with this change. This timestamp may
be from the sensor hub when present or local kernel timestamp. And the
unit of timestamp is nanosecond.

Signed-off-by: Ye Xiang <xiang.ye@intel.com>
---
 drivers/iio/gyro/hid-sensor-gyro-3d.c | 40 ++++++++++++++++-----------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/gyro/hid-sensor-gyro-3d.c b/drivers/iio/gyro/hid-sensor-gyro-3d.c
index c817d91b3b51..37c7ac0ad32b 100644
--- a/drivers/iio/gyro/hid-sensor-gyro-3d.c
+++ b/drivers/iio/gyro/hid-sensor-gyro-3d.c
@@ -23,15 +23,20 @@ enum gyro_3d_channel {
 	GYRO_3D_CHANNEL_MAX,
 };
 
+#define CHANNEL_SCAN_INDEX_TIMESTAMP GYRO_3D_CHANNEL_MAX
 struct gyro_3d_state {
 	struct hid_sensor_hub_callbacks callbacks;
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
-	u32 gyro_val[GYRO_3D_CHANNEL_MAX];
+	struct {
+		u32 gyro_val[GYRO_3D_CHANNEL_MAX];
+		u64 timestamp __aligned(8);
+	} scan;
 	int scale_pre_decml;
 	int scale_post_decml;
 	int scale_precision;
 	int value_offset;
+	s64 timestamp;
 };
 
 static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
@@ -79,7 +84,8 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
 		BIT(IIO_CHAN_INFO_HYSTERESIS),
 		.scan_index = CHANNEL_SCAN_INDEX_Z,
-	}
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
 };
 
 /* Adjust channel real bits based on report descriptor */
@@ -185,14 +191,6 @@ static const struct iio_info gyro_3d_info = {
 	.write_raw = &gyro_3d_write_raw,
 };
 
-/* Function to push data to buffer */
-static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
-	int len)
-{
-	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
-	iio_push_to_buffers(indio_dev, data);
-}
-
 /* Callback handler to send event after all samples are received and captured */
 static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 				unsigned usage_id,
@@ -202,10 +200,15 @@ static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
 
 	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));
+	if (atomic_read(&gyro_state->common_attributes.data_ready)) {
+		if (!gyro_state->timestamp)
+			gyro_state->timestamp = iio_get_time_ns(indio_dev);
+
+		iio_push_to_buffers_with_timestamp(indio_dev, &gyro_state->scan,
+						   gyro_state->timestamp);
+
+		gyro_state->timestamp = 0;
+	}
 
 	return 0;
 }
@@ -226,10 +229,15 @@ static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
 		offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
-		gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
-						*(u32 *)raw_data;
+		gyro_state->scan.gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
+				*(u32 *)raw_data;
 		ret = 0;
 	break;
+	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
+		gyro_state->timestamp =
+			hid_sensor_convert_timestamp(&gyro_state->common_attributes,
+						     *(s64 *)raw_data);
+	break;
 	default:
 		break;
 	}
-- 
2.17.1


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

* [PATCH v3 3/6] iio: hid-sensor-als: Add timestamp channel
  2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
  2021-01-05  7:21 ` [PATCH v3 1/6] iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor Ye Xiang
  2021-01-05  7:21 ` [PATCH v3 2/6] iio: hid-sensor-gyro-3d: Add timestamp channel Ye Xiang
@ 2021-01-05  7:22 ` Ye Xiang
  2021-01-05  7:22 ` [PATCH v3 4/6] iio: hid-sensor-magn-3d: " Ye Xiang
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Ye Xiang @ 2021-01-05  7:22 UTC (permalink / raw)
  To: jikos, jic23, srinivas.pandruvada
  Cc: linux-input, linux-iio, linux-kernel, Ye Xiang

Each sample has a timestamp field with this change. This timestamp may
be from the sensor hub when present or local kernel timestamp. And the
unit of timestamp is nanosecond.

Signed-off-by: Ye Xiang <xiang.ye@intel.com>
---
 drivers/iio/light/hid-sensor-als.c | 39 ++++++++++++++++++------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/light/hid-sensor-als.c b/drivers/iio/light/hid-sensor-als.c
index 5460a744dbd0..afcdb424bfb8 100644
--- a/drivers/iio/light/hid-sensor-als.c
+++ b/drivers/iio/light/hid-sensor-als.c
@@ -22,15 +22,21 @@ enum {
 	CHANNEL_SCAN_INDEX_MAX
 };
 
+#define CHANNEL_SCAN_INDEX_TIMESTAMP CHANNEL_SCAN_INDEX_MAX
+
 struct als_state {
 	struct hid_sensor_hub_callbacks callbacks;
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info als_illum;
-	u32 illum[CHANNEL_SCAN_INDEX_MAX];
+	struct {
+		u32 illum[CHANNEL_SCAN_INDEX_MAX];
+		u64 timestamp __aligned(8);
+	} scan;
 	int scale_pre_decml;
 	int scale_post_decml;
 	int scale_precision;
 	int value_offset;
+	s64 timestamp;
 };
 
 static const u32 als_sensitivity_addresses[] = {
@@ -59,7 +65,8 @@ static const struct iio_chan_spec als_channels[] = {
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
 		BIT(IIO_CHAN_INFO_HYSTERESIS),
 		.scan_index = CHANNEL_SCAN_INDEX_ILLUM,
-	}
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
 };
 
 /* Adjust channel real bits based on report descriptor */
@@ -173,14 +180,6 @@ static const struct iio_info als_info = {
 	.write_raw = &als_write_raw,
 };
 
-/* Function to push data to buffer */
-static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
-					int len)
-{
-	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
-	iio_push_to_buffers(indio_dev, data);
-}
-
 /* Callback handler to send event after all samples are received and captured */
 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
 				unsigned usage_id,
@@ -190,10 +189,14 @@ static int als_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct als_state *als_state = iio_priv(indio_dev);
 
 	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));
+	if (atomic_read(&als_state->common_attributes.data_ready)) {
+		if (!als_state->timestamp)
+			als_state->timestamp = iio_get_time_ns(indio_dev);
+
+		iio_push_to_buffers_with_timestamp(indio_dev, &als_state->scan,
+						   als_state->timestamp);
+		als_state->timestamp = 0;
+	}
 
 	return 0;
 }
@@ -211,10 +214,14 @@ static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
 
 	switch (usage_id) {
 	case HID_USAGE_SENSOR_LIGHT_ILLUM:
-		als_state->illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data;
-		als_state->illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_data;
+		als_state->scan.illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data;
+		als_state->scan.illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_data;
 		ret = 0;
 		break;
+	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
+		als_state->timestamp = hid_sensor_convert_timestamp(&als_state->common_attributes,
+								    *(s64 *)raw_data);
+		break;
 	default:
 		break;
 	}
-- 
2.17.1


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

* [PATCH v3 4/6] iio: hid-sensor-magn-3d: Add timestamp channel
  2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
                   ` (2 preceding siblings ...)
  2021-01-05  7:22 ` [PATCH v3 3/6] iio: hid-sensor-als: " Ye Xiang
@ 2021-01-05  7:22 ` Ye Xiang
  2021-01-05  7:22 ` [PATCH v3 5/6] iio: hid-sensor-incl-3d: " Ye Xiang
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Ye Xiang @ 2021-01-05  7:22 UTC (permalink / raw)
  To: jikos, jic23, srinivas.pandruvada
  Cc: linux-input, linux-iio, linux-kernel, Ye Xiang

Each sample has a timestamp field with this change. This timestamp may
be from the sensor hub when present or local kernel timestamp. And the
unit of timestamp is nanosecond.

Signed-off-by: Ye Xiang <xiang.ye@intel.com>
---
 drivers/iio/magnetometer/hid-sensor-magn-3d.c | 48 ++++++++++++-------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/drivers/iio/magnetometer/hid-sensor-magn-3d.c b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
index cacbd053b2c6..b79c8a5a98f9 100644
--- a/drivers/iio/magnetometer/hid-sensor-magn-3d.c
+++ b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
@@ -24,6 +24,7 @@ enum magn_3d_channel {
 	CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP,
 	CHANNEL_SCAN_INDEX_NORTH_MAGN,
 	CHANNEL_SCAN_INDEX_NORTH_TRUE,
+	CHANNEL_SCAN_INDEX_TIMESTAMP,
 	MAGN_3D_CHANNEL_MAX,
 };
 
@@ -47,6 +48,7 @@ struct magn_3d_state {
 
 	struct common_attributes magn_flux_attr;
 	struct common_attributes rot_attr;
+	s64 timestamp;
 };
 
 static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
@@ -57,6 +59,7 @@ static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
 	HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH,
 	HID_USAGE_SENSOR_ORIENT_MAGN_NORTH,
 	HID_USAGE_SENSOR_ORIENT_TRUE_NORTH,
+	HID_USAGE_SENSOR_TIME_TIMESTAMP,
 };
 
 static const u32 magn_3d_sensitivity_addresses[] = {
@@ -132,7 +135,8 @@ static const struct iio_chan_spec magn_3d_channels[] = {
 		BIT(IIO_CHAN_INFO_SCALE) |
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
 		BIT(IIO_CHAN_INFO_HYSTERESIS),
-	}
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(7)
 };
 
 /* Adjust channel real bits based on report descriptor */
@@ -281,13 +285,6 @@ static const struct iio_info magn_3d_info = {
 	.write_raw = &magn_3d_write_raw,
 };
 
-/* Function to push data to buffer */
-static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data)
-{
-	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
-	iio_push_to_buffers(indio_dev, data);
-}
-
 /* Callback handler to send event after all samples are received and captured */
 static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 				unsigned usage_id,
@@ -297,8 +294,15 @@ static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
 
 	dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n");
-	if (atomic_read(&magn_state->magn_flux_attributes.data_ready))
-		hid_sensor_push_data(indio_dev, magn_state->iio_vals);
+	if (atomic_read(&magn_state->magn_flux_attributes.data_ready)) {
+		if (!magn_state->timestamp)
+			magn_state->timestamp = iio_get_time_ns(indio_dev);
+
+		iio_push_to_buffers_with_timestamp(indio_dev,
+						   magn_state->iio_vals,
+						   magn_state->timestamp);
+		magn_state->timestamp = 0;
+	}
 
 	return 0;
 }
@@ -329,6 +333,11 @@ static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
 		offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH)
 				+ CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP;
 	break;
+	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
+		magn_state->timestamp =
+			hid_sensor_convert_timestamp(&magn_state->magn_flux_attributes,
+						     *(s64 *)raw_data);
+		return ret;
 	default:
 		return -EINVAL;
 	}
@@ -394,9 +403,10 @@ static int magn_3d_parse_report(struct platform_device *pdev,
 		return -ENOMEM;
 	}
 
-	st->iio_vals = devm_kcalloc(&pdev->dev, attr_count,
-				sizeof(u32),
-				GFP_KERNEL);
+	/* attr_count include timestamp channel, and the iio_vals should be aligned to 8byte */
+	st->iio_vals = devm_kcalloc(&pdev->dev,
+				    ((attr_count + 1) % 2 + (attr_count + 1) / 2) * 2,
+				    sizeof(u32), GFP_KERNEL);
 	if (!st->iio_vals) {
 		dev_err(&pdev->dev,
 			"failed to allocate space for iio values array\n");
@@ -412,11 +422,13 @@ static int magn_3d_parse_report(struct platform_device *pdev,
 			(_channels[*chan_count]).scan_index = *chan_count;
 			(_channels[*chan_count]).address = i;
 
-			/* Set magn_val_addr to iio value address */
-			st->magn_val_addr[i] = &(st->iio_vals[*chan_count]);
-			magn_3d_adjust_channel_bit_mask(_channels,
-							*chan_count,
-							st->magn[i].size);
+			if (i != CHANNEL_SCAN_INDEX_TIMESTAMP) {
+				/* Set magn_val_addr to iio value address */
+				st->magn_val_addr[i] = &st->iio_vals[*chan_count];
+				magn_3d_adjust_channel_bit_mask(_channels,
+								*chan_count,
+								st->magn[i].size);
+			}
 			(*chan_count)++;
 		}
 	}
-- 
2.17.1


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

* [PATCH v3 5/6] iio: hid-sensor-incl-3d: Add timestamp channel
  2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
                   ` (3 preceding siblings ...)
  2021-01-05  7:22 ` [PATCH v3 4/6] iio: hid-sensor-magn-3d: " Ye Xiang
@ 2021-01-05  7:22 ` Ye Xiang
  2021-01-05  7:22 ` [PATCH v3 6/6] iio: hid-sensor-rotation: " Ye Xiang
  2021-01-05  8:53 ` [PATCH v3 0/6] add timestamp channel for hid-sensors Srinivas Pandruvada
  6 siblings, 0 replies; 11+ messages in thread
From: Ye Xiang @ 2021-01-05  7:22 UTC (permalink / raw)
  To: jikos, jic23, srinivas.pandruvada
  Cc: linux-input, linux-iio, linux-kernel, Ye Xiang

Each sample has a timestamp field with this change. This timestamp may
be from the sensor hub when present or local kernel timestamp. And the
unit of timestamp is nanosecond.

Signed-off-by: Ye Xiang <xiang.ye@intel.com>
---
 drivers/iio/orientation/hid-sensor-incl-3d.c | 43 ++++++++++++--------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/orientation/hid-sensor-incl-3d.c b/drivers/iio/orientation/hid-sensor-incl-3d.c
index df2fe91e8117..3baa52fbb426 100644
--- a/drivers/iio/orientation/hid-sensor-incl-3d.c
+++ b/drivers/iio/orientation/hid-sensor-incl-3d.c
@@ -24,15 +24,21 @@ enum incl_3d_channel {
 	INCLI_3D_CHANNEL_MAX,
 };
 
+#define CHANNEL_SCAN_INDEX_TIMESTAMP INCLI_3D_CHANNEL_MAX
+
 struct incl_3d_state {
 	struct hid_sensor_hub_callbacks callbacks;
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX];
-	u32 incl_val[INCLI_3D_CHANNEL_MAX];
+	struct {
+		u32 incl_val[INCLI_3D_CHANNEL_MAX];
+		u64 timestamp __aligned(8);
+	} scan;
 	int scale_pre_decml;
 	int scale_post_decml;
 	int scale_precision;
 	int value_offset;
+	s64 timestamp;
 };
 
 static const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = {
@@ -81,7 +87,8 @@ static const struct iio_chan_spec incl_3d_channels[] = {
 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
 		BIT(IIO_CHAN_INFO_HYSTERESIS),
 		.scan_index = CHANNEL_SCAN_INDEX_Z,
-	}
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP),
 };
 
 /* Adjust channel real bits based on report descriptor */
@@ -186,13 +193,6 @@ static const struct iio_info incl_3d_info = {
 	.write_raw = &incl_3d_write_raw,
 };
 
-/* Function to push data to buffer */
-static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
-{
-	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
-	iio_push_to_buffers(indio_dev, (u8 *)data);
-}
-
 /* Callback handler to send event after all samples are received and captured */
 static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 				unsigned usage_id,
@@ -202,10 +202,16 @@ static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct incl_3d_state *incl_state = iio_priv(indio_dev);
 
 	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));
+	if (atomic_read(&incl_state->common_attributes.data_ready)) {
+		if (!incl_state->timestamp)
+			incl_state->timestamp = iio_get_time_ns(indio_dev);
+
+		iio_push_to_buffers_with_timestamp(indio_dev,
+						   &incl_state->scan,
+						   incl_state->timestamp);
+
+		incl_state->timestamp = 0;
+	}
 
 	return 0;
 }
@@ -222,13 +228,18 @@ static int incl_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
 
 	switch (usage_id) {
 	case HID_USAGE_SENSOR_ORIENT_TILT_X:
-		incl_state->incl_val[CHANNEL_SCAN_INDEX_X] = *(u32 *)raw_data;
+		incl_state->scan.incl_val[CHANNEL_SCAN_INDEX_X] = *(u32 *)raw_data;
 	break;
 	case HID_USAGE_SENSOR_ORIENT_TILT_Y:
-		incl_state->incl_val[CHANNEL_SCAN_INDEX_Y] = *(u32 *)raw_data;
+		incl_state->scan.incl_val[CHANNEL_SCAN_INDEX_Y] = *(u32 *)raw_data;
 	break;
 	case HID_USAGE_SENSOR_ORIENT_TILT_Z:
-		incl_state->incl_val[CHANNEL_SCAN_INDEX_Z] = *(u32 *)raw_data;
+		incl_state->scan.incl_val[CHANNEL_SCAN_INDEX_Z] = *(u32 *)raw_data;
+	break;
+	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
+		incl_state->timestamp =
+			hid_sensor_convert_timestamp(&incl_state->common_attributes,
+						     *(s64 *)raw_data);
 	break;
 	default:
 		ret = -EINVAL;
-- 
2.17.1


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

* [PATCH v3 6/6] iio: hid-sensor-rotation: Add timestamp channel
  2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
                   ` (4 preceding siblings ...)
  2021-01-05  7:22 ` [PATCH v3 5/6] iio: hid-sensor-incl-3d: " Ye Xiang
@ 2021-01-05  7:22 ` Ye Xiang
  2021-01-05  8:53 ` [PATCH v3 0/6] add timestamp channel for hid-sensors Srinivas Pandruvada
  6 siblings, 0 replies; 11+ messages in thread
From: Ye Xiang @ 2021-01-05  7:22 UTC (permalink / raw)
  To: jikos, jic23, srinivas.pandruvada
  Cc: linux-input, linux-iio, linux-kernel, Ye Xiang

Each sample has a timestamp field with this change. This timestamp may
be from the sensor hub when present or local kernel timestamp. And the
unit of timestamp is nanosecond.

Signed-off-by: Ye Xiang <xiang.ye@intel.com>
---
 drivers/iio/orientation/hid-sensor-rotation.c | 46 +++++++++++--------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/drivers/iio/orientation/hid-sensor-rotation.c b/drivers/iio/orientation/hid-sensor-rotation.c
index 665ccf3673cc..b0245b3b7ffc 100644
--- a/drivers/iio/orientation/hid-sensor-rotation.c
+++ b/drivers/iio/orientation/hid-sensor-rotation.c
@@ -20,11 +20,15 @@ struct dev_rot_state {
 	struct hid_sensor_hub_callbacks callbacks;
 	struct hid_sensor_common common_attributes;
 	struct hid_sensor_hub_attribute_info quaternion;
-	u32 sampled_vals[4];
+	struct {
+		u32 sampled_vals[4] __aligned(16);
+		u64 timestamp __aligned(8);
+	} scan;
 	int scale_pre_decml;
 	int scale_post_decml;
 	int scale_precision;
 	int value_offset;
+	s64 timestamp;
 };
 
 static const u32 rotation_sensitivity_addresses[] = {
@@ -42,8 +46,10 @@ static const struct iio_chan_spec dev_rot_channels[] = {
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
 					BIT(IIO_CHAN_INFO_OFFSET) |
 					BIT(IIO_CHAN_INFO_SCALE) |
-					BIT(IIO_CHAN_INFO_HYSTERESIS)
-	}
+					BIT(IIO_CHAN_INFO_HYSTERESIS),
+		.scan_index = 0
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(1)
 };
 
 /* Adjust channel real bits based on report descriptor */
@@ -75,7 +81,7 @@ static int dev_rot_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_RAW:
 		if (size >= 4) {
 			for (i = 0; i < 4; ++i)
-				vals[i] = rot_state->sampled_vals[i];
+				vals[i] = rot_state->scan.sampled_vals[i];
 			ret_type = IIO_VAL_INT_MULTIPLE;
 			*val_len =  4;
 		} else
@@ -137,15 +143,6 @@ static const struct iio_info dev_rot_info = {
 	.write_raw = &dev_rot_write_raw,
 };
 
-/* Function to push data to buffer */
-static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
-{
-	dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n");
-	iio_push_to_buffers(indio_dev, (u8 *)data);
-	dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n");
-
-}
-
 /* Callback handler to send event after all samples are received and captured */
 static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
 				unsigned usage_id,
@@ -155,10 +152,15 @@ static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
 
 	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));
+	if (atomic_read(&rot_state->common_attributes.data_ready)) {
+		if (!rot_state->timestamp)
+			rot_state->timestamp = iio_get_time_ns(indio_dev);
+
+		iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan,
+						   rot_state->timestamp);
+
+		rot_state->timestamp = 0;
+	}
 
 	return 0;
 }
@@ -173,10 +175,14 @@ static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
 
 	if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
-		memcpy(rot_state->sampled_vals, raw_data,
-					sizeof(rot_state->sampled_vals));
+		memcpy(&rot_state->scan.sampled_vals, raw_data,
+		       sizeof(rot_state->scan.sampled_vals));
+
 		dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
-					sizeof(rot_state->sampled_vals));
+			sizeof(rot_state->scan.sampled_vals));
+	} else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) {
+		rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes,
+								    *(s64 *)raw_data);
 	}
 
 	return 0;
-- 
2.17.1


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

* Re: [PATCH v3 0/6] add timestamp channel for hid-sensors
  2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
                   ` (5 preceding siblings ...)
  2021-01-05  7:22 ` [PATCH v3 6/6] iio: hid-sensor-rotation: " Ye Xiang
@ 2021-01-05  8:53 ` Srinivas Pandruvada
  2021-01-05  9:26   ` Ye, Xiang
  6 siblings, 1 reply; 11+ messages in thread
From: Srinivas Pandruvada @ 2021-01-05  8:53 UTC (permalink / raw)
  To: Ye Xiang, jikos, jic23; +Cc: linux-input, linux-iio, linux-kernel

On Tue, 2021-01-05 at 15:21 +0800, Ye Xiang wrote:
> This patch series add a timestamp channel for hid sensors,
> including gravity sensor, gyro sensor, magnetometer sensor,
> ambient light sensor, inclinometer sensor, and rotation sensor.
> 
> With this patch series, user can get the time when sensor yield
> a sample.
> 
I think this series is v1 for upstream not v3.

Thanks,
Srinivas

> ---
> v3:
>   - hid-sensor-magn-3d: fix iio_val buffer len issue.
>   - hid-sensor-accel-3d: refine commit message
> 
> v2:
>   - remove unrelated changes.
> 
> Ye Xiang (6):
>   iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor
>   iio: hid-sensor-gyro-3d: Add timestamp channel
>   iio: hid-sensor-als: Add timestamp channel
>   iio: hid-sensor-magn-3d: Add timestamp channel
>   iio: hid-sensor-incl-3d: Add timestamp channel
>   iio: hid-sensor-rotation: Add timestamp channel
> 
>  drivers/iio/accel/hid-sensor-accel-3d.c       |  6 ++-
>  drivers/iio/gyro/hid-sensor-gyro-3d.c         | 40 +++++++++-------
>  drivers/iio/light/hid-sensor-als.c            | 39 ++++++++-------
>  drivers/iio/magnetometer/hid-sensor-magn-3d.c | 48 ++++++++++++-----
> --
>  drivers/iio/orientation/hid-sensor-incl-3d.c  | 43 ++++++++++-------
>  drivers/iio/orientation/hid-sensor-rotation.c | 46 ++++++++++-------
> -
>  6 files changed, 134 insertions(+), 88 deletions(-)
> 



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

* Re: [PATCH v3 0/6] add timestamp channel for hid-sensors
  2021-01-05  8:53 ` [PATCH v3 0/6] add timestamp channel for hid-sensors Srinivas Pandruvada
@ 2021-01-05  9:26   ` Ye, Xiang
  2021-01-09 19:28     ` Jonathan Cameron
  0 siblings, 1 reply; 11+ messages in thread
From: Ye, Xiang @ 2021-01-05  9:26 UTC (permalink / raw)
  To: Srinivas Pandruvada; +Cc: jikos, jic23, linux-input, linux-iio, linux-kernel

On Tue, Jan 05, 2021 at 12:53:44AM -0800, Srinivas Pandruvada wrote:
> On Tue, 2021-01-05 at 15:21 +0800, Ye Xiang wrote:
> > This patch series add a timestamp channel for hid sensors,
> > including gravity sensor, gyro sensor, magnetometer sensor,
> > ambient light sensor, inclinometer sensor, and rotation sensor.
> > 
> > With this patch series, user can get the time when sensor yield
> > a sample.
> > 
> I think this series is v1 for upstream not v3.
Sorry, it's v1 for upstream. will resent it as v1. v3 is our internal review version.

Thanks
Ye Xiang
> 
> 
> > ---
> > v3:
> >   - hid-sensor-magn-3d: fix iio_val buffer len issue.
> >   - hid-sensor-accel-3d: refine commit message
> > 
> > v2:
> >   - remove unrelated changes.
> > 
> > Ye Xiang (6):
> >   iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor
> >   iio: hid-sensor-gyro-3d: Add timestamp channel
> >   iio: hid-sensor-als: Add timestamp channel
> >   iio: hid-sensor-magn-3d: Add timestamp channel
> >   iio: hid-sensor-incl-3d: Add timestamp channel
> >   iio: hid-sensor-rotation: Add timestamp channel
> > 
> >  drivers/iio/accel/hid-sensor-accel-3d.c       |  6 ++-
> >  drivers/iio/gyro/hid-sensor-gyro-3d.c         | 40 +++++++++-------
> >  drivers/iio/light/hid-sensor-als.c            | 39 ++++++++-------
> >  drivers/iio/magnetometer/hid-sensor-magn-3d.c | 48 ++++++++++++-----
> > --
> >  drivers/iio/orientation/hid-sensor-incl-3d.c  | 43 ++++++++++-------
> >  drivers/iio/orientation/hid-sensor-rotation.c | 46 ++++++++++-------
> > -
> >  6 files changed, 134 insertions(+), 88 deletions(-)
> > 
> 
> 

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

* Re: [PATCH v3 0/6] add timestamp channel for hid-sensors
  2021-01-05  9:26   ` Ye, Xiang
@ 2021-01-09 19:28     ` Jonathan Cameron
  2021-01-10  7:11       ` Ye, Xiang
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Cameron @ 2021-01-09 19:28 UTC (permalink / raw)
  To: Ye, Xiang
  Cc: Srinivas Pandruvada, jikos, linux-input, linux-iio, linux-kernel

On Tue, 5 Jan 2021 17:26:33 +0800
"Ye, Xiang" <xiang.ye@intel.com> wrote:

> On Tue, Jan 05, 2021 at 12:53:44AM -0800, Srinivas Pandruvada wrote:
> > On Tue, 2021-01-05 at 15:21 +0800, Ye Xiang wrote:  
> > > This patch series add a timestamp channel for hid sensors,
> > > including gravity sensor, gyro sensor, magnetometer sensor,
> > > ambient light sensor, inclinometer sensor, and rotation sensor.
> > > 
> > > With this patch series, user can get the time when sensor yield
> > > a sample.
> > >   
> > I think this series is v1 for upstream not v3.  
> Sorry, it's v1 for upstream. will resent it as v1. v3 is our internal review version.
> 
Future notice, if you make a mistake of this particular type - don't resend and
carry on with future version numbers.  Otherwise it gets really confusing
if we get to a public v3 version!   Monotonic version numbers only :)

Not a bit problem though but if that does happen check I don't grab the wrong
version.

Jonathan

> Thanks
> Ye Xiang
> > 
> >   
> > > ---
> > > v3:
> > >   - hid-sensor-magn-3d: fix iio_val buffer len issue.
> > >   - hid-sensor-accel-3d: refine commit message
> > > 
> > > v2:
> > >   - remove unrelated changes.
> > > 
> > > Ye Xiang (6):
> > >   iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor
> > >   iio: hid-sensor-gyro-3d: Add timestamp channel
> > >   iio: hid-sensor-als: Add timestamp channel
> > >   iio: hid-sensor-magn-3d: Add timestamp channel
> > >   iio: hid-sensor-incl-3d: Add timestamp channel
> > >   iio: hid-sensor-rotation: Add timestamp channel
> > > 
> > >  drivers/iio/accel/hid-sensor-accel-3d.c       |  6 ++-
> > >  drivers/iio/gyro/hid-sensor-gyro-3d.c         | 40 +++++++++-------
> > >  drivers/iio/light/hid-sensor-als.c            | 39 ++++++++-------
> > >  drivers/iio/magnetometer/hid-sensor-magn-3d.c | 48 ++++++++++++-----
> > > --
> > >  drivers/iio/orientation/hid-sensor-incl-3d.c  | 43 ++++++++++-------
> > >  drivers/iio/orientation/hid-sensor-rotation.c | 46 ++++++++++-------
> > > -
> > >  6 files changed, 134 insertions(+), 88 deletions(-)
> > >   
> > 
> >   


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

* Re: [PATCH v3 0/6] add timestamp channel for hid-sensors
  2021-01-09 19:28     ` Jonathan Cameron
@ 2021-01-10  7:11       ` Ye, Xiang
  0 siblings, 0 replies; 11+ messages in thread
From: Ye, Xiang @ 2021-01-10  7:11 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Srinivas Pandruvada, jikos, linux-input, linux-iio, linux-kernel

On Sat, Jan 09, 2021 at 07:28:05PM +0000, Jonathan Cameron wrote:
> On Tue, 5 Jan 2021 17:26:33 +0800
> "Ye, Xiang" <xiang.ye@intel.com> wrote:
> 
> > On Tue, Jan 05, 2021 at 12:53:44AM -0800, Srinivas Pandruvada wrote:
> > > On Tue, 2021-01-05 at 15:21 +0800, Ye Xiang wrote:  
> > > > This patch series add a timestamp channel for hid sensors,
> > > > including gravity sensor, gyro sensor, magnetometer sensor,
> > > > ambient light sensor, inclinometer sensor, and rotation sensor.
> > > > 
> > > > With this patch series, user can get the time when sensor yield
> > > > a sample.
> > > >   
> > > I think this series is v1 for upstream not v3.  
> > Sorry, it's v1 for upstream. will resent it as v1. v3 is our internal review version.
> > 
> Future notice, if you make a mistake of this particular type - don't resend and
> carry on with future version numbers.  Otherwise it gets really confusing
> if we get to a public v3 version!   Monotonic version numbers only :)
> 
> Not a bit problem though but if that does happen check I don't grab the wrong
> version.

Got it. you grabed the right version v1 for test. thanks.

Thanks
Ye Xiang

> 
> > > 
> > >   
> > > > ---
> > > > v3:
> > > >   - hid-sensor-magn-3d: fix iio_val buffer len issue.
> > > >   - hid-sensor-accel-3d: refine commit message
> > > > 
> > > > v2:
> > > >   - remove unrelated changes.
> > > > 
> > > > Ye Xiang (6):
> > > >   iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor
> > > >   iio: hid-sensor-gyro-3d: Add timestamp channel
> > > >   iio: hid-sensor-als: Add timestamp channel
> > > >   iio: hid-sensor-magn-3d: Add timestamp channel
> > > >   iio: hid-sensor-incl-3d: Add timestamp channel
> > > >   iio: hid-sensor-rotation: Add timestamp channel
> > > > 
> > > >  drivers/iio/accel/hid-sensor-accel-3d.c       |  6 ++-
> > > >  drivers/iio/gyro/hid-sensor-gyro-3d.c         | 40 +++++++++-------
> > > >  drivers/iio/light/hid-sensor-als.c            | 39 ++++++++-------
> > > >  drivers/iio/magnetometer/hid-sensor-magn-3d.c | 48 ++++++++++++-----
> > > > --
> > > >  drivers/iio/orientation/hid-sensor-incl-3d.c  | 43 ++++++++++-------
> > > >  drivers/iio/orientation/hid-sensor-rotation.c | 46 ++++++++++-------
> > > > -
> > > >  6 files changed, 134 insertions(+), 88 deletions(-)
> > > >   
> > > 
> > >   
> 

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

end of thread, other threads:[~2021-01-10  7:11 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-05  7:21 [PATCH v3 0/6] add timestamp channel for hid-sensors Ye Xiang
2021-01-05  7:21 ` [PATCH v3 1/6] iio: hid-sensor-accel-3d: Add timestamp channel for gravity sensor Ye Xiang
2021-01-05  7:21 ` [PATCH v3 2/6] iio: hid-sensor-gyro-3d: Add timestamp channel Ye Xiang
2021-01-05  7:22 ` [PATCH v3 3/6] iio: hid-sensor-als: " Ye Xiang
2021-01-05  7:22 ` [PATCH v3 4/6] iio: hid-sensor-magn-3d: " Ye Xiang
2021-01-05  7:22 ` [PATCH v3 5/6] iio: hid-sensor-incl-3d: " Ye Xiang
2021-01-05  7:22 ` [PATCH v3 6/6] iio: hid-sensor-rotation: " Ye Xiang
2021-01-05  8:53 ` [PATCH v3 0/6] add timestamp channel for hid-sensors Srinivas Pandruvada
2021-01-05  9:26   ` Ye, Xiang
2021-01-09 19:28     ` Jonathan Cameron
2021-01-10  7:11       ` Ye, Xiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).