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 #define ST_ISM330DLC_DEV_NAME	"ism330dlc"
22 #define ST_LSM6DSO_DEV_NAME	"lsm6dso"
23 
24 enum st_lsm6dsx_hw_id {
25 	ST_LSM6DS3_ID,
26 	ST_LSM6DS3H_ID,
27 	ST_LSM6DSL_ID,
28 	ST_LSM6DSM_ID,
29 	ST_ISM330DLC_ID,
30 	ST_LSM6DSO_ID,
31 	ST_LSM6DSX_MAX_ID,
32 };
33 
34 #define ST_LSM6DSX_BUFF_SIZE		512
35 #define ST_LSM6DSX_CHAN_SIZE		2
36 #define ST_LSM6DSX_SAMPLE_SIZE		6
37 #define ST_LSM6DSX_TAG_SIZE		1
38 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE	(ST_LSM6DSX_SAMPLE_SIZE + \
39 					 ST_LSM6DSX_TAG_SIZE)
40 #define ST_LSM6DSX_MAX_WORD_LEN		((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
41 					 ST_LSM6DSX_SAMPLE_SIZE)
42 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN	((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
43 					 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
44 #define ST_LSM6DSX_SHIFT_VAL(val, mask)	(((val) << __ffs(mask)) & (mask))
45 
46 struct st_lsm6dsx_reg {
47 	u8 addr;
48 	u8 mask;
49 };
50 
51 struct st_lsm6dsx_hw;
52 
53 /**
54  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
55  * @read_fifo: Read FIFO callback.
56  * @fifo_th: FIFO threshold register info (addr + mask).
57  * @fifo_diff: FIFO diff status register info (addr + mask).
58  * @th_wl: FIFO threshold word length.
59  */
60 struct st_lsm6dsx_fifo_ops {
61 	int (*read_fifo)(struct st_lsm6dsx_hw *hw);
62 	struct {
63 		u8 addr;
64 		u16 mask;
65 	} fifo_th;
66 	struct {
67 		u8 addr;
68 		u16 mask;
69 	} fifo_diff;
70 	u8 th_wl;
71 };
72 
73 /**
74  * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
75  * @timer_en: Hw timer enable register info (addr + mask).
76  * @hr_timer: Hw timer resolution register info (addr + mask).
77  * @fifo_en: Hw timer FIFO enable register info (addr + mask).
78  * @decimator: Hw timer FIFO decimator register info (addr + mask).
79  */
80 struct st_lsm6dsx_hw_ts_settings {
81 	struct st_lsm6dsx_reg timer_en;
82 	struct st_lsm6dsx_reg hr_timer;
83 	struct st_lsm6dsx_reg fifo_en;
84 	struct st_lsm6dsx_reg decimator;
85 };
86 
87 /**
88  * struct st_lsm6dsx_settings - ST IMU sensor settings
89  * @wai: Sensor WhoAmI default value.
90  * @max_fifo_size: Sensor max fifo length in FIFO words.
91  * @id: List of hw id supported by the driver configuration.
92  * @decimator: List of decimator register info (addr + mask).
93  * @batch: List of FIFO batching register info (addr + mask).
94  * @fifo_ops: Sensor hw FIFO parameters.
95  * @ts_settings: Hw timer related settings.
96  */
97 struct st_lsm6dsx_settings {
98 	u8 wai;
99 	u16 max_fifo_size;
100 	enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID];
101 	struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
102 	struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
103 	struct st_lsm6dsx_fifo_ops fifo_ops;
104 	struct st_lsm6dsx_hw_ts_settings ts_settings;
105 };
106 
107 enum st_lsm6dsx_sensor_id {
108 	ST_LSM6DSX_ID_ACC,
109 	ST_LSM6DSX_ID_GYRO,
110 	ST_LSM6DSX_ID_MAX,
111 };
112 
113 enum st_lsm6dsx_fifo_mode {
114 	ST_LSM6DSX_FIFO_BYPASS = 0x0,
115 	ST_LSM6DSX_FIFO_CONT = 0x6,
116 };
117 
118 /**
119  * struct st_lsm6dsx_sensor - ST IMU sensor instance
120  * @name: Sensor name.
121  * @id: Sensor identifier.
122  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
123  * @gain: Configured sensor sensitivity.
124  * @odr: Output data rate of the sensor [Hz].
125  * @watermark: Sensor watermark level.
126  * @sip: Number of samples in a given pattern.
127  * @decimator: FIFO decimation factor.
128  * @ts_ref: Sensor timestamp reference for hw one.
129  */
130 struct st_lsm6dsx_sensor {
131 	char name[32];
132 	enum st_lsm6dsx_sensor_id id;
133 	struct st_lsm6dsx_hw *hw;
134 
135 	u32 gain;
136 	u16 odr;
137 
138 	u16 watermark;
139 	u8 sip;
140 	u8 decimator;
141 	s64 ts_ref;
142 };
143 
144 /**
145  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
146  * @dev: Pointer to instance of struct device (I2C or SPI).
147  * @regmap: Register map of the device.
148  * @irq: Device interrupt line (I2C or SPI).
149  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
150  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
151  * @fifo_mode: FIFO operating mode supported by the device.
152  * @enable_mask: Enabled sensor bitmask.
153  * @ts_sip: Total number of timestamp samples in a given pattern.
154  * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
155  * @buff: Device read buffer.
156  * @iio_devs: Pointers to acc/gyro iio_dev instances.
157  * @settings: Pointer to the specific sensor settings in use.
158  */
159 struct st_lsm6dsx_hw {
160 	struct device *dev;
161 	struct regmap *regmap;
162 	int irq;
163 
164 	struct mutex fifo_lock;
165 	struct mutex conf_lock;
166 
167 	enum st_lsm6dsx_fifo_mode fifo_mode;
168 	u8 enable_mask;
169 	u8 ts_sip;
170 	u8 sip;
171 
172 	u8 *buff;
173 
174 	struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
175 
176 	const struct st_lsm6dsx_settings *settings;
177 };
178 
179 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
180 
181 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
182 		     struct regmap *regmap);
183 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor);
184 int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor);
185 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
186 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
187 				u16 watermark);
188 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
189 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
190 			     enum st_lsm6dsx_fifo_mode fifo_mode);
191 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
192 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
193 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);
194 
195 #endif /* ST_LSM6DSX_H */
196