153067471SGwendal Grignou /* SPDX-License-Identifier: GPL-2.0 */
253067471SGwendal Grignou /*
353067471SGwendal Grignou  * Chrome OS EC MEMS Sensor Hub driver.
453067471SGwendal Grignou  *
553067471SGwendal Grignou  * Copyright 2019 Google LLC
653067471SGwendal Grignou  */
753067471SGwendal Grignou 
853067471SGwendal Grignou #ifndef __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H
953067471SGwendal Grignou #define __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H
1053067471SGwendal Grignou 
11145d59baSGwendal Grignou #include <linux/ktime.h>
12145d59baSGwendal Grignou #include <linux/mutex.h>
13145d59baSGwendal Grignou #include <linux/notifier.h>
1453067471SGwendal Grignou #include <linux/platform_data/cros_ec_commands.h>
1553067471SGwendal Grignou 
16145d59baSGwendal Grignou struct iio_dev;
17145d59baSGwendal Grignou 
1853067471SGwendal Grignou /**
19d60ac88aSGwendal Grignou  * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
20d60ac88aSGwendal Grignou  * @sensor_num: Id of the sensor, as reported by the EC.
21d60ac88aSGwendal Grignou  */
22d60ac88aSGwendal Grignou struct cros_ec_sensor_platform {
23d60ac88aSGwendal Grignou 	u8 sensor_num;
24d60ac88aSGwendal Grignou };
25d60ac88aSGwendal Grignou 
26d60ac88aSGwendal Grignou /**
27145d59baSGwendal Grignou  * typedef cros_ec_sensorhub_push_data_cb_t - Callback function to send datum
28145d59baSGwendal Grignou  *					      to specific sensors.
29145d59baSGwendal Grignou  *
30145d59baSGwendal Grignou  * @indio_dev: The IIO device that will process the sample.
31145d59baSGwendal Grignou  * @data: Vector array of the ring sample.
32145d59baSGwendal Grignou  * @timestamp: Timestamp in host timespace when the sample was acquired by
33145d59baSGwendal Grignou  *             the EC.
34145d59baSGwendal Grignou  */
35145d59baSGwendal Grignou typedef int (*cros_ec_sensorhub_push_data_cb_t)(struct iio_dev *indio_dev,
36145d59baSGwendal Grignou 						s16 *data,
37145d59baSGwendal Grignou 						s64 timestamp);
38145d59baSGwendal Grignou 
39145d59baSGwendal Grignou struct cros_ec_sensorhub_sensor_push_data {
40145d59baSGwendal Grignou 	struct iio_dev *indio_dev;
41145d59baSGwendal Grignou 	cros_ec_sensorhub_push_data_cb_t push_data_cb;
42145d59baSGwendal Grignou };
43145d59baSGwendal Grignou 
44145d59baSGwendal Grignou enum {
45145d59baSGwendal Grignou 	CROS_EC_SENSOR_LAST_TS,
46145d59baSGwendal Grignou 	CROS_EC_SENSOR_NEW_TS,
47145d59baSGwendal Grignou 	CROS_EC_SENSOR_ALL_TS
48145d59baSGwendal Grignou };
49145d59baSGwendal Grignou 
50145d59baSGwendal Grignou struct cros_ec_sensors_ring_sample {
51145d59baSGwendal Grignou 	u8  sensor_id;
52145d59baSGwendal Grignou 	u8  flag;
53145d59baSGwendal Grignou 	s16 vector[3];
54145d59baSGwendal Grignou 	s64 timestamp;
55145d59baSGwendal Grignou } __packed;
56145d59baSGwendal Grignou 
5793fe48a5SGwendal Grignou /* State used for cros_ec_ring_fix_overflow */
5893fe48a5SGwendal Grignou struct cros_ec_sensors_ec_overflow_state {
5993fe48a5SGwendal Grignou 	s64 offset;
6093fe48a5SGwendal Grignou 	s64 last;
6193fe48a5SGwendal Grignou };
6293fe48a5SGwendal Grignou 
6393fe48a5SGwendal Grignou /* Length of the filter, how long to remember entries for */
6493fe48a5SGwendal Grignou #define CROS_EC_SENSORHUB_TS_HISTORY_SIZE 64
6593fe48a5SGwendal Grignou 
66145d59baSGwendal Grignou /**
6793fe48a5SGwendal Grignou  * struct cros_ec_sensors_ts_filter_state - Timestamp filetr state.
6893fe48a5SGwendal Grignou  *
6993fe48a5SGwendal Grignou  * @x_offset: x is EC interrupt time. x_offset its last value.
7093fe48a5SGwendal Grignou  * @y_offset: y is the difference between AP and EC time, y_offset its last
7193fe48a5SGwendal Grignou  *            value.
7293fe48a5SGwendal Grignou  * @x_history: The past history of x, relative to x_offset.
7393fe48a5SGwendal Grignou  * @y_history: The past history of y, relative to y_offset.
7493fe48a5SGwendal Grignou  * @m_history: rate between y and x.
7593fe48a5SGwendal Grignou  * @history_len: Amount of valid historic data in the arrays.
7693fe48a5SGwendal Grignou  * @temp_buf: Temporary buffer used when updating the filter.
7793fe48a5SGwendal Grignou  * @median_m: median value of m_history
7893fe48a5SGwendal Grignou  * @median_error: final error to apply to AP interrupt timestamp to get the
7993fe48a5SGwendal Grignou  *                "true timestamp" the event occurred.
8093fe48a5SGwendal Grignou  */
8193fe48a5SGwendal Grignou struct cros_ec_sensors_ts_filter_state {
8293fe48a5SGwendal Grignou 	s64 x_offset, y_offset;
8393fe48a5SGwendal Grignou 	s64 x_history[CROS_EC_SENSORHUB_TS_HISTORY_SIZE];
8493fe48a5SGwendal Grignou 	s64 y_history[CROS_EC_SENSORHUB_TS_HISTORY_SIZE];
8593fe48a5SGwendal Grignou 	s64 m_history[CROS_EC_SENSORHUB_TS_HISTORY_SIZE];
8693fe48a5SGwendal Grignou 	int history_len;
8793fe48a5SGwendal Grignou 
8893fe48a5SGwendal Grignou 	s64 temp_buf[CROS_EC_SENSORHUB_TS_HISTORY_SIZE];
8993fe48a5SGwendal Grignou 
9093fe48a5SGwendal Grignou 	s64 median_m;
9193fe48a5SGwendal Grignou 	s64 median_error;
9293fe48a5SGwendal Grignou };
9393fe48a5SGwendal Grignou 
9493fe48a5SGwendal Grignou /* struct cros_ec_sensors_ts_batch_state - State of batch of a single sensor.
9593fe48a5SGwendal Grignou  *
9693fe48a5SGwendal Grignou  * Use to store information to batch data using median fileter information.
9793fe48a5SGwendal Grignou  *
9893fe48a5SGwendal Grignou  * @penul_ts: last but one batch timestamp (penultimate timestamp).
9993fe48a5SGwendal Grignou  *	      Used for timestamp spreading calculations
10093fe48a5SGwendal Grignou  *	      when a batch shows up.
10193fe48a5SGwendal Grignou  * @penul_len: last but one batch length.
10293fe48a5SGwendal Grignou  * @last_ts: Last batch timestam.
10393fe48a5SGwendal Grignou  * @last_len: Last batch length.
10493fe48a5SGwendal Grignou  * @newest_sensor_event: Last sensor timestamp.
10593fe48a5SGwendal Grignou  */
10693fe48a5SGwendal Grignou struct cros_ec_sensors_ts_batch_state {
10793fe48a5SGwendal Grignou 	s64 penul_ts;
10893fe48a5SGwendal Grignou 	int penul_len;
10993fe48a5SGwendal Grignou 	s64 last_ts;
11093fe48a5SGwendal Grignou 	int last_len;
11193fe48a5SGwendal Grignou 	s64 newest_sensor_event;
11293fe48a5SGwendal Grignou };
11393fe48a5SGwendal Grignou 
11493fe48a5SGwendal Grignou /*
11553067471SGwendal Grignou  * struct cros_ec_sensorhub - Sensor Hub device data.
11653067471SGwendal Grignou  *
117145d59baSGwendal Grignou  * @dev: Device object, mostly used for logging.
11853067471SGwendal Grignou  * @ec: Embedded Controller where the hub is located.
119cee416a3SGwendal Grignou  * @sensor_num: Number of MEMS sensors present in the EC.
120145d59baSGwendal Grignou  * @msg: Structure to send FIFO requests.
121145d59baSGwendal Grignou  * @params: Pointer to parameters in msg.
122145d59baSGwendal Grignou  * @resp: Pointer to responses in msg.
123145d59baSGwendal Grignou  * @cmd_lock : Lock for sending msg.
124145d59baSGwendal Grignou  * @notifier: Notifier to kick the FIFO interrupt.
125145d59baSGwendal Grignou  * @ring: Preprocessed ring to store events.
12693fe48a5SGwendal Grignou  * @fifo_timestamp: Array for event timestamp and spreading.
12793fe48a5SGwendal Grignou  * @fifo_info: Copy of FIFO information coming from the EC.
12893fe48a5SGwendal Grignou  * @fifo_size: Size of the ring.
12993fe48a5SGwendal Grignou  * @batch_state: Per sensor information of the last batches received.
13093fe48a5SGwendal Grignou  * @overflow_a: For handling timestamp overflow for a time (sensor events)
13193fe48a5SGwendal Grignou  * @overflow_b: For handling timestamp overflow for b time (ec interrupts)
13293fe48a5SGwendal Grignou  * @filter: Medium fileter structure.
13393fe48a5SGwendal Grignou  * @tight_timestamps: Set to truen when EC support tight timestamping:
13493fe48a5SGwendal Grignou  *		      The timestamps reported from the EC have low jitter.
13593fe48a5SGwendal Grignou  *		      Timestamps also come before every sample. Set either
13693fe48a5SGwendal Grignou  *		      by feature bits coming from the EC or userspace.
13793fe48a5SGwendal Grignou  * @future_timestamp_count: Statistics used to compute shaved time.
13893fe48a5SGwendal Grignou  *			    This occurs when timestamp interpolation from EC
13993fe48a5SGwendal Grignou  *			    time to AP time accidentally puts timestamps in
14093fe48a5SGwendal Grignou  *			    the future. These timestamps are clamped to
14193fe48a5SGwendal Grignou  *			    `now` and these count/total_ns maintain the
14293fe48a5SGwendal Grignou  *			    statistics for how much time was removed in a
14393fe48a5SGwendal Grignou  *			    given period.
14493fe48a5SGwendal Grignou  * @future_timestamp_total_ns: Total amount of time shaved.
14593fe48a5SGwendal Grignou  * @push_data: Array of callback to send datums to iio sensor object.
14653067471SGwendal Grignou  */
14753067471SGwendal Grignou struct cros_ec_sensorhub {
148145d59baSGwendal Grignou 	struct device *dev;
14953067471SGwendal Grignou 	struct cros_ec_dev *ec;
150cee416a3SGwendal Grignou 	int sensor_num;
151145d59baSGwendal Grignou 
152145d59baSGwendal Grignou 	struct cros_ec_command *msg;
153145d59baSGwendal Grignou 	struct ec_params_motion_sense *params;
154145d59baSGwendal Grignou 	struct ec_response_motion_sense *resp;
155145d59baSGwendal Grignou 	struct mutex cmd_lock;  /* Lock for protecting msg structure. */
156145d59baSGwendal Grignou 
157145d59baSGwendal Grignou 	struct notifier_block notifier;
158145d59baSGwendal Grignou 
159145d59baSGwendal Grignou 	struct cros_ec_sensors_ring_sample *ring;
160145d59baSGwendal Grignou 
161145d59baSGwendal Grignou 	ktime_t fifo_timestamp[CROS_EC_SENSOR_ALL_TS];
162145d59baSGwendal Grignou 	struct ec_response_motion_sense_fifo_info *fifo_info;
163145d59baSGwendal Grignou 	int fifo_size;
164145d59baSGwendal Grignou 
16593fe48a5SGwendal Grignou 	struct cros_ec_sensors_ts_batch_state *batch_state;
16693fe48a5SGwendal Grignou 
16793fe48a5SGwendal Grignou 	struct cros_ec_sensors_ec_overflow_state overflow_a;
16893fe48a5SGwendal Grignou 	struct cros_ec_sensors_ec_overflow_state overflow_b;
16993fe48a5SGwendal Grignou 
17093fe48a5SGwendal Grignou 	struct cros_ec_sensors_ts_filter_state filter;
17193fe48a5SGwendal Grignou 
17293fe48a5SGwendal Grignou 	int tight_timestamps;
17393fe48a5SGwendal Grignou 
17493fe48a5SGwendal Grignou 	s32 future_timestamp_count;
17593fe48a5SGwendal Grignou 	s64 future_timestamp_total_ns;
17693fe48a5SGwendal Grignou 
177145d59baSGwendal Grignou 	struct cros_ec_sensorhub_sensor_push_data *push_data;
17853067471SGwendal Grignou };
17953067471SGwendal Grignou 
180145d59baSGwendal Grignou int cros_ec_sensorhub_register_push_data(struct cros_ec_sensorhub *sensorhub,
181145d59baSGwendal Grignou 					 u8 sensor_num,
182145d59baSGwendal Grignou 					 struct iio_dev *indio_dev,
183145d59baSGwendal Grignou 					 cros_ec_sensorhub_push_data_cb_t cb);
184145d59baSGwendal Grignou 
185145d59baSGwendal Grignou void cros_ec_sensorhub_unregister_push_data(struct cros_ec_sensorhub *sensorhub,
186145d59baSGwendal Grignou 					    u8 sensor_num);
187145d59baSGwendal Grignou 
188*b31d1d2bSGwendal Grignou int cros_ec_sensorhub_ring_allocate(struct cros_ec_sensorhub *sensorhub);
189145d59baSGwendal Grignou int cros_ec_sensorhub_ring_add(struct cros_ec_sensorhub *sensorhub);
190145d59baSGwendal Grignou void cros_ec_sensorhub_ring_remove(void *arg);
191145d59baSGwendal Grignou int cros_ec_sensorhub_ring_fifo_enable(struct cros_ec_sensorhub *sensorhub,
192145d59baSGwendal Grignou 				       bool on);
193145d59baSGwendal Grignou 
19453067471SGwendal Grignou #endif   /* __LINUX_PLATFORM_DATA_CROS_EC_SENSORHUB_H */
195