xref: /linux/drivers/iio/imu/inv_icm45600/inv_icm45600_buffer.h (revision 83bd89291f5cc866f60d32c34e268896c7ba8a3d)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Copyright (C) 2025 Invensense, Inc. */
3 
4 #ifndef INV_ICM45600_BUFFER_H_
5 #define INV_ICM45600_BUFFER_H_
6 
7 #include <linux/bits.h>
8 #include <linux/limits.h>
9 #include <linux/types.h>
10 
11 #include <asm/byteorder.h>
12 
13 #include <linux/iio/iio.h>
14 
15 struct inv_icm45600_state;
16 
17 #define INV_ICM45600_SENSOR_GYRO	BIT(0)
18 #define INV_ICM45600_SENSOR_ACCEL	BIT(1)
19 #define INV_ICM45600_SENSOR_TEMP	BIT(2)
20 
21 /**
22  * struct inv_icm45600_fifo - FIFO state variables
23  * @on:		reference counter for FIFO on.
24  * @en:		bits field of INV_ICM45600_SENSOR_* for FIFO EN bits.
25  * @period:	FIFO internal period.
26  * @watermark:	watermark configuration values for accel and gyro.
27  * @watermark.gyro:	 requested watermark for gyro.
28  * @watermark.accel:	 requested watermark for accel.
29  * @watermark.eff_gyro:	 effective watermark for gyro.
30  * @watermark.eff_accel: effective watermark for accel.
31  * @count:	number of bytes in the FIFO data buffer.
32  * @nb:		gyro, accel and total samples in the FIFO data buffer.
33  * @data:	FIFO data buffer aligned for DMA (8kB)
34  */
35 struct inv_icm45600_fifo {
36 	unsigned int on;
37 	unsigned int en;
38 	u32 period;
39 	struct {
40 		unsigned int gyro;
41 		unsigned int accel;
42 		unsigned int eff_gyro;
43 		unsigned int eff_accel;
44 	} watermark;
45 	size_t count;
46 	struct {
47 		size_t gyro;
48 		size_t accel;
49 		size_t total;
50 	} nb;
51 	u8 *data;
52 };
53 
54 /* FIFO data packet */
55 struct inv_icm45600_fifo_sensor_data {
56 	__le16 x;
57 	__le16 y;
58 	__le16 z;
59 } __packed;
60 #define INV_ICM45600_DATA_INVALID		S16_MIN
61 
62 static inline bool
inv_icm45600_fifo_is_data_valid(const struct inv_icm45600_fifo_sensor_data * s)63 inv_icm45600_fifo_is_data_valid(const struct inv_icm45600_fifo_sensor_data *s)
64 {
65 	s16 x, y, z;
66 
67 	x = le16_to_cpu(s->x);
68 	y = le16_to_cpu(s->y);
69 	z = le16_to_cpu(s->z);
70 
71 	return (x != INV_ICM45600_DATA_INVALID ||
72 		y != INV_ICM45600_DATA_INVALID ||
73 		z != INV_ICM45600_DATA_INVALID);
74 }
75 
76 ssize_t inv_icm45600_fifo_decode_packet(const void *packet,
77 					const struct inv_icm45600_fifo_sensor_data **accel,
78 					const struct inv_icm45600_fifo_sensor_data **gyro,
79 					const s8 **temp,
80 					const __le16 **timestamp, unsigned int *odr);
81 
82 extern const struct iio_buffer_setup_ops inv_icm45600_buffer_ops;
83 
84 int inv_icm45600_buffer_init(struct inv_icm45600_state *st);
85 
86 void inv_icm45600_buffer_update_fifo_period(struct inv_icm45600_state *st);
87 
88 int inv_icm45600_buffer_set_fifo_en(struct inv_icm45600_state *st,
89 				    unsigned int fifo_en);
90 
91 int inv_icm45600_buffer_update_watermark(struct inv_icm45600_state *st);
92 
93 int inv_icm45600_buffer_fifo_read(struct inv_icm45600_state *st,
94 				  unsigned int max);
95 
96 int inv_icm45600_buffer_fifo_parse(struct inv_icm45600_state *st);
97 
98 int inv_icm45600_buffer_hwfifo_flush(struct inv_icm45600_state *st,
99 				     unsigned int count);
100 
101 #endif
102