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>
16 #include <linux/regulator/consumer.h>
18 #define ST_LSM6DS3_DEV_NAME "lsm6ds3"
19 #define ST_LSM6DS3H_DEV_NAME "lsm6ds3h"
20 #define ST_LSM6DSL_DEV_NAME "lsm6dsl"
21 #define ST_LSM6DSM_DEV_NAME "lsm6dsm"
22 #define ST_ISM330DLC_DEV_NAME "ism330dlc"
23 #define ST_LSM6DSO_DEV_NAME "lsm6dso"
24 #define ST_ASM330LHH_DEV_NAME "asm330lhh"
25 #define ST_LSM6DSOX_DEV_NAME "lsm6dsox"
26 #define ST_LSM6DSR_DEV_NAME "lsm6dsr"
27 #define ST_LSM6DS3TRC_DEV_NAME "lsm6ds3tr-c"
28 #define ST_ISM330DHCX_DEV_NAME "ism330dhcx"
29 #define ST_LSM9DS1_DEV_NAME "lsm9ds1-imu"
30 #define ST_LSM6DS0_DEV_NAME "lsm6ds0"
31 #define ST_LSM6DSRX_DEV_NAME "lsm6dsrx"
32 #define ST_LSM6DST_DEV_NAME "lsm6dst"
33 #define ST_LSM6DSOP_DEV_NAME "lsm6dsop"
35 enum st_lsm6dsx_hw_id
{
55 #define ST_LSM6DSX_BUFF_SIZE 512
56 #define ST_LSM6DSX_CHAN_SIZE 2
57 #define ST_LSM6DSX_SAMPLE_SIZE 6
58 #define ST_LSM6DSX_TAG_SIZE 1
59 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE (ST_LSM6DSX_SAMPLE_SIZE + \
61 #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
62 ST_LSM6DSX_SAMPLE_SIZE)
63 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
64 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
65 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
67 #define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx) \
73 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
74 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
75 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
76 .scan_index = scan_idx, \
81 .endianness = IIO_LE, \
83 .event_spec = &st_lsm6dsx_event, \
84 .ext_info = st_lsm6dsx_accel_ext_info, \
85 .num_event_specs = 1, \
88 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
95 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
96 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
97 .scan_index = scan_idx, \
102 .endianness = IIO_LE, \
106 struct st_lsm6dsx_reg
{
111 struct st_lsm6dsx_sensor
;
112 struct st_lsm6dsx_hw
;
114 struct st_lsm6dsx_odr
{
119 #define ST_LSM6DSX_ODR_LIST_SIZE 8
120 struct st_lsm6dsx_odr_table_entry
{
121 struct st_lsm6dsx_reg reg
;
123 struct st_lsm6dsx_odr odr_avl
[ST_LSM6DSX_ODR_LIST_SIZE
];
127 struct st_lsm6dsx_fs
{
132 #define ST_LSM6DSX_FS_LIST_SIZE 4
133 struct st_lsm6dsx_fs_table_entry
{
134 struct st_lsm6dsx_reg reg
;
136 struct st_lsm6dsx_fs fs_avl
[ST_LSM6DSX_FS_LIST_SIZE
];
141 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
142 * @update_fifo: Update FIFO configuration callback.
143 * @read_fifo: Read FIFO callback.
144 * @fifo_th: FIFO threshold register info (addr + mask).
145 * @fifo_diff: FIFO diff status register info (addr + mask).
146 * @th_wl: FIFO threshold word length.
148 struct st_lsm6dsx_fifo_ops
{
149 int (*update_fifo
)(struct st_lsm6dsx_sensor
*sensor
, bool enable
);
150 int (*read_fifo
)(struct st_lsm6dsx_hw
*hw
);
163 * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
164 * @timer_en: Hw timer enable register info (addr + mask).
165 * @hr_timer: Hw timer resolution register info (addr + mask).
166 * @fifo_en: Hw timer FIFO enable register info (addr + mask).
167 * @decimator: Hw timer FIFO decimator register info (addr + mask).
168 * @freq_fine: Difference in % of ODR with respect to the typical.
170 struct st_lsm6dsx_hw_ts_settings
{
171 struct st_lsm6dsx_reg timer_en
;
172 struct st_lsm6dsx_reg hr_timer
;
173 struct st_lsm6dsx_reg fifo_en
;
174 struct st_lsm6dsx_reg decimator
;
179 * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
180 * @page_mux: register page mux info (addr + mask).
181 * @master_en: master config register info (addr + mask).
182 * @pullup_en: i2c controller pull-up register info (addr + mask).
183 * @aux_sens: aux sensor register info (addr + mask).
184 * @wr_once: write_once register info (addr + mask).
185 * @emb_func: embedded function register info (addr + mask).
186 * @num_ext_dev: max number of slave devices.
187 * @shub_out: sensor hub first output register info.
188 * @slv0_addr: slave0 address in secondary page.
189 * @dw_slv0_addr: slave0 write register address in secondary page.
190 * @batch_en: Enable/disable FIFO batching.
191 * @pause: controller pause value.
193 struct st_lsm6dsx_shub_settings
{
194 struct st_lsm6dsx_reg page_mux
;
205 struct st_lsm6dsx_reg aux_sens
;
206 struct st_lsm6dsx_reg wr_once
;
207 struct st_lsm6dsx_reg emb_func
;
219 struct st_lsm6dsx_event_settings
{
220 struct st_lsm6dsx_reg enable_reg
;
221 struct st_lsm6dsx_reg wakeup_reg
;
223 u8 wakeup_src_status_mask
;
224 u8 wakeup_src_z_mask
;
225 u8 wakeup_src_y_mask
;
226 u8 wakeup_src_x_mask
;
229 enum st_lsm6dsx_ext_sensor_id
{
234 * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
235 * @i2c_addr: I2c slave address list.
236 * @wai: Wai address info.
237 * @id: external sensor id.
238 * @odr_table: Output data rate of the sensor [Hz].
239 * @fs_table: Configured sensor sensitivity table depending on full scale.
240 * @temp_comp: Temperature compensation register info (addr + mask).
241 * @pwr_table: Power on register info (addr + mask).
242 * @off_canc: Offset cancellation register info (addr + mask).
243 * @bdu: Block data update register info (addr + mask).
244 * @out: Output register info.
246 struct st_lsm6dsx_ext_dev_settings
{
252 enum st_lsm6dsx_ext_sensor_id id
;
253 struct st_lsm6dsx_odr_table_entry odr_table
;
254 struct st_lsm6dsx_fs_table_entry fs_table
;
255 struct st_lsm6dsx_reg temp_comp
;
257 struct st_lsm6dsx_reg reg
;
261 struct st_lsm6dsx_reg off_canc
;
262 struct st_lsm6dsx_reg bdu
;
270 * struct st_lsm6dsx_settings - ST IMU sensor settings
271 * @reset: register address for reset.
272 * @boot: register address for boot.
273 * @bdu: register address for Block Data Update.
274 * @max_fifo_size: Sensor max fifo length in FIFO words.
275 * @id: List of hw id/device name supported by the driver configuration.
276 * @channels: IIO channels supported by the device.
277 * @irq_config: interrupts related registers.
278 * @drdy_mask: register info for data-ready mask (addr + mask).
279 * @odr_table: Hw sensors odr table (Hz + val).
280 * @fs_table: Hw sensors gain table (gain + val).
281 * @decimator: List of decimator register info (addr + mask).
282 * @batch: List of FIFO batching register info (addr + mask).
283 * @fifo_ops: Sensor hw FIFO parameters.
284 * @ts_settings: Hw timer related settings.
285 * @shub_settings: i2c controller related settings.
287 struct st_lsm6dsx_settings
{
288 struct st_lsm6dsx_reg reset
;
289 struct st_lsm6dsx_reg boot
;
290 struct st_lsm6dsx_reg bdu
;
293 enum st_lsm6dsx_hw_id hw_id
;
296 } id
[ST_LSM6DSX_MAX_ID
];
298 const struct iio_chan_spec
*chan
;
302 struct st_lsm6dsx_reg irq1
;
303 struct st_lsm6dsx_reg irq2
;
304 struct st_lsm6dsx_reg irq1_func
;
305 struct st_lsm6dsx_reg irq2_func
;
306 struct st_lsm6dsx_reg lir
;
307 struct st_lsm6dsx_reg clear_on_read
;
308 struct st_lsm6dsx_reg hla
;
309 struct st_lsm6dsx_reg od
;
311 struct st_lsm6dsx_reg drdy_mask
;
312 struct st_lsm6dsx_odr_table_entry odr_table
[2];
313 struct st_lsm6dsx_fs_table_entry fs_table
[2];
314 struct st_lsm6dsx_reg decimator
[ST_LSM6DSX_MAX_ID
];
315 struct st_lsm6dsx_reg batch
[ST_LSM6DSX_MAX_ID
];
316 struct st_lsm6dsx_fifo_ops fifo_ops
;
317 struct st_lsm6dsx_hw_ts_settings ts_settings
;
318 struct st_lsm6dsx_shub_settings shub_settings
;
319 struct st_lsm6dsx_event_settings event_settings
;
322 enum st_lsm6dsx_sensor_id
{
331 enum st_lsm6dsx_fifo_mode
{
332 ST_LSM6DSX_FIFO_BYPASS
= 0x0,
333 ST_LSM6DSX_FIFO_CONT
= 0x6,
337 * struct st_lsm6dsx_sensor - ST IMU sensor instance
338 * @name: Sensor name.
339 * @id: Sensor identifier.
340 * @hw: Pointer to instance of struct st_lsm6dsx_hw.
341 * @gain: Configured sensor sensitivity.
342 * @odr: Output data rate of the sensor [Hz].
343 * @watermark: Sensor watermark level.
344 * @decimator: Sensor decimation factor.
345 * @sip: Number of samples in a given pattern.
346 * @ts_ref: Sensor timestamp reference for hw one.
347 * @ext_info: Sensor settings if it is connected to i2c controller
349 struct st_lsm6dsx_sensor
{
351 enum st_lsm6dsx_sensor_id id
;
352 struct st_lsm6dsx_hw
*hw
;
363 const struct st_lsm6dsx_ext_dev_settings
*settings
;
370 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
371 * @dev: Pointer to instance of struct device (I2C or SPI).
372 * @regmap: Register map of the device.
373 * @regulators: VDD/VDDIO voltage regulators.
374 * @irq: Device interrupt line (I2C or SPI).
375 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
376 * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
377 * @page_lock: Mutex to prevent concurrent memory page configuration.
378 * @suspend_mask: Suspended sensor bitmask.
379 * @enable_mask: Enabled sensor bitmask.
380 * @fifo_mask: Enabled hw FIFO bitmask.
381 * @ts_gain: Hw timestamp rate after internal calibration.
382 * @ts_sip: Total number of timestamp samples in a given pattern.
383 * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
384 * @buff: Device read buffer.
385 * @irq_routing: pointer to interrupt routing configuration.
386 * @event_threshold: wakeup event threshold.
387 * @enable_event: enabled event bitmask.
388 * @iio_devs: Pointers to acc/gyro iio_dev instances.
389 * @settings: Pointer to the specific sensor settings in use.
390 * @orientation: sensor chip orientation relative to main hardware.
391 * @scan: Temporary buffers used to align data before iio_push_to_buffers()
393 struct st_lsm6dsx_hw
{
395 struct regmap
*regmap
;
396 struct regulator_bulk_data regulators
[2];
399 struct mutex fifo_lock
;
400 struct mutex conf_lock
;
401 struct mutex page_lock
;
410 const struct st_lsm6dsx_reg
*irq_routing
;
416 struct iio_dev
*iio_devs
[ST_LSM6DSX_ID_MAX
];
418 const struct st_lsm6dsx_settings
*settings
;
420 struct iio_mount_matrix orientation
;
421 /* Ensure natural alignment of buffer elements */
428 static __maybe_unused
const struct iio_event_spec st_lsm6dsx_event
= {
429 .type
= IIO_EV_TYPE_THRESH
,
430 .dir
= IIO_EV_DIR_EITHER
,
431 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
432 BIT(IIO_EV_INFO_ENABLE
)
435 static __maybe_unused
const unsigned long st_lsm6dsx_available_scan_masks
[] = {
439 extern const struct dev_pm_ops st_lsm6dsx_pm_ops
;
441 int st_lsm6dsx_probe(struct device
*dev
, int irq
, int hw_id
,
442 struct regmap
*regmap
);
443 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor
*sensor
,
445 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw
*hw
);
446 int st_lsm6dsx_set_watermark(struct iio_dev
*iio_dev
, unsigned int val
);
447 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor
*sensor
,
449 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor
*sensor
, bool enable
);
450 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw
*hw
);
451 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw
*hw
);
452 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw
*hw
);
453 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw
*hw
);
454 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor
*sensor
, u32 odr
, u8
*val
);
455 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw
*hw
, const char *name
);
456 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor
*sensor
, bool enable
);
457 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw
*hw
, bool enable
);
460 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw
*hw
, unsigned int addr
,
461 unsigned int mask
, unsigned int val
)
465 mutex_lock(&hw
->page_lock
);
466 err
= regmap_update_bits(hw
->regmap
, addr
, mask
, val
);
467 mutex_unlock(&hw
->page_lock
);
473 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw
*hw
, unsigned int addr
,
474 void *val
, unsigned int len
)
478 mutex_lock(&hw
->page_lock
);
479 err
= regmap_bulk_read(hw
->regmap
, addr
, val
, len
);
480 mutex_unlock(&hw
->page_lock
);
486 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw
*hw
, unsigned int addr
,
491 mutex_lock(&hw
->page_lock
);
492 err
= regmap_write(hw
->regmap
, addr
, val
);
493 mutex_unlock(&hw
->page_lock
);
498 static inline const struct iio_mount_matrix
*
499 st_lsm6dsx_get_mount_matrix(const struct iio_dev
*iio_dev
,
500 const struct iio_chan_spec
*chan
)
502 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
503 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
505 return &hw
->orientation
;
509 struct iio_chan_spec_ext_info __maybe_unused st_lsm6dsx_accel_ext_info
[] = {
510 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL
, st_lsm6dsx_get_mount_matrix
),
514 #endif /* ST_LSM6DSX_H */