1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * STMicroelectronics st_lsm6dsx sensor driver
5 * Copyright 2016 STMicroelectronics Inc.
7 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
8 * Denis Ciocca <denis.ciocca@st.com>
14 #include <linux/device.h>
15 #include <linux/iio/iio.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"
26 #define ST_LSM6DS3TRC_DEV_NAME "lsm6ds3tr-c"
27 #define ST_ISM330DHCX_DEV_NAME "ism330dhcx"
28 #define ST_LSM9DS1_DEV_NAME "lsm9ds1-imu"
29 #define ST_LSM6DS0_DEV_NAME "lsm6ds0"
30 #define ST_LSM6DSRX_DEV_NAME "lsm6dsrx"
32 enum st_lsm6dsx_hw_id
{
50 #define ST_LSM6DSX_BUFF_SIZE 512
51 #define ST_LSM6DSX_CHAN_SIZE 2
52 #define ST_LSM6DSX_SAMPLE_SIZE 6
53 #define ST_LSM6DSX_TAG_SIZE 1
54 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE (ST_LSM6DSX_SAMPLE_SIZE + \
56 #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
57 ST_LSM6DSX_SAMPLE_SIZE)
58 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
59 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
60 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
62 #define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx) \
68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
69 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
70 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
71 .scan_index = scan_idx, \
76 .endianness = IIO_LE, \
78 .event_spec = &st_lsm6dsx_event, \
79 .num_event_specs = 1, \
82 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
88 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
89 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
90 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
91 .scan_index = scan_idx, \
96 .endianness = IIO_LE, \
100 struct st_lsm6dsx_reg
{
105 struct st_lsm6dsx_sensor
;
106 struct st_lsm6dsx_hw
;
108 struct st_lsm6dsx_odr
{
113 #define ST_LSM6DSX_ODR_LIST_SIZE 6
114 struct st_lsm6dsx_odr_table_entry
{
115 struct st_lsm6dsx_reg reg
;
117 struct st_lsm6dsx_odr odr_avl
[ST_LSM6DSX_ODR_LIST_SIZE
];
121 struct st_lsm6dsx_fs
{
126 #define ST_LSM6DSX_FS_LIST_SIZE 4
127 struct st_lsm6dsx_fs_table_entry
{
128 struct st_lsm6dsx_reg reg
;
130 struct st_lsm6dsx_fs fs_avl
[ST_LSM6DSX_FS_LIST_SIZE
];
135 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
136 * @update_fifo: Update FIFO configuration callback.
137 * @read_fifo: Read FIFO callback.
138 * @fifo_th: FIFO threshold register info (addr + mask).
139 * @fifo_diff: FIFO diff status register info (addr + mask).
140 * @th_wl: FIFO threshold word length.
142 struct st_lsm6dsx_fifo_ops
{
143 int (*update_fifo
)(struct st_lsm6dsx_sensor
*sensor
, bool enable
);
144 int (*read_fifo
)(struct st_lsm6dsx_hw
*hw
);
157 * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
158 * @timer_en: Hw timer enable register info (addr + mask).
159 * @hr_timer: Hw timer resolution register info (addr + mask).
160 * @fifo_en: Hw timer FIFO enable register info (addr + mask).
161 * @decimator: Hw timer FIFO decimator register info (addr + mask).
162 * @freq_fine: Difference in % of ODR with respect to the typical.
164 struct st_lsm6dsx_hw_ts_settings
{
165 struct st_lsm6dsx_reg timer_en
;
166 struct st_lsm6dsx_reg hr_timer
;
167 struct st_lsm6dsx_reg fifo_en
;
168 struct st_lsm6dsx_reg decimator
;
173 * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
174 * @page_mux: register page mux info (addr + mask).
175 * @master_en: master config register info (addr + mask).
176 * @pullup_en: i2c controller pull-up register info (addr + mask).
177 * @aux_sens: aux sensor register info (addr + mask).
178 * @wr_once: write_once register info (addr + mask).
179 * @emb_func: embedded function register info (addr + mask).
180 * @num_ext_dev: max number of slave devices.
181 * @shub_out: sensor hub first output register info.
182 * @slv0_addr: slave0 address in secondary page.
183 * @dw_slv0_addr: slave0 write register address in secondary page.
184 * @batch_en: Enable/disable FIFO batching.
185 * @pause: controller pause value.
187 struct st_lsm6dsx_shub_settings
{
188 struct st_lsm6dsx_reg page_mux
;
199 struct st_lsm6dsx_reg aux_sens
;
200 struct st_lsm6dsx_reg wr_once
;
201 struct st_lsm6dsx_reg emb_func
;
213 struct st_lsm6dsx_event_settings
{
214 struct st_lsm6dsx_reg enable_reg
;
215 struct st_lsm6dsx_reg wakeup_reg
;
217 u8 wakeup_src_status_mask
;
218 u8 wakeup_src_z_mask
;
219 u8 wakeup_src_y_mask
;
220 u8 wakeup_src_x_mask
;
223 enum st_lsm6dsx_ext_sensor_id
{
228 * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
229 * @i2c_addr: I2c slave address list.
230 * @wai: Wai address info.
231 * @id: external sensor id.
232 * @odr: Output data rate of the sensor [Hz].
233 * @gain: Configured sensor sensitivity.
234 * @temp_comp: Temperature compensation register info (addr + mask).
235 * @pwr_table: Power on register info (addr + mask).
236 * @off_canc: Offset cancellation register info (addr + mask).
237 * @bdu: Block data update register info (addr + mask).
238 * @out: Output register info.
240 struct st_lsm6dsx_ext_dev_settings
{
246 enum st_lsm6dsx_ext_sensor_id id
;
247 struct st_lsm6dsx_odr_table_entry odr_table
;
248 struct st_lsm6dsx_fs_table_entry fs_table
;
249 struct st_lsm6dsx_reg temp_comp
;
251 struct st_lsm6dsx_reg reg
;
255 struct st_lsm6dsx_reg off_canc
;
256 struct st_lsm6dsx_reg bdu
;
264 * struct st_lsm6dsx_settings - ST IMU sensor settings
265 * @wai: Sensor WhoAmI default value.
266 * @reset: register address for reset.
267 * @boot: register address for boot.
268 * @bdu: register address for Block Data Update.
269 * @max_fifo_size: Sensor max fifo length in FIFO words.
270 * @id: List of hw id/device name supported by the driver configuration.
271 * @channels: IIO channels supported by the device.
272 * @irq_config: interrupts related registers.
273 * @drdy_mask: register info for data-ready mask (addr + mask).
274 * @odr_table: Hw sensors odr table (Hz + val).
275 * @fs_table: Hw sensors gain table (gain + val).
276 * @decimator: List of decimator register info (addr + mask).
277 * @batch: List of FIFO batching register info (addr + mask).
278 * @fifo_ops: Sensor hw FIFO parameters.
279 * @ts_settings: Hw timer related settings.
280 * @shub_settings: i2c controller related settings.
282 struct st_lsm6dsx_settings
{
284 struct st_lsm6dsx_reg reset
;
285 struct st_lsm6dsx_reg boot
;
286 struct st_lsm6dsx_reg bdu
;
289 enum st_lsm6dsx_hw_id hw_id
;
291 } id
[ST_LSM6DSX_MAX_ID
];
293 const struct iio_chan_spec
*chan
;
297 struct st_lsm6dsx_reg irq1
;
298 struct st_lsm6dsx_reg irq2
;
299 struct st_lsm6dsx_reg irq1_func
;
300 struct st_lsm6dsx_reg irq2_func
;
301 struct st_lsm6dsx_reg lir
;
302 struct st_lsm6dsx_reg clear_on_read
;
303 struct st_lsm6dsx_reg hla
;
304 struct st_lsm6dsx_reg od
;
306 struct st_lsm6dsx_reg drdy_mask
;
307 struct st_lsm6dsx_odr_table_entry odr_table
[2];
308 struct st_lsm6dsx_fs_table_entry fs_table
[2];
309 struct st_lsm6dsx_reg decimator
[ST_LSM6DSX_MAX_ID
];
310 struct st_lsm6dsx_reg batch
[ST_LSM6DSX_MAX_ID
];
311 struct st_lsm6dsx_fifo_ops fifo_ops
;
312 struct st_lsm6dsx_hw_ts_settings ts_settings
;
313 struct st_lsm6dsx_shub_settings shub_settings
;
314 struct st_lsm6dsx_event_settings event_settings
;
317 enum st_lsm6dsx_sensor_id
{
326 enum st_lsm6dsx_fifo_mode
{
327 ST_LSM6DSX_FIFO_BYPASS
= 0x0,
328 ST_LSM6DSX_FIFO_CONT
= 0x6,
332 * struct st_lsm6dsx_sensor - ST IMU sensor instance
333 * @name: Sensor name.
334 * @id: Sensor identifier.
335 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
336 * @gain: Configured sensor sensitivity.
337 * @odr: Output data rate of the sensor [Hz].
338 * @watermark: Sensor watermark level.
339 * @sip: Number of samples in a given pattern.
340 * @ts_ref: Sensor timestamp reference for hw one.
341 * @ext_info: Sensor settings if it is connected to i2c controller
343 struct st_lsm6dsx_sensor
{
345 enum st_lsm6dsx_sensor_id id
;
346 struct st_lsm6dsx_hw
*hw
;
356 const struct st_lsm6dsx_ext_dev_settings
*settings
;
362 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
363 * @dev: Pointer to instance of struct device (I2C or SPI).
364 * @regmap: Register map of the device.
365 * @irq: Device interrupt line (I2C or SPI).
366 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
367 * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
368 * @page_lock: Mutex to prevent concurrent memory page configuration.
369 * @suspend_mask: Suspended sensor bitmask.
370 * @enable_mask: Enabled sensor bitmask.
371 * @fifo_mask: Enabled hw FIFO bitmask.
372 * @ts_gain: Hw timestamp rate after internal calibration.
373 * @ts_sip: Total number of timestamp samples in a given pattern.
374 * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
375 * @buff: Device read buffer.
376 * @irq_routing: pointer to interrupt routing configuration.
377 * @event_threshold: wakeup event threshold.
378 * @enable_event: enabled event bitmask.
379 * @iio_devs: Pointers to acc/gyro iio_dev instances.
380 * @settings: Pointer to the specific sensor settings in use.
382 struct st_lsm6dsx_hw
{
384 struct regmap
*regmap
;
387 struct mutex fifo_lock
;
388 struct mutex conf_lock
;
389 struct mutex page_lock
;
398 const struct st_lsm6dsx_reg
*irq_routing
;
404 struct iio_dev
*iio_devs
[ST_LSM6DSX_ID_MAX
];
406 const struct st_lsm6dsx_settings
*settings
;
409 static __maybe_unused
const struct iio_event_spec st_lsm6dsx_event
= {
410 .type
= IIO_EV_TYPE_THRESH
,
411 .dir
= IIO_EV_DIR_EITHER
,
412 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
413 BIT(IIO_EV_INFO_ENABLE
)
416 static __maybe_unused
const unsigned long st_lsm6dsx_available_scan_masks
[] = {
420 extern const struct dev_pm_ops st_lsm6dsx_pm_ops
;
422 int st_lsm6dsx_probe(struct device
*dev
, int irq
, int hw_id
,
423 struct regmap
*regmap
);
424 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor
*sensor
,
426 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw
*hw
);
427 int st_lsm6dsx_set_watermark(struct iio_dev
*iio_dev
, unsigned int val
);
428 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor
*sensor
,
430 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor
*sensor
, bool enable
);
431 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw
*hw
);
432 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw
*hw
,
433 enum st_lsm6dsx_fifo_mode fifo_mode
);
434 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw
*hw
);
435 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw
*hw
);
436 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor
*sensor
, u32 odr
, u8
*val
);
437 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw
*hw
, const char *name
);
438 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor
*sensor
, bool enable
);
439 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw
*hw
, bool enable
);
442 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw
*hw
, unsigned int addr
,
443 unsigned int mask
, unsigned int val
)
447 mutex_lock(&hw
->page_lock
);
448 err
= regmap_update_bits(hw
->regmap
, addr
, mask
, val
);
449 mutex_unlock(&hw
->page_lock
);
455 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw
*hw
, unsigned int addr
,
456 void *val
, unsigned int len
)
460 mutex_lock(&hw
->page_lock
);
461 err
= regmap_bulk_read(hw
->regmap
, addr
, val
, len
);
462 mutex_unlock(&hw
->page_lock
);
468 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw
*hw
, unsigned int addr
,
473 mutex_lock(&hw
->page_lock
);
474 err
= regmap_write(hw
->regmap
, addr
, val
);
475 mutex_unlock(&hw
->page_lock
);
480 #endif /* ST_LSM6DSX_H */