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