summaryrefslogtreecommitdiff
path: root/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.h
blob: de2a3949dcc7d6f82041f383b1f7659f5564f67e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2020 Invensense, Inc.
 */

#ifndef INV_ICM42600_BUFFER_H_
#define INV_ICM42600_BUFFER_H_

#include <linux/kernel.h>
#include <linux/bits.h>

struct inv_icm42600_state;

#define INV_ICM42600_SENSOR_GYRO	BIT(0)
#define INV_ICM42600_SENSOR_ACCEL	BIT(1)
#define INV_ICM42600_SENSOR_TEMP	BIT(2)

/**
 * struct inv_icm42600_fifo - FIFO state variables
 * @on:		reference counter for FIFO on.
 * @en:		bits field of INV_ICM42600_SENSOR_* for FIFO EN bits.
 * @period:	FIFO internal period.
 * @watermark:	watermark configuration values for accel and gyro.
 * @count:	number of bytes in the FIFO data buffer.
 * @nb:		gyro, accel and total samples in the FIFO data buffer.
 * @data:	FIFO data buffer aligned for DMA (2kB + 32 bytes of read cache).
 */
struct inv_icm42600_fifo {
	unsigned int on;
	unsigned int en;
	uint32_t period;
	struct {
		unsigned int gyro;
		unsigned int accel;
	} watermark;
	size_t count;
	struct {
		size_t gyro;
		size_t accel;
		size_t total;
	} nb;
	uint8_t data[2080] ____cacheline_aligned;
};

/* FIFO data packet */
struct inv_icm42600_fifo_sensor_data {
	__be16 x;
	__be16 y;
	__be16 z;
} __packed;
#define INV_ICM42600_FIFO_DATA_INVALID		-32768

static inline int16_t inv_icm42600_fifo_get_sensor_data(__be16 d)
{
	return be16_to_cpu(d);
}

static inline bool
inv_icm42600_fifo_is_data_valid(const struct inv_icm42600_fifo_sensor_data *s)
{
	int16_t x, y, z;

	x = inv_icm42600_fifo_get_sensor_data(s->x);
	y = inv_icm42600_fifo_get_sensor_data(s->y);
	z = inv_icm42600_fifo_get_sensor_data(s->z);

	if (x == INV_ICM42600_FIFO_DATA_INVALID &&
	    y == INV_ICM42600_FIFO_DATA_INVALID &&
	    z == INV_ICM42600_FIFO_DATA_INVALID)
		return false;

	return true;
}

ssize_t inv_icm42600_fifo_decode_packet(const void *packet, const void **accel,
					const void **gyro, const int8_t **temp,
					const void **timestamp, unsigned int *odr);

extern const struct iio_buffer_setup_ops inv_icm42600_buffer_ops;

int inv_icm42600_buffer_init(struct inv_icm42600_state *st);

void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st);

int inv_icm42600_buffer_set_fifo_en(struct inv_icm42600_state *st,
				    unsigned int fifo_en);

int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st);

int inv_icm42600_buffer_fifo_read(struct inv_icm42600_state *st,
				  unsigned int max);

int inv_icm42600_buffer_fifo_parse(struct inv_icm42600_state *st);

int inv_icm42600_buffer_hwfifo_flush(struct inv_icm42600_state *st,
				     unsigned int count);

#endif