| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Chrome OS EC MEMS Sensor Hub driver. |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | */ |
| 7 | |
| 8 | #ifndef __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H |
| 9 | #define __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H |
| 10 | |
| 11 | #include <linux/ktime.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/notifier.h> |
| 14 | #include <linux/platform_data/cros_ec_commands.h> |
| 15 | |
| 16 | struct iio_dev; |
| 17 | |
| 18 | /** |
| 19 | * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information. |
| 20 | * @sensor_num: Id of the sensor, as reported by the EC. |
| 21 | */ |
| 22 | struct cros_ec_sensor_platform { |
| 23 | u8 sensor_num; |
| 24 | }; |
| 25 | |
| 26 | /** |
| 27 | * typedef cros_ec_sensorhub_push_data_cb_t - Callback function to send datum |
| 28 | * to specific sensors. |
| 29 | * |
| 30 | * @indio_dev: The IIO device that will process the sample. |
| 31 | * @data: Vector array of the ring sample. |
| 32 | * @timestamp: Timestamp in host timespace when the sample was acquired by |
| 33 | * the EC. |
| 34 | */ |
| 35 | typedef int (*cros_ec_sensorhub_push_data_cb_t)(struct iio_dev *indio_dev, |
| 36 | s16 *data, |
| 37 | s64 timestamp); |
| 38 | |
| 39 | struct cros_ec_sensorhub_sensor_push_data { |
| 40 | struct iio_dev *indio_dev; |
| 41 | cros_ec_sensorhub_push_data_cb_t push_data_cb; |
| 42 | }; |
| 43 | |
| 44 | enum { |
| 45 | CROS_EC_SENSOR_LAST_TS, |
| 46 | CROS_EC_SENSOR_NEW_TS, |
| 47 | CROS_EC_SENSOR_ALL_TS |
| 48 | }; |
| 49 | |
| 50 | struct cros_ec_sensors_ring_sample { |
| 51 | u8 sensor_id; |
| 52 | u8 flag; |
| 53 | s16 vector[3]; |
| 54 | s64 timestamp; |
| 55 | } __packed; |
| 56 | |
| 57 | /* State used for cros_ec_ring_fix_overflow */ |
| 58 | struct cros_ec_sensors_ec_overflow_state { |
| 59 | s64 offset; |
| 60 | s64 last; |
| 61 | }; |
| 62 | |
| 63 | /* Length of the filter, how long to remember entries for */ |
| 64 | #define CROS_EC_SENSORHUB_TS_HISTORY_SIZE 64 |
| 65 | |
| 66 | /** |
| 67 | * struct cros_ec_sensors_ts_filter_state - Timestamp filetr state. |
| 68 | * |
| 69 | * @x_offset: x is EC interrupt time. x_offset its last value. |
| 70 | * @y_offset: y is the difference between AP and EC time, y_offset its last |
| 71 | * value. |
| 72 | * @x_history: The past history of x, relative to x_offset. |
| 73 | * @y_history: The past history of y, relative to y_offset. |
| 74 | * @m_history: rate between y and x. |
| 75 | * @history_len: Amount of valid historic data in the arrays. |
| 76 | * @temp_buf: Temporary buffer used when updating the filter. |
| 77 | * @median_m: median value of m_history |
| 78 | * @median_error: final error to apply to AP interrupt timestamp to get the |
| 79 | * "true timestamp" the event occurred. |
| 80 | */ |
| 81 | struct cros_ec_sensors_ts_filter_state { |
| 82 | s64 x_offset, y_offset; |
| 83 | s64 x_history[CROS_EC_SENSORHUB_TS_HISTORY_SIZE]; |
| 84 | s64 y_history[CROS_EC_SENSORHUB_TS_HISTORY_SIZE]; |
| 85 | s64 m_history[CROS_EC_SENSORHUB_TS_HISTORY_SIZE]; |
| 86 | int history_len; |
| 87 | |
| 88 | s64 temp_buf[CROS_EC_SENSORHUB_TS_HISTORY_SIZE]; |
| 89 | |
| 90 | s64 median_m; |
| 91 | s64 median_error; |
| 92 | }; |
| 93 | |
| 94 | /* struct cros_ec_sensors_ts_batch_state - State of batch of a single sensor. |
| 95 | * |
| 96 | * Use to store information to batch data using median fileter information. |
| 97 | * |
| 98 | * @penul_ts: last but one batch timestamp (penultimate timestamp). |
| 99 | * Used for timestamp spreading calculations |
| 100 | * when a batch shows up. |
| 101 | * @penul_len: last but one batch length. |
| 102 | * @last_ts: Last batch timestam. |
| 103 | * @last_len: Last batch length. |
| 104 | * @newest_sensor_event: Last sensor timestamp. |
| 105 | */ |
| 106 | struct cros_ec_sensors_ts_batch_state { |
| 107 | s64 penul_ts; |
| 108 | int penul_len; |
| 109 | s64 last_ts; |
| 110 | int last_len; |
| 111 | s64 newest_sensor_event; |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | * struct cros_ec_sensorhub - Sensor Hub device data. |
| 116 | * |
| 117 | * @dev: Device object, mostly used for logging. |
| 118 | * @ec: Embedded Controller where the hub is located. |
| 119 | * @sensor_num: Number of MEMS sensors present in the EC. |
| 120 | * @msg: Structure to send FIFO requests. |
| 121 | * @params: Pointer to parameters in msg. |
| 122 | * @resp: Pointer to responses in msg. |
| 123 | * @cmd_lock : Lock for sending msg. |
| 124 | * @notifier: Notifier to kick the FIFO interrupt. |
| 125 | * @ring: Preprocessed ring to store events. |
| 126 | * @fifo_timestamp: Array for event timestamp and spreading. |
| 127 | * @fifo_info: Copy of FIFO information coming from the EC. |
| 128 | * @fifo_size: Size of the ring. |
| 129 | * @batch_state: Per sensor information of the last batches received. |
| 130 | * @overflow_a: For handling timestamp overflow for a time (sensor events) |
| 131 | * @overflow_b: For handling timestamp overflow for b time (ec interrupts) |
| 132 | * @filter: Medium fileter structure. |
| 133 | * @tight_timestamps: Set to truen when EC support tight timestamping: |
| 134 | * The timestamps reported from the EC have low jitter. |
| 135 | * Timestamps also come before every sample. Set either |
| 136 | * by feature bits coming from the EC or userspace. |
| 137 | * @future_timestamp_count: Statistics used to compute shaved time. |
| 138 | * This occurs when timestamp interpolation from EC |
| 139 | * time to AP time accidentally puts timestamps in |
| 140 | * the future. These timestamps are clamped to |
| 141 | * `now` and these count/total_ns maintain the |
| 142 | * statistics for how much time was removed in a |
| 143 | * given period. |
| 144 | * @future_timestamp_total_ns: Total amount of time shaved. |
| 145 | * @push_data: Array of callback to send datums to iio sensor object. |
| 146 | */ |
| 147 | struct cros_ec_sensorhub { |
| 148 | struct device *dev; |
| 149 | struct cros_ec_dev *ec; |
| 150 | int sensor_num; |
| 151 | |
| 152 | struct cros_ec_command *msg; |
| 153 | struct ec_params_motion_sense *params; |
| 154 | struct ec_response_motion_sense *resp; |
| 155 | struct mutex cmd_lock; /* Lock for protecting msg structure. */ |
| 156 | |
| 157 | struct notifier_block notifier; |
| 158 | |
| 159 | struct cros_ec_sensors_ring_sample *ring; |
| 160 | |
| 161 | ktime_t fifo_timestamp[CROS_EC_SENSOR_ALL_TS]; |
| 162 | struct ec_response_motion_sense_fifo_info *fifo_info; |
| 163 | int fifo_size; |
| 164 | |
| 165 | struct cros_ec_sensors_ts_batch_state *batch_state; |
| 166 | |
| 167 | struct cros_ec_sensors_ec_overflow_state overflow_a; |
| 168 | struct cros_ec_sensors_ec_overflow_state overflow_b; |
| 169 | |
| 170 | struct cros_ec_sensors_ts_filter_state filter; |
| 171 | |
| 172 | int tight_timestamps; |
| 173 | |
| 174 | s32 future_timestamp_count; |
| 175 | s64 future_timestamp_total_ns; |
| 176 | |
| 177 | struct cros_ec_sensorhub_sensor_push_data *push_data; |
| 178 | }; |
| 179 | |
| 180 | int cros_ec_sensorhub_register_push_data(struct cros_ec_sensorhub *sensorhub, |
| 181 | u8 sensor_num, |
| 182 | struct iio_dev *indio_dev, |
| 183 | cros_ec_sensorhub_push_data_cb_t cb); |
| 184 | |
| 185 | void cros_ec_sensorhub_unregister_push_data(struct cros_ec_sensorhub *sensorhub, |
| 186 | u8 sensor_num); |
| 187 | |
| 188 | int cros_ec_sensorhub_ring_allocate(struct cros_ec_sensorhub *sensorhub); |
| 189 | int cros_ec_sensorhub_ring_add(struct cros_ec_sensorhub *sensorhub); |
| 190 | void cros_ec_sensorhub_ring_remove(void *arg); |
| 191 | int cros_ec_sensorhub_ring_fifo_enable(struct cros_ec_sensorhub *sensorhub, |
| 192 | bool on); |
| 193 | |
| 194 | #endif /* __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H */ |
| 195 | |