iio: hid-sensor-gyro-3d: Add timestamp channel
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> Link: https://lore.kernel.org/r/20210105093515.19135-3-xiang.ye@intel.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
parent
4c2617207e
commit
4648cbd8fb
@ -23,15 +23,20 @@ enum gyro_3d_channel {
|
|||||||
GYRO_3D_CHANNEL_MAX,
|
GYRO_3D_CHANNEL_MAX,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define CHANNEL_SCAN_INDEX_TIMESTAMP GYRO_3D_CHANNEL_MAX
|
||||||
struct gyro_3d_state {
|
struct gyro_3d_state {
|
||||||
struct hid_sensor_hub_callbacks callbacks;
|
struct hid_sensor_hub_callbacks callbacks;
|
||||||
struct hid_sensor_common common_attributes;
|
struct hid_sensor_common common_attributes;
|
||||||
struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
|
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_pre_decml;
|
||||||
int scale_post_decml;
|
int scale_post_decml;
|
||||||
int scale_precision;
|
int scale_precision;
|
||||||
int value_offset;
|
int value_offset;
|
||||||
|
s64 timestamp;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
|
static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
|
||||||
@ -72,7 +77,8 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
|
|||||||
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
|
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
|
||||||
BIT(IIO_CHAN_INFO_HYSTERESIS),
|
BIT(IIO_CHAN_INFO_HYSTERESIS),
|
||||||
.scan_index = CHANNEL_SCAN_INDEX_Z,
|
.scan_index = CHANNEL_SCAN_INDEX_Z,
|
||||||
}
|
},
|
||||||
|
IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Adjust channel real bits based on report descriptor */
|
/* Adjust channel real bits based on report descriptor */
|
||||||
@ -178,14 +184,6 @@ static const struct iio_info gyro_3d_info = {
|
|||||||
.write_raw = &gyro_3d_write_raw,
|
.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 */
|
/* Callback handler to send event after all samples are received and captured */
|
||||||
static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
|
static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
|
||||||
unsigned usage_id,
|
unsigned usage_id,
|
||||||
@ -195,10 +193,15 @@ static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
|
|||||||
struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
|
struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
|
||||||
|
|
||||||
dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
|
dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
|
||||||
if (atomic_read(&gyro_state->common_attributes.data_ready))
|
if (atomic_read(&gyro_state->common_attributes.data_ready)) {
|
||||||
hid_sensor_push_data(indio_dev,
|
if (!gyro_state->timestamp)
|
||||||
gyro_state->gyro_val,
|
gyro_state->timestamp = iio_get_time_ns(indio_dev);
|
||||||
sizeof(gyro_state->gyro_val));
|
|
||||||
|
iio_push_to_buffers_with_timestamp(indio_dev, &gyro_state->scan,
|
||||||
|
gyro_state->timestamp);
|
||||||
|
|
||||||
|
gyro_state->timestamp = 0;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -219,10 +222,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_Y_AXIS:
|
||||||
case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
|
case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
|
||||||
offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
|
offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
|
||||||
gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
|
gyro_state->scan.gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
|
||||||
*(u32 *)raw_data;
|
*(u32 *)raw_data;
|
||||||
ret = 0;
|
ret = 0;
|
||||||
break;
|
break;
|
||||||
|
case HID_USAGE_SENSOR_TIME_TIMESTAMP:
|
||||||
|
gyro_state->timestamp =
|
||||||
|
hid_sensor_convert_timestamp(&gyro_state->common_attributes,
|
||||||
|
*(s64 *)raw_data);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user