1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Chrome OS EC MEMS Sensor Hub driver.
5 * Copyright 2019 Google LLC
8 #ifndef __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H
9 #define __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H
11 #include <linux/ktime.h>
12 #include <linux/mutex.h>
13 #include <linux/notifier.h>
14 #include <linux/platform_data/cros_ec_commands.h>
19 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
20 * @sensor_num: Id of the sensor, as reported by the EC.
22 struct cros_ec_sensor_platform {
27 * typedef cros_ec_sensorhub_push_data_cb_t - Callback function to send datum
28 * to specific sensors.
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
35 typedef int (*cros_ec_sensorhub_push_data_cb_t)(struct iio_dev *indio_dev,
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;
45 CROS_EC_SENSOR_LAST_TS,
46 CROS_EC_SENSOR_NEW_TS,
50 struct cros_ec_sensors_ring_sample {
57 /* State used for cros_ec_ring_fix_overflow */
58 struct cros_ec_sensors_ec_overflow_state {
63 /* Length of the filter, how long to remember entries for */
64 #define CROS_EC_SENSORHUB_TS_HISTORY_SIZE 64
67 * struct cros_ec_sensors_ts_filter_state - Timestamp filetr state.
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
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.
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];
88 s64 temp_buf[CROS_EC_SENSORHUB_TS_HISTORY_SIZE];
94 /* struct cros_ec_sensors_ts_batch_state - State of batch of a single sensor.
96 * Use to store information to batch data using median fileter information.
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.
106 struct cros_ec_sensors_ts_batch_state {
111 s64 newest_sensor_event;
115 * struct cros_ec_sensorhub - Sensor Hub device data.
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
144 * @future_timestamp_total_ns: Total amount of time shaved.
145 * @push_data: Array of callback to send datums to iio sensor object.
147 struct cros_ec_sensorhub {
149 struct cros_ec_dev *ec;
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. */
157 struct notifier_block notifier;
159 struct cros_ec_sensors_ring_sample *ring;
161 ktime_t fifo_timestamp[CROS_EC_SENSOR_ALL_TS];
162 struct ec_response_motion_sense_fifo_info *fifo_info;
165 struct cros_ec_sensors_ts_batch_state *batch_state;
167 struct cros_ec_sensors_ec_overflow_state overflow_a;
168 struct cros_ec_sensors_ec_overflow_state overflow_b;
170 struct cros_ec_sensors_ts_filter_state filter;
172 int tight_timestamps;
174 s32 future_timestamp_count;
175 s64 future_timestamp_total_ns;
177 struct cros_ec_sensorhub_sensor_push_data *push_data;
180 int cros_ec_sensorhub_register_push_data(struct cros_ec_sensorhub *sensorhub,
182 struct iio_dev *indio_dev,
183 cros_ec_sensorhub_push_data_cb_t cb);
185 void cros_ec_sensorhub_unregister_push_data(struct cros_ec_sensorhub *sensorhub,
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,
194 #endif /* __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H */