Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * STMicroelectronics st_lsm6dsx sensor driver
0004  *
0005  * Copyright 2016 STMicroelectronics Inc.
0006  *
0007  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
0008  * Denis Ciocca <denis.ciocca@st.com>
0009  */
0010 
0011 #ifndef ST_LSM6DSX_H
0012 #define ST_LSM6DSX_H
0013 
0014 #include <linux/device.h>
0015 #include <linux/iio/iio.h>
0016 #include <linux/regulator/consumer.h>
0017 
0018 #define ST_LSM6DS3_DEV_NAME "lsm6ds3"
0019 #define ST_LSM6DS3H_DEV_NAME    "lsm6ds3h"
0020 #define ST_LSM6DSL_DEV_NAME "lsm6dsl"
0021 #define ST_LSM6DSM_DEV_NAME "lsm6dsm"
0022 #define ST_ISM330DLC_DEV_NAME   "ism330dlc"
0023 #define ST_LSM6DSO_DEV_NAME "lsm6dso"
0024 #define ST_ASM330LHH_DEV_NAME   "asm330lhh"
0025 #define ST_LSM6DSOX_DEV_NAME    "lsm6dsox"
0026 #define ST_LSM6DSR_DEV_NAME "lsm6dsr"
0027 #define ST_LSM6DS3TRC_DEV_NAME  "lsm6ds3tr-c"
0028 #define ST_ISM330DHCX_DEV_NAME  "ism330dhcx"
0029 #define ST_LSM9DS1_DEV_NAME "lsm9ds1-imu"
0030 #define ST_LSM6DS0_DEV_NAME "lsm6ds0"
0031 #define ST_LSM6DSRX_DEV_NAME    "lsm6dsrx"
0032 #define ST_LSM6DST_DEV_NAME "lsm6dst"
0033 #define ST_LSM6DSOP_DEV_NAME    "lsm6dsop"
0034 #define ST_ASM330LHHX_DEV_NAME  "asm330lhhx"
0035 
0036 enum st_lsm6dsx_hw_id {
0037     ST_LSM6DS3_ID,
0038     ST_LSM6DS3H_ID,
0039     ST_LSM6DSL_ID,
0040     ST_LSM6DSM_ID,
0041     ST_ISM330DLC_ID,
0042     ST_LSM6DSO_ID,
0043     ST_ASM330LHH_ID,
0044     ST_LSM6DSOX_ID,
0045     ST_LSM6DSR_ID,
0046     ST_LSM6DS3TRC_ID,
0047     ST_ISM330DHCX_ID,
0048     ST_LSM9DS1_ID,
0049     ST_LSM6DS0_ID,
0050     ST_LSM6DSRX_ID,
0051     ST_LSM6DST_ID,
0052     ST_LSM6DSOP_ID,
0053     ST_ASM330LHHX_ID,
0054     ST_LSM6DSX_MAX_ID,
0055 };
0056 
0057 #define ST_LSM6DSX_BUFF_SIZE        512
0058 #define ST_LSM6DSX_CHAN_SIZE        2
0059 #define ST_LSM6DSX_SAMPLE_SIZE      6
0060 #define ST_LSM6DSX_TAG_SIZE     1
0061 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE   (ST_LSM6DSX_SAMPLE_SIZE + \
0062                      ST_LSM6DSX_TAG_SIZE)
0063 #define ST_LSM6DSX_MAX_WORD_LEN     ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
0064                      ST_LSM6DSX_SAMPLE_SIZE)
0065 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN  ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
0066                      * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
0067 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
0068 
0069 #define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx)      \
0070 {                                   \
0071     .type = chan_type,                      \
0072     .address = addr,                        \
0073     .modified = 1,                          \
0074     .channel2 = mod,                        \
0075     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
0076     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),       \
0077     .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),    \
0078     .scan_index = scan_idx,                     \
0079     .scan_type = {                          \
0080         .sign = 's',                        \
0081         .realbits = 16,                     \
0082         .storagebits = 16,                  \
0083         .endianness = IIO_LE,                   \
0084     },                              \
0085     .event_spec = &st_lsm6dsx_event,                \
0086     .ext_info = st_lsm6dsx_accel_ext_info,              \
0087     .num_event_specs = 1,                       \
0088 }
0089 
0090 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx)      \
0091 {                                   \
0092     .type = chan_type,                      \
0093     .address = addr,                        \
0094     .modified = 1,                          \
0095     .channel2 = mod,                        \
0096     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
0097     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),       \
0098     .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),    \
0099     .scan_index = scan_idx,                     \
0100     .scan_type = {                          \
0101         .sign = 's',                        \
0102         .realbits = 16,                     \
0103         .storagebits = 16,                  \
0104         .endianness = IIO_LE,                   \
0105     },                              \
0106 }
0107 
0108 struct st_lsm6dsx_reg {
0109     u8 addr;
0110     u8 mask;
0111 };
0112 
0113 struct st_lsm6dsx_sensor;
0114 struct st_lsm6dsx_hw;
0115 
0116 struct st_lsm6dsx_odr {
0117     u32 milli_hz;
0118     u8 val;
0119 };
0120 
0121 #define ST_LSM6DSX_ODR_LIST_SIZE    8
0122 struct st_lsm6dsx_odr_table_entry {
0123     struct st_lsm6dsx_reg reg;
0124 
0125     struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
0126     int odr_len;
0127 };
0128 
0129 struct st_lsm6dsx_fs {
0130     u32 gain;
0131     u8 val;
0132 };
0133 
0134 #define ST_LSM6DSX_FS_LIST_SIZE     4
0135 struct st_lsm6dsx_fs_table_entry {
0136     struct st_lsm6dsx_reg reg;
0137 
0138     struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
0139     int fs_len;
0140 };
0141 
0142 /**
0143  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
0144  * @update_fifo: Update FIFO configuration callback.
0145  * @read_fifo: Read FIFO callback.
0146  * @fifo_th: FIFO threshold register info (addr + mask).
0147  * @fifo_diff: FIFO diff status register info (addr + mask).
0148  * @max_size: Sensor max fifo length in FIFO words.
0149  * @th_wl: FIFO threshold word length.
0150  */
0151 struct st_lsm6dsx_fifo_ops {
0152     int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable);
0153     int (*read_fifo)(struct st_lsm6dsx_hw *hw);
0154     struct {
0155         u8 addr;
0156         u16 mask;
0157     } fifo_th;
0158     struct {
0159         u8 addr;
0160         u16 mask;
0161     } fifo_diff;
0162     u16 max_size;
0163     u8 th_wl;
0164 };
0165 
0166 /**
0167  * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
0168  * @timer_en: Hw timer enable register info (addr + mask).
0169  * @hr_timer: Hw timer resolution register info (addr + mask).
0170  * @fifo_en: Hw timer FIFO enable register info (addr + mask).
0171  * @decimator: Hw timer FIFO decimator register info (addr + mask).
0172  * @freq_fine: Difference in % of ODR with respect to the typical.
0173  */
0174 struct st_lsm6dsx_hw_ts_settings {
0175     struct st_lsm6dsx_reg timer_en;
0176     struct st_lsm6dsx_reg hr_timer;
0177     struct st_lsm6dsx_reg fifo_en;
0178     struct st_lsm6dsx_reg decimator;
0179     u8 freq_fine;
0180 };
0181 
0182 /**
0183  * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
0184  * @page_mux: register page mux info (addr + mask).
0185  * @master_en: master config register info (addr + mask).
0186  * @pullup_en: i2c controller pull-up register info (addr + mask).
0187  * @aux_sens: aux sensor register info (addr + mask).
0188  * @wr_once: write_once register info (addr + mask).
0189  * @emb_func:  embedded function register info (addr + mask).
0190  * @num_ext_dev: max number of slave devices.
0191  * @shub_out: sensor hub first output register info.
0192  * @slv0_addr: slave0 address in secondary page.
0193  * @dw_slv0_addr: slave0 write register address in secondary page.
0194  * @batch_en: Enable/disable FIFO batching.
0195  * @pause: controller pause value.
0196  */
0197 struct st_lsm6dsx_shub_settings {
0198     struct st_lsm6dsx_reg page_mux;
0199     struct {
0200         bool sec_page;
0201         u8 addr;
0202         u8 mask;
0203     } master_en;
0204     struct {
0205         bool sec_page;
0206         u8 addr;
0207         u8 mask;
0208     } pullup_en;
0209     struct st_lsm6dsx_reg aux_sens;
0210     struct st_lsm6dsx_reg wr_once;
0211     struct st_lsm6dsx_reg emb_func;
0212     u8 num_ext_dev;
0213     struct {
0214         bool sec_page;
0215         u8 addr;
0216     } shub_out;
0217     u8 slv0_addr;
0218     u8 dw_slv0_addr;
0219     u8 batch_en;
0220     u8 pause;
0221 };
0222 
0223 struct st_lsm6dsx_event_settings {
0224     struct st_lsm6dsx_reg enable_reg;
0225     struct st_lsm6dsx_reg wakeup_reg;
0226     u8 wakeup_src_reg;
0227     u8 wakeup_src_status_mask;
0228     u8 wakeup_src_z_mask;
0229     u8 wakeup_src_y_mask;
0230     u8 wakeup_src_x_mask;
0231 };
0232 
0233 enum st_lsm6dsx_ext_sensor_id {
0234     ST_LSM6DSX_ID_MAGN,
0235 };
0236 
0237 /**
0238  * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
0239  * @i2c_addr: I2c slave address list.
0240  * @wai: Wai address info.
0241  * @id: external sensor id.
0242  * @odr_table: Output data rate of the sensor [Hz].
0243  * @fs_table: Configured sensor sensitivity table depending on full scale.
0244  * @temp_comp: Temperature compensation register info (addr + mask).
0245  * @pwr_table: Power on register info (addr + mask).
0246  * @off_canc: Offset cancellation register info (addr + mask).
0247  * @bdu: Block data update register info (addr + mask).
0248  * @out: Output register info.
0249  */
0250 struct st_lsm6dsx_ext_dev_settings {
0251     u8 i2c_addr[2];
0252     struct {
0253         u8 addr;
0254         u8 val;
0255     } wai;
0256     enum st_lsm6dsx_ext_sensor_id id;
0257     struct st_lsm6dsx_odr_table_entry odr_table;
0258     struct st_lsm6dsx_fs_table_entry fs_table;
0259     struct st_lsm6dsx_reg temp_comp;
0260     struct {
0261         struct st_lsm6dsx_reg reg;
0262         u8 off_val;
0263         u8 on_val;
0264     } pwr_table;
0265     struct st_lsm6dsx_reg off_canc;
0266     struct st_lsm6dsx_reg bdu;
0267     struct {
0268         u8 addr;
0269         u8 len;
0270     } out;
0271 };
0272 
0273 /**
0274  * struct st_lsm6dsx_settings - ST IMU sensor settings
0275  * @reset: register address for reset.
0276  * @boot: register address for boot.
0277  * @bdu: register address for Block Data Update.
0278  * @id: List of hw id/device name supported by the driver configuration.
0279  * @channels: IIO channels supported by the device.
0280  * @irq_config: interrupts related registers.
0281  * @drdy_mask: register info for data-ready mask (addr + mask).
0282  * @odr_table: Hw sensors odr table (Hz + val).
0283  * @fs_table: Hw sensors gain table (gain + val).
0284  * @decimator: List of decimator register info (addr + mask).
0285  * @batch: List of FIFO batching register info (addr + mask).
0286  * @fifo_ops: Sensor hw FIFO parameters.
0287  * @ts_settings: Hw timer related settings.
0288  * @shub_settings: i2c controller related settings.
0289  */
0290 struct st_lsm6dsx_settings {
0291     struct st_lsm6dsx_reg reset;
0292     struct st_lsm6dsx_reg boot;
0293     struct st_lsm6dsx_reg bdu;
0294     struct {
0295         enum st_lsm6dsx_hw_id hw_id;
0296         const char *name;
0297         u8 wai;
0298     } id[ST_LSM6DSX_MAX_ID];
0299     struct {
0300         const struct iio_chan_spec *chan;
0301         int len;
0302     } channels[2];
0303     struct {
0304         struct st_lsm6dsx_reg irq1;
0305         struct st_lsm6dsx_reg irq2;
0306         struct st_lsm6dsx_reg irq1_func;
0307         struct st_lsm6dsx_reg irq2_func;
0308         struct st_lsm6dsx_reg lir;
0309         struct st_lsm6dsx_reg clear_on_read;
0310         struct st_lsm6dsx_reg hla;
0311         struct st_lsm6dsx_reg od;
0312     } irq_config;
0313     struct st_lsm6dsx_reg drdy_mask;
0314     struct st_lsm6dsx_odr_table_entry odr_table[2];
0315     struct st_lsm6dsx_fs_table_entry fs_table[2];
0316     struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
0317     struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
0318     struct st_lsm6dsx_fifo_ops fifo_ops;
0319     struct st_lsm6dsx_hw_ts_settings ts_settings;
0320     struct st_lsm6dsx_shub_settings shub_settings;
0321     struct st_lsm6dsx_event_settings event_settings;
0322 };
0323 
0324 enum st_lsm6dsx_sensor_id {
0325     ST_LSM6DSX_ID_GYRO,
0326     ST_LSM6DSX_ID_ACC,
0327     ST_LSM6DSX_ID_EXT0,
0328     ST_LSM6DSX_ID_EXT1,
0329     ST_LSM6DSX_ID_EXT2,
0330     ST_LSM6DSX_ID_MAX,
0331 };
0332 
0333 enum st_lsm6dsx_fifo_mode {
0334     ST_LSM6DSX_FIFO_BYPASS = 0x0,
0335     ST_LSM6DSX_FIFO_CONT = 0x6,
0336 };
0337 
0338 /**
0339  * struct st_lsm6dsx_sensor - ST IMU sensor instance
0340  * @name: Sensor name.
0341  * @id: Sensor identifier.
0342  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
0343  * @gain: Configured sensor sensitivity.
0344  * @odr: Output data rate of the sensor [Hz].
0345  * @watermark: Sensor watermark level.
0346  * @decimator: Sensor decimation factor.
0347  * @sip: Number of samples in a given pattern.
0348  * @ts_ref: Sensor timestamp reference for hw one.
0349  * @ext_info: Sensor settings if it is connected to i2c controller
0350  */
0351 struct st_lsm6dsx_sensor {
0352     char name[32];
0353     enum st_lsm6dsx_sensor_id id;
0354     struct st_lsm6dsx_hw *hw;
0355 
0356     u32 gain;
0357     u32 odr;
0358 
0359     u16 watermark;
0360     u8 decimator;
0361     u8 sip;
0362     s64 ts_ref;
0363 
0364     struct {
0365         const struct st_lsm6dsx_ext_dev_settings *settings;
0366         u32 slv_odr;
0367         u8 addr;
0368     } ext_info;
0369 };
0370 
0371 /**
0372  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
0373  * @dev: Pointer to instance of struct device (I2C or SPI).
0374  * @regmap: Register map of the device.
0375  * @regulators: VDD/VDDIO voltage regulators.
0376  * @irq: Device interrupt line (I2C or SPI).
0377  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
0378  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
0379  * @page_lock: Mutex to prevent concurrent memory page configuration.
0380  * @suspend_mask: Suspended sensor bitmask.
0381  * @enable_mask: Enabled sensor bitmask.
0382  * @fifo_mask: Enabled hw FIFO bitmask.
0383  * @ts_gain: Hw timestamp rate after internal calibration.
0384  * @ts_sip: Total number of timestamp samples in a given pattern.
0385  * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
0386  * @buff: Device read buffer.
0387  * @irq_routing: pointer to interrupt routing configuration.
0388  * @event_threshold: wakeup event threshold.
0389  * @enable_event: enabled event bitmask.
0390  * @iio_devs: Pointers to acc/gyro iio_dev instances.
0391  * @settings: Pointer to the specific sensor settings in use.
0392  * @orientation: sensor chip orientation relative to main hardware.
0393  * @scan: Temporary buffers used to align data before iio_push_to_buffers()
0394  */
0395 struct st_lsm6dsx_hw {
0396     struct device *dev;
0397     struct regmap *regmap;
0398     struct regulator_bulk_data regulators[2];
0399     int irq;
0400 
0401     struct mutex fifo_lock;
0402     struct mutex conf_lock;
0403     struct mutex page_lock;
0404 
0405     u8 suspend_mask;
0406     u8 enable_mask;
0407     u8 fifo_mask;
0408     s64 ts_gain;
0409     u8 ts_sip;
0410     u8 sip;
0411 
0412     const struct st_lsm6dsx_reg *irq_routing;
0413     u8 event_threshold;
0414     u8 enable_event;
0415 
0416     u8 *buff;
0417 
0418     struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
0419 
0420     const struct st_lsm6dsx_settings *settings;
0421 
0422     struct iio_mount_matrix orientation;
0423     /* Ensure natural alignment of buffer elements */
0424     struct {
0425         __le16 channels[3];
0426         s64 ts __aligned(8);
0427     } scan[3];
0428 };
0429 
0430 static __maybe_unused const struct iio_event_spec st_lsm6dsx_event = {
0431     .type = IIO_EV_TYPE_THRESH,
0432     .dir = IIO_EV_DIR_EITHER,
0433     .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0434              BIT(IIO_EV_INFO_ENABLE)
0435 };
0436 
0437 static __maybe_unused const unsigned long st_lsm6dsx_available_scan_masks[] = {
0438     0x7, 0x0,
0439 };
0440 
0441 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
0442 
0443 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
0444              struct regmap *regmap);
0445 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
0446                  bool enable);
0447 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
0448 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
0449 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
0450                 u16 watermark);
0451 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable);
0452 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
0453 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw);
0454 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
0455 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
0456 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val);
0457 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
0458 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
0459 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
0460 
0461 static inline int
0462 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
0463                   unsigned int mask, unsigned int val)
0464 {
0465     int err;
0466 
0467     mutex_lock(&hw->page_lock);
0468     err = regmap_update_bits(hw->regmap, addr, mask, val);
0469     mutex_unlock(&hw->page_lock);
0470 
0471     return err;
0472 }
0473 
0474 static inline int
0475 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
0476                void *val, unsigned int len)
0477 {
0478     int err;
0479 
0480     mutex_lock(&hw->page_lock);
0481     err = regmap_bulk_read(hw->regmap, addr, val, len);
0482     mutex_unlock(&hw->page_lock);
0483 
0484     return err;
0485 }
0486 
0487 static inline int
0488 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
0489             unsigned int val)
0490 {
0491     int err;
0492 
0493     mutex_lock(&hw->page_lock);
0494     err = regmap_write(hw->regmap, addr, val);
0495     mutex_unlock(&hw->page_lock);
0496 
0497     return err;
0498 }
0499 
0500 static inline const struct iio_mount_matrix *
0501 st_lsm6dsx_get_mount_matrix(const struct iio_dev *iio_dev,
0502                 const struct iio_chan_spec *chan)
0503 {
0504     struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
0505     struct st_lsm6dsx_hw *hw = sensor->hw;
0506 
0507     return &hw->orientation;
0508 }
0509 
0510 static const
0511 struct iio_chan_spec_ext_info __maybe_unused st_lsm6dsx_accel_ext_info[] = {
0512     IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_lsm6dsx_get_mount_matrix),
0513     { }
0514 };
0515 
0516 #endif /* ST_LSM6DSX_H */