Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Ye Xiang and committed by
Jonathan Cameron
4648cbd8 4c261720

+24 -16
+24 -16
drivers/iio/gyro/hid-sensor-gyro-3d.c
··· 23 23 GYRO_3D_CHANNEL_MAX, 24 24 }; 25 25 26 + #define CHANNEL_SCAN_INDEX_TIMESTAMP GYRO_3D_CHANNEL_MAX 26 27 struct gyro_3d_state { 27 28 struct hid_sensor_hub_callbacks callbacks; 28 29 struct hid_sensor_common common_attributes; 29 30 struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX]; 30 - u32 gyro_val[GYRO_3D_CHANNEL_MAX]; 31 + struct { 32 + u32 gyro_val[GYRO_3D_CHANNEL_MAX]; 33 + u64 timestamp __aligned(8); 34 + } scan; 31 35 int scale_pre_decml; 32 36 int scale_post_decml; 33 37 int scale_precision; 34 38 int value_offset; 39 + s64 timestamp; 35 40 }; 36 41 37 42 static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = { ··· 77 72 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 78 73 BIT(IIO_CHAN_INFO_HYSTERESIS), 79 74 .scan_index = CHANNEL_SCAN_INDEX_Z, 80 - } 75 + }, 76 + IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP) 81 77 }; 82 78 83 79 /* Adjust channel real bits based on report descriptor */ ··· 184 178 .write_raw = &gyro_3d_write_raw, 185 179 }; 186 180 187 - /* Function to push data to buffer */ 188 - static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data, 189 - int len) 190 - { 191 - dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 192 - iio_push_to_buffers(indio_dev, data); 193 - } 194 - 195 181 /* Callback handler to send event after all samples are received and captured */ 196 182 static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev, 197 183 unsigned usage_id, ··· 193 195 struct gyro_3d_state *gyro_state = iio_priv(indio_dev); 194 196 195 197 dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n"); 196 - if (atomic_read(&gyro_state->common_attributes.data_ready)) 197 - hid_sensor_push_data(indio_dev, 198 - gyro_state->gyro_val, 199 - sizeof(gyro_state->gyro_val)); 198 + if (atomic_read(&gyro_state->common_attributes.data_ready)) { 199 + if (!gyro_state->timestamp) 200 + gyro_state->timestamp = iio_get_time_ns(indio_dev); 201 + 202 + iio_push_to_buffers_with_timestamp(indio_dev, &gyro_state->scan, 203 + gyro_state->timestamp); 204 + 205 + gyro_state->timestamp = 0; 206 + } 200 207 201 208 return 0; 202 209 } ··· 222 219 case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS: 223 220 case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS: 224 221 offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS; 225 - gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] = 226 - *(u32 *)raw_data; 222 + gyro_state->scan.gyro_val[CHANNEL_SCAN_INDEX_X + offset] = 223 + *(u32 *)raw_data; 227 224 ret = 0; 225 + break; 226 + case HID_USAGE_SENSOR_TIME_TIMESTAMP: 227 + gyro_state->timestamp = 228 + hid_sensor_convert_timestamp(&gyro_state->common_attributes, 229 + *(s64 *)raw_data); 228 230 break; 229 231 default: 230 232 break;