2 * STMicroelectronics st_lsm6dsx sensor driver
4 * Copyright 2016 STMicroelectronics Inc.
6 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7 * Denis Ciocca <denis.ciocca@st.com>
9 * Licensed under the GPL-2.
15 #include <linux/device.h>
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 #define ST_ASM330LHH_DEV_NAME "asm330lhh"
24 #define ST_LSM6DSOX_DEV_NAME "lsm6dsox"
25 #define ST_LSM6DSR_DEV_NAME "lsm6dsr"
27 enum st_lsm6dsx_hw_id {
40 #define ST_LSM6DSX_BUFF_SIZE 512
41 #define ST_LSM6DSX_CHAN_SIZE 2
42 #define ST_LSM6DSX_SAMPLE_SIZE 6
43 #define ST_LSM6DSX_TAG_SIZE 1
44 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE (ST_LSM6DSX_SAMPLE_SIZE + \
46 #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
47 ST_LSM6DSX_SAMPLE_SIZE)
48 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
49 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
50 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
52 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
59 BIT(IIO_CHAN_INFO_SCALE), \
60 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
61 .scan_index = scan_idx, \
66 .endianness = IIO_LE, \
70 struct st_lsm6dsx_reg {
77 struct st_lsm6dsx_odr {
82 #define ST_LSM6DSX_ODR_LIST_SIZE 6
83 struct st_lsm6dsx_odr_table_entry {
84 struct st_lsm6dsx_reg reg;
85 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
88 struct st_lsm6dsx_fs {
93 #define ST_LSM6DSX_FS_LIST_SIZE 4
94 struct st_lsm6dsx_fs_table_entry {
95 struct st_lsm6dsx_reg reg;
96 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
100 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
101 * @read_fifo: Read FIFO callback.
102 * @fifo_th: FIFO threshold register info (addr + mask).
103 * @fifo_diff: FIFO diff status register info (addr + mask).
104 * @th_wl: FIFO threshold word length.
106 struct st_lsm6dsx_fifo_ops {
107 int (*read_fifo)(struct st_lsm6dsx_hw *hw);
120 * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
121 * @timer_en: Hw timer enable register info (addr + mask).
122 * @hr_timer: Hw timer resolution register info (addr + mask).
123 * @fifo_en: Hw timer FIFO enable register info (addr + mask).
124 * @decimator: Hw timer FIFO decimator register info (addr + mask).
126 struct st_lsm6dsx_hw_ts_settings {
127 struct st_lsm6dsx_reg timer_en;
128 struct st_lsm6dsx_reg hr_timer;
129 struct st_lsm6dsx_reg fifo_en;
130 struct st_lsm6dsx_reg decimator;
134 * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
135 * @page_mux: register page mux info (addr + mask).
136 * @master_en: master config register info (addr + mask).
137 * @pullup_en: i2c controller pull-up register info (addr + mask).
138 * @aux_sens: aux sensor register info (addr + mask).
139 * @wr_once: write_once register info (addr + mask).
140 * @shub_out: sensor hub first output register info.
141 * @slv0_addr: slave0 address in secondary page.
142 * @dw_slv0_addr: slave0 write register address in secondary page.
143 * @batch_en: Enable/disable FIFO batching.
145 struct st_lsm6dsx_shub_settings {
146 struct st_lsm6dsx_reg page_mux;
147 struct st_lsm6dsx_reg master_en;
148 struct st_lsm6dsx_reg pullup_en;
149 struct st_lsm6dsx_reg aux_sens;
150 struct st_lsm6dsx_reg wr_once;
157 enum st_lsm6dsx_ext_sensor_id {
162 * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
163 * @i2c_addr: I2c slave address list.
164 * @wai: Wai address info.
165 * @id: external sensor id.
166 * @odr: Output data rate of the sensor [Hz].
167 * @gain: Configured sensor sensitivity.
168 * @temp_comp: Temperature compensation register info (addr + mask).
169 * @pwr_table: Power on register info (addr + mask).
170 * @off_canc: Offset cancellation register info (addr + mask).
171 * @bdu: Block data update register info (addr + mask).
172 * @out: Output register info.
174 struct st_lsm6dsx_ext_dev_settings {
180 enum st_lsm6dsx_ext_sensor_id id;
181 struct st_lsm6dsx_odr_table_entry odr_table;
182 struct st_lsm6dsx_fs_table_entry fs_table;
183 struct st_lsm6dsx_reg temp_comp;
185 struct st_lsm6dsx_reg reg;
189 struct st_lsm6dsx_reg off_canc;
190 struct st_lsm6dsx_reg bdu;
198 * struct st_lsm6dsx_settings - ST IMU sensor settings
199 * @wai: Sensor WhoAmI default value.
200 * @max_fifo_size: Sensor max fifo length in FIFO words.
201 * @id: List of hw id/device name supported by the driver configuration.
202 * @decimator: List of decimator register info (addr + mask).
203 * @batch: List of FIFO batching register info (addr + mask).
204 * @fifo_ops: Sensor hw FIFO parameters.
205 * @ts_settings: Hw timer related settings.
206 * @shub_settings: i2c controller related settings.
208 struct st_lsm6dsx_settings {
212 enum st_lsm6dsx_hw_id hw_id;
214 } id[ST_LSM6DSX_MAX_ID];
215 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
216 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
217 struct st_lsm6dsx_fifo_ops fifo_ops;
218 struct st_lsm6dsx_hw_ts_settings ts_settings;
219 struct st_lsm6dsx_shub_settings shub_settings;
222 enum st_lsm6dsx_sensor_id {
231 enum st_lsm6dsx_fifo_mode {
232 ST_LSM6DSX_FIFO_BYPASS = 0x0,
233 ST_LSM6DSX_FIFO_CONT = 0x6,
237 * struct st_lsm6dsx_sensor - ST IMU sensor instance
238 * @name: Sensor name.
239 * @id: Sensor identifier.
240 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
241 * @gain: Configured sensor sensitivity.
242 * @odr: Output data rate of the sensor [Hz].
243 * @watermark: Sensor watermark level.
244 * @sip: Number of samples in a given pattern.
245 * @decimator: FIFO decimation factor.
246 * @ts_ref: Sensor timestamp reference for hw one.
247 * @ext_info: Sensor settings if it is connected to i2c controller
249 struct st_lsm6dsx_sensor {
251 enum st_lsm6dsx_sensor_id id;
252 struct st_lsm6dsx_hw *hw;
263 const struct st_lsm6dsx_ext_dev_settings *settings;
269 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
270 * @dev: Pointer to instance of struct device (I2C or SPI).
271 * @regmap: Register map of the device.
272 * @irq: Device interrupt line (I2C or SPI).
273 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
274 * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
275 * @page_lock: Mutex to prevent concurrent memory page configuration.
276 * @fifo_mode: FIFO operating mode supported by the device.
277 * @enable_mask: Enabled sensor bitmask.
278 * @ts_sip: Total number of timestamp samples in a given pattern.
279 * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
280 * @buff: Device read buffer.
281 * @iio_devs: Pointers to acc/gyro iio_dev instances.
282 * @settings: Pointer to the specific sensor settings in use.
284 struct st_lsm6dsx_hw {
286 struct regmap *regmap;
289 struct mutex fifo_lock;
290 struct mutex conf_lock;
291 struct mutex page_lock;
293 enum st_lsm6dsx_fifo_mode fifo_mode;
300 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
302 const struct st_lsm6dsx_settings *settings;
305 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
306 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
308 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
309 struct regmap *regmap);
310 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
312 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
313 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
314 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
316 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
317 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
318 enum st_lsm6dsx_fifo_mode fifo_mode);
319 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
320 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
321 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);
322 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
323 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
324 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
327 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
328 unsigned int mask, unsigned int val)
332 mutex_lock(&hw->page_lock);
333 err = regmap_update_bits(hw->regmap, addr, mask, val);
334 mutex_unlock(&hw->page_lock);
340 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
341 void *val, unsigned int len)
345 mutex_lock(&hw->page_lock);
346 err = regmap_bulk_read(hw->regmap, addr, val, len);
347 mutex_unlock(&hw->page_lock);
353 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
358 mutex_lock(&hw->page_lock);
359 err = regmap_write(hw->regmap, addr, val);
360 mutex_unlock(&hw->page_lock);
365 #endif /* ST_LSM6DSX_H */