1 /*
2  * STMicroelectronics st_lsm6dsx sensor driver
3  *
4  * Copyright 2016 STMicroelectronics Inc.
5  *
6  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7  * Denis Ciocca <denis.ciocca@st.com>
8  *
9  * Licensed under the GPL-2.
10  */
11 
12 #ifndef ST_LSM6DSX_H
13 #define ST_LSM6DSX_H
14 
15 #include <linux/device.h>
16 
17 #define ST_LSM6DS3_DEV_NAME	"lsm6ds3"
18 #define ST_LSM6DS3H_DEV_NAME	"lsm6ds3h"
19 #define ST_LSM6DSL_DEV_NAME	"lsm6dsl"
20 #define ST_LSM6DSM_DEV_NAME	"lsm6dsm"
21 
22 enum st_lsm6dsx_hw_id {
23 	ST_LSM6DS3_ID,
24 	ST_LSM6DS3H_ID,
25 	ST_LSM6DSL_ID,
26 	ST_LSM6DSM_ID,
27 	ST_LSM6DSX_MAX_ID,
28 };
29 
30 #define ST_LSM6DSX_CHAN_SIZE		2
31 #define ST_LSM6DSX_SAMPLE_SIZE		6
32 
33 #if defined(CONFIG_SPI_MASTER)
34 #define ST_LSM6DSX_RX_MAX_LENGTH	256
35 #define ST_LSM6DSX_TX_MAX_LENGTH	8
36 
37 struct st_lsm6dsx_transfer_buffer {
38 	u8 rx_buf[ST_LSM6DSX_RX_MAX_LENGTH];
39 	u8 tx_buf[ST_LSM6DSX_TX_MAX_LENGTH] ____cacheline_aligned;
40 };
41 #endif /* CONFIG_SPI_MASTER */
42 
43 struct st_lsm6dsx_transfer_function {
44 	int (*read)(struct device *dev, u8 addr, int len, u8 *data);
45 	int (*write)(struct device *dev, u8 addr, int len, u8 *data);
46 };
47 
48 struct st_lsm6dsx_reg {
49 	u8 addr;
50 	u8 mask;
51 };
52 
53 /**
54  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
55  * @fifo_th: FIFO threshold register info (addr + mask).
56  * @fifo_diff: FIFO diff status register info (addr + mask).
57  * @th_wl: FIFO threshold word length.
58  */
59 struct st_lsm6dsx_fifo_ops {
60 	struct {
61 		u8 addr;
62 		u16 mask;
63 	} fifo_th;
64 	struct {
65 		u8 addr;
66 		u16 mask;
67 	} fifo_diff;
68 	u8 th_wl;
69 };
70 
71 /**
72  * struct st_lsm6dsx_settings - ST IMU sensor settings
73  * @wai: Sensor WhoAmI default value.
74  * @max_fifo_size: Sensor max fifo length in FIFO words.
75  * @id: List of hw id supported by the driver configuration.
76  * @decimator: List of decimator register info (addr + mask).
77  * @fifo_ops: Sensor hw FIFO parameters.
78  */
79 struct st_lsm6dsx_settings {
80 	u8 wai;
81 	u16 max_fifo_size;
82 	enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID];
83 	struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
84 	struct st_lsm6dsx_fifo_ops fifo_ops;
85 };
86 
87 enum st_lsm6dsx_sensor_id {
88 	ST_LSM6DSX_ID_ACC,
89 	ST_LSM6DSX_ID_GYRO,
90 	ST_LSM6DSX_ID_MAX,
91 };
92 
93 enum st_lsm6dsx_fifo_mode {
94 	ST_LSM6DSX_FIFO_BYPASS = 0x0,
95 	ST_LSM6DSX_FIFO_CONT = 0x6,
96 };
97 
98 /**
99  * struct st_lsm6dsx_sensor - ST IMU sensor instance
100  * @name: Sensor name.
101  * @id: Sensor identifier.
102  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
103  * @gain: Configured sensor sensitivity.
104  * @odr: Output data rate of the sensor [Hz].
105  * @watermark: Sensor watermark level.
106  * @sip: Number of samples in a given pattern.
107  * @decimator: FIFO decimation factor.
108  * @delta_ts: Delta time between two consecutive interrupts.
109  * @ts: Latest timestamp from the interrupt handler.
110  */
111 struct st_lsm6dsx_sensor {
112 	char name[32];
113 	enum st_lsm6dsx_sensor_id id;
114 	struct st_lsm6dsx_hw *hw;
115 
116 	u32 gain;
117 	u16 odr;
118 
119 	u16 watermark;
120 	u8 sip;
121 	u8 decimator;
122 
123 	s64 delta_ts;
124 	s64 ts;
125 };
126 
127 /**
128  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
129  * @dev: Pointer to instance of struct device (I2C or SPI).
130  * @irq: Device interrupt line (I2C or SPI).
131  * @lock: Mutex to protect read and write operations.
132  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
133  * @fifo_mode: FIFO operating mode supported by the device.
134  * @enable_mask: Enabled sensor bitmask.
135  * @sip: Total number of samples (acc/gyro) in a given pattern.
136  * @iio_devs: Pointers to acc/gyro iio_dev instances.
137  * @settings: Pointer to the specific sensor settings in use.
138  * @tf: Transfer function structure used by I/O operations.
139  * @tb: Transfer buffers used by SPI I/O operations.
140  */
141 struct st_lsm6dsx_hw {
142 	struct device *dev;
143 	int irq;
144 
145 	struct mutex lock;
146 	struct mutex fifo_lock;
147 
148 	enum st_lsm6dsx_fifo_mode fifo_mode;
149 	u8 enable_mask;
150 	u8 sip;
151 
152 	struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
153 
154 	const struct st_lsm6dsx_settings *settings;
155 
156 	const struct st_lsm6dsx_transfer_function *tf;
157 #if defined(CONFIG_SPI_MASTER)
158 	struct st_lsm6dsx_transfer_buffer tb;
159 #endif /* CONFIG_SPI_MASTER */
160 };
161 
162 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
163 
164 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
165 		     const struct st_lsm6dsx_transfer_function *tf_ops);
166 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor);
167 int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor);
168 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
169 int st_lsm6dsx_write_with_mask(struct st_lsm6dsx_hw *hw, u8 addr, u8 mask,
170 			       u8 val);
171 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
172 				u16 watermark);
173 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
174 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
175 			     enum st_lsm6dsx_fifo_mode fifo_mode);
176 
177 #endif /* ST_LSM6DSX_H */
178