1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * STMicroelectronics st_lsm6dsx sensor driver 4 * 5 * Copyright 2016 STMicroelectronics Inc. 6 * 7 * Lorenzo Bianconi <lorenzo.bianconi@st.com> 8 * Denis Ciocca <denis.ciocca@st.com> 9 */ 10 11 #ifndef ST_LSM6DSX_H 12 #define ST_LSM6DSX_H 13 14 #include <linux/device.h> 15 16 #define ST_LSM6DS3_DEV_NAME "lsm6ds3" 17 #define ST_LSM6DS3H_DEV_NAME "lsm6ds3h" 18 #define ST_LSM6DSL_DEV_NAME "lsm6dsl" 19 #define ST_LSM6DSM_DEV_NAME "lsm6dsm" 20 #define ST_ISM330DLC_DEV_NAME "ism330dlc" 21 #define ST_LSM6DSO_DEV_NAME "lsm6dso" 22 #define ST_ASM330LHH_DEV_NAME "asm330lhh" 23 #define ST_LSM6DSOX_DEV_NAME "lsm6dsox" 24 #define ST_LSM6DSR_DEV_NAME "lsm6dsr" 25 #define ST_LSM6DS3TRC_DEV_NAME "lsm6ds3tr-c" 26 #define ST_ISM330DHCX_DEV_NAME "ism330dhcx" 27 #define ST_LSM9DS1_DEV_NAME "lsm9ds1-imu" 28 29 enum st_lsm6dsx_hw_id { 30 ST_LSM6DS3_ID, 31 ST_LSM6DS3H_ID, 32 ST_LSM6DSL_ID, 33 ST_LSM6DSM_ID, 34 ST_ISM330DLC_ID, 35 ST_LSM6DSO_ID, 36 ST_ASM330LHH_ID, 37 ST_LSM6DSOX_ID, 38 ST_LSM6DSR_ID, 39 ST_LSM6DS3TRC_ID, 40 ST_ISM330DHCX_ID, 41 ST_LSM9DS1_ID, 42 ST_LSM6DSX_MAX_ID, 43 }; 44 45 #define ST_LSM6DSX_BUFF_SIZE 512 46 #define ST_LSM6DSX_CHAN_SIZE 2 47 #define ST_LSM6DSX_SAMPLE_SIZE 6 48 #define ST_LSM6DSX_TAG_SIZE 1 49 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE (ST_LSM6DSX_SAMPLE_SIZE + \ 50 ST_LSM6DSX_TAG_SIZE) 51 #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \ 52 ST_LSM6DSX_SAMPLE_SIZE) 53 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \ 54 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE) 55 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask)) 56 57 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \ 58 { \ 59 .type = chan_type, \ 60 .address = addr, \ 61 .modified = 1, \ 62 .channel2 = mod, \ 63 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 64 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 65 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 66 .scan_index = scan_idx, \ 67 .scan_type = { \ 68 .sign = 's', \ 69 .realbits = 16, \ 70 .storagebits = 16, \ 71 .endianness = IIO_LE, \ 72 }, \ 73 } 74 75 struct st_lsm6dsx_reg { 76 u8 addr; 77 u8 mask; 78 }; 79 80 struct st_lsm6dsx_sensor; 81 struct st_lsm6dsx_hw; 82 83 struct st_lsm6dsx_odr { 84 u16 hz; 85 u8 val; 86 }; 87 88 #define ST_LSM6DSX_ODR_LIST_SIZE 6 89 struct st_lsm6dsx_odr_table_entry { 90 struct st_lsm6dsx_reg reg; 91 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE]; 92 }; 93 94 struct st_lsm6dsx_fs { 95 u32 gain; 96 u8 val; 97 }; 98 99 #define ST_LSM6DSX_FS_LIST_SIZE 4 100 struct st_lsm6dsx_fs_table_entry { 101 struct st_lsm6dsx_reg reg; 102 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE]; 103 }; 104 105 /** 106 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings 107 * @update_fifo: Update FIFO configuration callback. 108 * @read_fifo: Read FIFO callback. 109 * @fifo_th: FIFO threshold register info (addr + mask). 110 * @fifo_diff: FIFO diff status register info (addr + mask). 111 * @th_wl: FIFO threshold word length. 112 */ 113 struct st_lsm6dsx_fifo_ops { 114 int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable); 115 int (*read_fifo)(struct st_lsm6dsx_hw *hw); 116 struct { 117 u8 addr; 118 u16 mask; 119 } fifo_th; 120 struct { 121 u8 addr; 122 u16 mask; 123 } fifo_diff; 124 u8 th_wl; 125 }; 126 127 /** 128 * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings 129 * @timer_en: Hw timer enable register info (addr + mask). 130 * @hr_timer: Hw timer resolution register info (addr + mask). 131 * @fifo_en: Hw timer FIFO enable register info (addr + mask). 132 * @decimator: Hw timer FIFO decimator register info (addr + mask). 133 */ 134 struct st_lsm6dsx_hw_ts_settings { 135 struct st_lsm6dsx_reg timer_en; 136 struct st_lsm6dsx_reg hr_timer; 137 struct st_lsm6dsx_reg fifo_en; 138 struct st_lsm6dsx_reg decimator; 139 }; 140 141 /** 142 * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings 143 * @page_mux: register page mux info (addr + mask). 144 * @master_en: master config register info (addr + mask). 145 * @pullup_en: i2c controller pull-up register info (addr + mask). 146 * @aux_sens: aux sensor register info (addr + mask). 147 * @wr_once: write_once register info (addr + mask). 148 * @shub_out: sensor hub first output register info. 149 * @slv0_addr: slave0 address in secondary page. 150 * @dw_slv0_addr: slave0 write register address in secondary page. 151 * @batch_en: Enable/disable FIFO batching. 152 */ 153 struct st_lsm6dsx_shub_settings { 154 struct st_lsm6dsx_reg page_mux; 155 struct st_lsm6dsx_reg master_en; 156 struct st_lsm6dsx_reg pullup_en; 157 struct st_lsm6dsx_reg aux_sens; 158 struct st_lsm6dsx_reg wr_once; 159 u8 shub_out; 160 u8 slv0_addr; 161 u8 dw_slv0_addr; 162 u8 batch_en; 163 }; 164 165 enum st_lsm6dsx_ext_sensor_id { 166 ST_LSM6DSX_ID_MAGN, 167 }; 168 169 /** 170 * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings 171 * @i2c_addr: I2c slave address list. 172 * @wai: Wai address info. 173 * @id: external sensor id. 174 * @odr: Output data rate of the sensor [Hz]. 175 * @gain: Configured sensor sensitivity. 176 * @temp_comp: Temperature compensation register info (addr + mask). 177 * @pwr_table: Power on register info (addr + mask). 178 * @off_canc: Offset cancellation register info (addr + mask). 179 * @bdu: Block data update register info (addr + mask). 180 * @out: Output register info. 181 */ 182 struct st_lsm6dsx_ext_dev_settings { 183 u8 i2c_addr[2]; 184 struct { 185 u8 addr; 186 u8 val; 187 } wai; 188 enum st_lsm6dsx_ext_sensor_id id; 189 struct st_lsm6dsx_odr_table_entry odr_table; 190 struct st_lsm6dsx_fs_table_entry fs_table; 191 struct st_lsm6dsx_reg temp_comp; 192 struct { 193 struct st_lsm6dsx_reg reg; 194 u8 off_val; 195 u8 on_val; 196 } pwr_table; 197 struct st_lsm6dsx_reg off_canc; 198 struct st_lsm6dsx_reg bdu; 199 struct { 200 u8 addr; 201 u8 len; 202 } out; 203 }; 204 205 /** 206 * struct st_lsm6dsx_settings - ST IMU sensor settings 207 * @wai: Sensor WhoAmI default value. 208 * @int1_addr: Control Register address for INT1 209 * @int2_addr: Control Register address for INT2 210 * @reset_addr: register address for reset/reboot 211 * @max_fifo_size: Sensor max fifo length in FIFO words. 212 * @id: List of hw id/device name supported by the driver configuration. 213 * @channels: IIO channels supported by the device. 214 * @odr_table: Hw sensors odr table (Hz + val). 215 * @fs_table: Hw sensors gain table (gain + val). 216 * @decimator: List of decimator register info (addr + mask). 217 * @batch: List of FIFO batching register info (addr + mask). 218 * @fifo_ops: Sensor hw FIFO parameters. 219 * @ts_settings: Hw timer related settings. 220 * @shub_settings: i2c controller related settings. 221 */ 222 struct st_lsm6dsx_settings { 223 u8 wai; 224 u8 int1_addr; 225 u8 int2_addr; 226 u8 reset_addr; 227 u16 max_fifo_size; 228 struct { 229 enum st_lsm6dsx_hw_id hw_id; 230 const char *name; 231 } id[ST_LSM6DSX_MAX_ID]; 232 struct { 233 const struct iio_chan_spec *chan; 234 int len; 235 } channels[2]; 236 struct st_lsm6dsx_odr_table_entry odr_table[2]; 237 struct st_lsm6dsx_fs_table_entry fs_table[2]; 238 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID]; 239 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID]; 240 struct st_lsm6dsx_fifo_ops fifo_ops; 241 struct st_lsm6dsx_hw_ts_settings ts_settings; 242 struct st_lsm6dsx_shub_settings shub_settings; 243 }; 244 245 enum st_lsm6dsx_sensor_id { 246 ST_LSM6DSX_ID_GYRO, 247 ST_LSM6DSX_ID_ACC, 248 ST_LSM6DSX_ID_EXT0, 249 ST_LSM6DSX_ID_EXT1, 250 ST_LSM6DSX_ID_EXT2, 251 ST_LSM6DSX_ID_MAX, 252 }; 253 254 enum st_lsm6dsx_fifo_mode { 255 ST_LSM6DSX_FIFO_BYPASS = 0x0, 256 ST_LSM6DSX_FIFO_CONT = 0x6, 257 }; 258 259 /** 260 * struct st_lsm6dsx_sensor - ST IMU sensor instance 261 * @name: Sensor name. 262 * @id: Sensor identifier. 263 * @hw: Pointer to instance of struct st_lsm6dsx_hw. 264 * @gain: Configured sensor sensitivity. 265 * @odr: Output data rate of the sensor [Hz]. 266 * @watermark: Sensor watermark level. 267 * @sip: Number of samples in a given pattern. 268 * @decimator: FIFO decimation factor. 269 * @ts_ref: Sensor timestamp reference for hw one. 270 * @ext_info: Sensor settings if it is connected to i2c controller 271 */ 272 struct st_lsm6dsx_sensor { 273 char name[32]; 274 enum st_lsm6dsx_sensor_id id; 275 struct st_lsm6dsx_hw *hw; 276 277 u32 gain; 278 u16 odr; 279 280 u16 watermark; 281 u8 sip; 282 u8 decimator; 283 s64 ts_ref; 284 285 struct { 286 const struct st_lsm6dsx_ext_dev_settings *settings; 287 u8 addr; 288 } ext_info; 289 }; 290 291 /** 292 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance 293 * @dev: Pointer to instance of struct device (I2C or SPI). 294 * @regmap: Register map of the device. 295 * @irq: Device interrupt line (I2C or SPI). 296 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO. 297 * @conf_lock: Mutex to prevent concurrent FIFO configuration update. 298 * @page_lock: Mutex to prevent concurrent memory page configuration. 299 * @fifo_mode: FIFO operating mode supported by the device. 300 * @suspend_mask: Suspended sensor bitmask. 301 * @enable_mask: Enabled sensor bitmask. 302 * @ts_sip: Total number of timestamp samples in a given pattern. 303 * @sip: Total number of samples (acc/gyro/ts) in a given pattern. 304 * @buff: Device read buffer. 305 * @iio_devs: Pointers to acc/gyro iio_dev instances. 306 * @settings: Pointer to the specific sensor settings in use. 307 */ 308 struct st_lsm6dsx_hw { 309 struct device *dev; 310 struct regmap *regmap; 311 int irq; 312 313 struct mutex fifo_lock; 314 struct mutex conf_lock; 315 struct mutex page_lock; 316 317 enum st_lsm6dsx_fifo_mode fifo_mode; 318 u8 suspend_mask; 319 u8 enable_mask; 320 u8 ts_sip; 321 u8 sip; 322 323 u8 *buff; 324 325 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX]; 326 327 const struct st_lsm6dsx_settings *settings; 328 }; 329 330 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0}; 331 extern const struct dev_pm_ops st_lsm6dsx_pm_ops; 332 333 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, 334 struct regmap *regmap); 335 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor, 336 bool enable); 337 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw); 338 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val); 339 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, 340 u16 watermark); 341 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable); 342 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw); 343 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw, 344 enum st_lsm6dsx_fifo_mode fifo_mode); 345 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw); 346 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw); 347 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val); 348 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name); 349 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable); 350 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable); 351 352 static inline int 353 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 354 unsigned int mask, unsigned int val) 355 { 356 int err; 357 358 mutex_lock(&hw->page_lock); 359 err = regmap_update_bits(hw->regmap, addr, mask, val); 360 mutex_unlock(&hw->page_lock); 361 362 return err; 363 } 364 365 static inline int 366 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 367 void *val, unsigned int len) 368 { 369 int err; 370 371 mutex_lock(&hw->page_lock); 372 err = regmap_bulk_read(hw->regmap, addr, val, len); 373 mutex_unlock(&hw->page_lock); 374 375 return err; 376 } 377 378 static inline int 379 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 380 unsigned int val) 381 { 382 int err; 383 384 mutex_lock(&hw->page_lock); 385 err = regmap_write(hw->regmap, addr, val); 386 mutex_unlock(&hw->page_lock); 387 388 return err; 389 } 390 391 #endif /* ST_LSM6DSX_H */ 392