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"
24 enum st_lsm6dsx_hw_id {
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 + \
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))
46 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
52 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
53 BIT(IIO_CHAN_INFO_SCALE), \
54 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
55 .scan_index = scan_idx, \
60 .endianness = IIO_LE, \
64 struct st_lsm6dsx_reg {
71 struct st_lsm6dsx_odr {
76 #define ST_LSM6DSX_ODR_LIST_SIZE 6
77 struct st_lsm6dsx_odr_table_entry {
78 struct st_lsm6dsx_reg reg;
79 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
82 struct st_lsm6dsx_fs {
87 #define ST_LSM6DSX_FS_LIST_SIZE 4
88 struct st_lsm6dsx_fs_table_entry {
89 struct st_lsm6dsx_reg reg;
90 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
94 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
95 * @read_fifo: Read FIFO callback.
96 * @fifo_th: FIFO threshold register info (addr + mask).
97 * @fifo_diff: FIFO diff status register info (addr + mask).
98 * @th_wl: FIFO threshold word length.
100 struct st_lsm6dsx_fifo_ops {
101 int (*read_fifo)(struct st_lsm6dsx_hw *hw);
114 * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
115 * @timer_en: Hw timer enable register info (addr + mask).
116 * @hr_timer: Hw timer resolution register info (addr + mask).
117 * @fifo_en: Hw timer FIFO enable register info (addr + mask).
118 * @decimator: Hw timer FIFO decimator register info (addr + mask).
120 struct st_lsm6dsx_hw_ts_settings {
121 struct st_lsm6dsx_reg timer_en;
122 struct st_lsm6dsx_reg hr_timer;
123 struct st_lsm6dsx_reg fifo_en;
124 struct st_lsm6dsx_reg decimator;
128 * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
129 * @page_mux: register page mux info (addr + mask).
130 * @master_en: master config register info (addr + mask).
131 * @pullup_en: i2c controller pull-up register info (addr + mask).
132 * @aux_sens: aux sensor register info (addr + mask).
133 * @wr_once: write_once register info (addr + mask).
134 * @shub_out: sensor hub first output register info.
135 * @slv0_addr: slave0 address in secondary page.
136 * @dw_slv0_addr: slave0 write register address in secondary page.
137 * @batch_en: Enable/disable FIFO batching.
139 struct st_lsm6dsx_shub_settings {
140 struct st_lsm6dsx_reg page_mux;
141 struct st_lsm6dsx_reg master_en;
142 struct st_lsm6dsx_reg pullup_en;
143 struct st_lsm6dsx_reg aux_sens;
144 struct st_lsm6dsx_reg wr_once;
151 enum st_lsm6dsx_ext_sensor_id {
156 * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
157 * @i2c_addr: I2c slave address list.
158 * @wai: Wai address info.
159 * @id: external sensor id.
160 * @odr: Output data rate of the sensor [Hz].
161 * @gain: Configured sensor sensitivity.
162 * @temp_comp: Temperature compensation register info (addr + mask).
163 * @pwr_table: Power on register info (addr + mask).
164 * @off_canc: Offset cancellation register info (addr + mask).
165 * @bdu: Block data update register info (addr + mask).
166 * @out: Output register info.
168 struct st_lsm6dsx_ext_dev_settings {
174 enum st_lsm6dsx_ext_sensor_id id;
175 struct st_lsm6dsx_odr_table_entry odr_table;
176 struct st_lsm6dsx_fs_table_entry fs_table;
177 struct st_lsm6dsx_reg temp_comp;
179 struct st_lsm6dsx_reg reg;
183 struct st_lsm6dsx_reg off_canc;
184 struct st_lsm6dsx_reg bdu;
192 * struct st_lsm6dsx_settings - ST IMU sensor settings
193 * @wai: Sensor WhoAmI default value.
194 * @max_fifo_size: Sensor max fifo length in FIFO words.
195 * @id: List of hw id supported by the driver configuration.
196 * @decimator: List of decimator register info (addr + mask).
197 * @batch: List of FIFO batching register info (addr + mask).
198 * @fifo_ops: Sensor hw FIFO parameters.
199 * @ts_settings: Hw timer related settings.
200 * @shub_settings: i2c controller related settings.
202 struct st_lsm6dsx_settings {
205 enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID];
206 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
207 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
208 struct st_lsm6dsx_fifo_ops fifo_ops;
209 struct st_lsm6dsx_hw_ts_settings ts_settings;
210 struct st_lsm6dsx_shub_settings shub_settings;
213 enum st_lsm6dsx_sensor_id {
222 enum st_lsm6dsx_fifo_mode {
223 ST_LSM6DSX_FIFO_BYPASS = 0x0,
224 ST_LSM6DSX_FIFO_CONT = 0x6,
228 * struct st_lsm6dsx_sensor - ST IMU sensor instance
229 * @name: Sensor name.
230 * @id: Sensor identifier.
231 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
232 * @gain: Configured sensor sensitivity.
233 * @odr: Output data rate of the sensor [Hz].
234 * @watermark: Sensor watermark level.
235 * @sip: Number of samples in a given pattern.
236 * @decimator: FIFO decimation factor.
237 * @ts_ref: Sensor timestamp reference for hw one.
238 * @ext_info: Sensor settings if it is connected to i2c controller
240 struct st_lsm6dsx_sensor {
242 enum st_lsm6dsx_sensor_id id;
243 struct st_lsm6dsx_hw *hw;
254 const struct st_lsm6dsx_ext_dev_settings *settings;
260 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
261 * @dev: Pointer to instance of struct device (I2C or SPI).
262 * @regmap: Register map of the device.
263 * @irq: Device interrupt line (I2C or SPI).
264 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
265 * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
266 * @page_lock: Mutex to prevent concurrent memory page configuration.
267 * @fifo_mode: FIFO operating mode supported by the device.
268 * @enable_mask: Enabled sensor bitmask.
269 * @ts_sip: Total number of timestamp samples in a given pattern.
270 * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
271 * @buff: Device read buffer.
272 * @iio_devs: Pointers to acc/gyro iio_dev instances.
273 * @settings: Pointer to the specific sensor settings in use.
275 struct st_lsm6dsx_hw {
277 struct regmap *regmap;
280 struct mutex fifo_lock;
281 struct mutex conf_lock;
282 struct mutex page_lock;
284 enum st_lsm6dsx_fifo_mode fifo_mode;
291 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
293 const struct st_lsm6dsx_settings *settings;
296 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
297 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
299 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
300 struct regmap *regmap);
301 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
303 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
304 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
305 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
307 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
308 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
309 enum st_lsm6dsx_fifo_mode fifo_mode);
310 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
311 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
312 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);
313 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
314 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
315 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
318 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
319 unsigned int mask, unsigned int val)
323 mutex_lock(&hw->page_lock);
324 err = regmap_update_bits(hw->regmap, addr, mask, val);
325 mutex_unlock(&hw->page_lock);
331 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
332 void *val, unsigned int len)
336 mutex_lock(&hw->page_lock);
337 err = regmap_bulk_read(hw->regmap, addr, val, len);
338 mutex_unlock(&hw->page_lock);
344 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
349 mutex_lock(&hw->page_lock);
350 err = regmap_write(hw->regmap, addr, val);
351 mutex_unlock(&hw->page_lock);
356 #endif /* ST_LSM6DSX_H */