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 #include <linux/iio/iio.h> 16 #include <linux/regulator/consumer.h> 17 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" 34 #define ST_ASM330LHHX_DEV_NAME "asm330lhhx" 35 #define ST_LSM6DSTX_DEV_NAME "lsm6dstx" 36 #define ST_LSM6DSV_DEV_NAME "lsm6dsv" 37 #define ST_LSM6DSV16X_DEV_NAME "lsm6dsv16x" 38 #define ST_LSM6DSO16IS_DEV_NAME "lsm6dso16is" 39 #define ST_ISM330IS_DEV_NAME "ism330is" 40 41 enum st_lsm6dsx_hw_id { 42 ST_LSM6DS3_ID, 43 ST_LSM6DS3H_ID, 44 ST_LSM6DSL_ID, 45 ST_LSM6DSM_ID, 46 ST_ISM330DLC_ID, 47 ST_LSM6DSO_ID, 48 ST_ASM330LHH_ID, 49 ST_LSM6DSOX_ID, 50 ST_LSM6DSR_ID, 51 ST_LSM6DS3TRC_ID, 52 ST_ISM330DHCX_ID, 53 ST_LSM9DS1_ID, 54 ST_LSM6DS0_ID, 55 ST_LSM6DSRX_ID, 56 ST_LSM6DST_ID, 57 ST_LSM6DSOP_ID, 58 ST_ASM330LHHX_ID, 59 ST_LSM6DSTX_ID, 60 ST_LSM6DSV_ID, 61 ST_LSM6DSV16X_ID, 62 ST_LSM6DSO16IS_ID, 63 ST_ISM330IS_ID, 64 ST_LSM6DSX_MAX_ID, 65 }; 66 67 #define ST_LSM6DSX_BUFF_SIZE 512 68 #define ST_LSM6DSX_CHAN_SIZE 2 69 #define ST_LSM6DSX_SAMPLE_SIZE 6 70 #define ST_LSM6DSX_TAG_SIZE 1 71 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE (ST_LSM6DSX_SAMPLE_SIZE + \ 72 ST_LSM6DSX_TAG_SIZE) 73 #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \ 74 ST_LSM6DSX_SAMPLE_SIZE) 75 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \ 76 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE) 77 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask)) 78 79 #define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx) \ 80 { \ 81 .type = chan_type, \ 82 .address = addr, \ 83 .modified = 1, \ 84 .channel2 = mod, \ 85 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 86 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 87 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 88 .scan_index = scan_idx, \ 89 .scan_type = { \ 90 .sign = 's', \ 91 .realbits = 16, \ 92 .storagebits = 16, \ 93 .endianness = IIO_LE, \ 94 }, \ 95 .event_spec = &st_lsm6dsx_event, \ 96 .ext_info = st_lsm6dsx_ext_info, \ 97 .num_event_specs = 1, \ 98 } 99 100 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \ 101 { \ 102 .type = chan_type, \ 103 .address = addr, \ 104 .modified = 1, \ 105 .channel2 = mod, \ 106 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 107 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 108 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 109 .scan_index = scan_idx, \ 110 .scan_type = { \ 111 .sign = 's', \ 112 .realbits = 16, \ 113 .storagebits = 16, \ 114 .endianness = IIO_LE, \ 115 }, \ 116 .ext_info = st_lsm6dsx_ext_info, \ 117 } 118 119 struct st_lsm6dsx_reg { 120 u8 addr; 121 u8 mask; 122 }; 123 124 struct st_lsm6dsx_sensor; 125 struct st_lsm6dsx_hw; 126 127 struct st_lsm6dsx_odr { 128 u32 milli_hz; 129 u8 val; 130 }; 131 132 #define ST_LSM6DSX_ODR_LIST_SIZE 8 133 struct st_lsm6dsx_odr_table_entry { 134 struct st_lsm6dsx_reg reg; 135 136 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE]; 137 int odr_len; 138 }; 139 140 struct st_lsm6dsx_fs { 141 u32 gain; 142 u8 val; 143 }; 144 145 #define ST_LSM6DSX_FS_LIST_SIZE 4 146 struct st_lsm6dsx_fs_table_entry { 147 struct st_lsm6dsx_reg reg; 148 149 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE]; 150 int fs_len; 151 }; 152 153 /** 154 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings 155 * @update_fifo: Update FIFO configuration callback. 156 * @read_fifo: Read FIFO callback. 157 * @fifo_th: FIFO threshold register info (addr + mask). 158 * @fifo_diff: FIFO diff status register info (addr + mask). 159 * @max_size: Sensor max fifo length in FIFO words. 160 * @th_wl: FIFO threshold word length. 161 */ 162 struct st_lsm6dsx_fifo_ops { 163 int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable); 164 int (*read_fifo)(struct st_lsm6dsx_hw *hw); 165 struct { 166 u8 addr; 167 u16 mask; 168 } fifo_th; 169 struct { 170 u8 addr; 171 u16 mask; 172 } fifo_diff; 173 u16 max_size; 174 u8 th_wl; 175 }; 176 177 /** 178 * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings 179 * @timer_en: Hw timer enable register info (addr + mask). 180 * @hr_timer: Hw timer resolution register info (addr + mask). 181 * @fifo_en: Hw timer FIFO enable register info (addr + mask). 182 * @decimator: Hw timer FIFO decimator register info (addr + mask). 183 * @freq_fine: Difference in % of ODR with respect to the typical. 184 */ 185 struct st_lsm6dsx_hw_ts_settings { 186 struct st_lsm6dsx_reg timer_en; 187 struct st_lsm6dsx_reg hr_timer; 188 struct st_lsm6dsx_reg fifo_en; 189 struct st_lsm6dsx_reg decimator; 190 u8 freq_fine; 191 }; 192 193 /** 194 * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings 195 * @page_mux: register page mux info (addr + mask). 196 * @master_en: master config register info (addr + mask). 197 * @pullup_en: i2c controller pull-up register info (addr + mask). 198 * @aux_sens: aux sensor register info (addr + mask). 199 * @wr_once: write_once register info (addr + mask). 200 * @emb_func: embedded function register info (addr + mask). 201 * @num_ext_dev: max number of slave devices. 202 * @shub_out: sensor hub first output register info. 203 * @slv0_addr: slave0 address in secondary page. 204 * @dw_slv0_addr: slave0 write register address in secondary page. 205 * @batch_en: Enable/disable FIFO batching. 206 * @pause: controller pause value. 207 */ 208 struct st_lsm6dsx_shub_settings { 209 struct st_lsm6dsx_reg page_mux; 210 struct { 211 bool sec_page; 212 u8 addr; 213 u8 mask; 214 } master_en; 215 struct { 216 bool sec_page; 217 u8 addr; 218 u8 mask; 219 } pullup_en; 220 struct st_lsm6dsx_reg aux_sens; 221 struct st_lsm6dsx_reg wr_once; 222 struct st_lsm6dsx_reg emb_func; 223 u8 num_ext_dev; 224 struct { 225 bool sec_page; 226 u8 addr; 227 } shub_out; 228 u8 slv0_addr; 229 u8 dw_slv0_addr; 230 u8 batch_en; 231 u8 pause; 232 }; 233 234 struct st_lsm6dsx_event_settings { 235 struct st_lsm6dsx_reg enable_reg; 236 struct st_lsm6dsx_reg wakeup_reg; 237 u8 wakeup_src_reg; 238 u8 wakeup_src_status_mask; 239 u8 wakeup_src_z_mask; 240 u8 wakeup_src_y_mask; 241 u8 wakeup_src_x_mask; 242 }; 243 244 enum st_lsm6dsx_ext_sensor_id { 245 ST_LSM6DSX_ID_MAGN, 246 }; 247 248 /** 249 * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings 250 * @i2c_addr: I2c slave address list. 251 * @wai: Wai address info. 252 * @id: external sensor id. 253 * @odr_table: Output data rate of the sensor [Hz]. 254 * @fs_table: Configured sensor sensitivity table depending on full scale. 255 * @temp_comp: Temperature compensation register info (addr + mask). 256 * @pwr_table: Power on register info (addr + mask). 257 * @off_canc: Offset cancellation register info (addr + mask). 258 * @bdu: Block data update register info (addr + mask). 259 * @out: Output register info. 260 */ 261 struct st_lsm6dsx_ext_dev_settings { 262 u8 i2c_addr[2]; 263 struct { 264 u8 addr; 265 u8 val; 266 } wai; 267 enum st_lsm6dsx_ext_sensor_id id; 268 struct st_lsm6dsx_odr_table_entry odr_table; 269 struct st_lsm6dsx_fs_table_entry fs_table; 270 struct st_lsm6dsx_reg temp_comp; 271 struct { 272 struct st_lsm6dsx_reg reg; 273 u8 off_val; 274 u8 on_val; 275 } pwr_table; 276 struct st_lsm6dsx_reg off_canc; 277 struct st_lsm6dsx_reg bdu; 278 struct { 279 u8 addr; 280 u8 len; 281 } out; 282 }; 283 284 /** 285 * struct st_lsm6dsx_settings - ST IMU sensor settings 286 * @reset: register address for reset. 287 * @boot: register address for boot. 288 * @bdu: register address for Block Data Update. 289 * @id: List of hw id/device name supported by the driver configuration. 290 * @channels: IIO channels supported by the device. 291 * @irq_config: interrupts related registers. 292 * @drdy_mask: register info for data-ready mask (addr + mask). 293 * @odr_table: Hw sensors odr table (Hz + val). 294 * @fs_table: Hw sensors gain table (gain + val). 295 * @decimator: List of decimator register info (addr + mask). 296 * @batch: List of FIFO batching register info (addr + mask). 297 * @fifo_ops: Sensor hw FIFO parameters. 298 * @ts_settings: Hw timer related settings. 299 * @shub_settings: i2c controller related settings. 300 */ 301 struct st_lsm6dsx_settings { 302 struct st_lsm6dsx_reg reset; 303 struct st_lsm6dsx_reg boot; 304 struct st_lsm6dsx_reg bdu; 305 struct { 306 enum st_lsm6dsx_hw_id hw_id; 307 const char *name; 308 u8 wai; 309 } id[ST_LSM6DSX_MAX_ID]; 310 struct { 311 const struct iio_chan_spec *chan; 312 int len; 313 } channels[2]; 314 struct { 315 struct st_lsm6dsx_reg irq1; 316 struct st_lsm6dsx_reg irq2; 317 struct st_lsm6dsx_reg irq1_func; 318 struct st_lsm6dsx_reg irq2_func; 319 struct st_lsm6dsx_reg lir; 320 struct st_lsm6dsx_reg clear_on_read; 321 struct st_lsm6dsx_reg hla; 322 struct st_lsm6dsx_reg od; 323 } irq_config; 324 struct st_lsm6dsx_reg drdy_mask; 325 struct st_lsm6dsx_odr_table_entry odr_table[2]; 326 struct st_lsm6dsx_fs_table_entry fs_table[2]; 327 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID]; 328 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID]; 329 struct st_lsm6dsx_fifo_ops fifo_ops; 330 struct st_lsm6dsx_hw_ts_settings ts_settings; 331 struct st_lsm6dsx_shub_settings shub_settings; 332 struct st_lsm6dsx_event_settings event_settings; 333 }; 334 335 enum st_lsm6dsx_sensor_id { 336 ST_LSM6DSX_ID_GYRO, 337 ST_LSM6DSX_ID_ACC, 338 ST_LSM6DSX_ID_EXT0, 339 ST_LSM6DSX_ID_EXT1, 340 ST_LSM6DSX_ID_EXT2, 341 ST_LSM6DSX_ID_MAX, 342 }; 343 344 enum st_lsm6dsx_fifo_mode { 345 ST_LSM6DSX_FIFO_BYPASS = 0x0, 346 ST_LSM6DSX_FIFO_CONT = 0x6, 347 }; 348 349 /** 350 * struct st_lsm6dsx_sensor - ST IMU sensor instance 351 * @name: Sensor name. 352 * @id: Sensor identifier. 353 * @hw: Pointer to instance of struct st_lsm6dsx_hw. 354 * @gain: Configured sensor sensitivity. 355 * @odr: Output data rate of the sensor [Hz]. 356 * @watermark: Sensor watermark level. 357 * @decimator: Sensor decimation factor. 358 * @sip: Number of samples in a given pattern. 359 * @ts_ref: Sensor timestamp reference for hw one. 360 * @ext_info: Sensor settings if it is connected to i2c controller 361 */ 362 struct st_lsm6dsx_sensor { 363 char name[32]; 364 enum st_lsm6dsx_sensor_id id; 365 struct st_lsm6dsx_hw *hw; 366 367 u32 gain; 368 u32 odr; 369 370 u16 watermark; 371 u8 decimator; 372 u8 sip; 373 s64 ts_ref; 374 375 struct { 376 const struct st_lsm6dsx_ext_dev_settings *settings; 377 u32 slv_odr; 378 u8 addr; 379 } ext_info; 380 }; 381 382 /** 383 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance 384 * @dev: Pointer to instance of struct device (I2C or SPI). 385 * @regmap: Register map of the device. 386 * @irq: Device interrupt line (I2C or SPI). 387 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO. 388 * @conf_lock: Mutex to prevent concurrent FIFO configuration update. 389 * @page_lock: Mutex to prevent concurrent memory page configuration. 390 * @suspend_mask: Suspended sensor bitmask. 391 * @enable_mask: Enabled sensor bitmask. 392 * @fifo_mask: Enabled hw FIFO bitmask. 393 * @ts_gain: Hw timestamp rate after internal calibration. 394 * @ts_sip: Total number of timestamp samples in a given pattern. 395 * @sip: Total number of samples (acc/gyro/ts) in a given pattern. 396 * @buff: Device read buffer. 397 * @irq_routing: pointer to interrupt routing configuration. 398 * @event_threshold: wakeup event threshold. 399 * @enable_event: enabled event bitmask. 400 * @iio_devs: Pointers to acc/gyro iio_dev instances. 401 * @settings: Pointer to the specific sensor settings in use. 402 * @orientation: sensor chip orientation relative to main hardware. 403 * @scan: Temporary buffers used to align data before iio_push_to_buffers() 404 */ 405 struct st_lsm6dsx_hw { 406 struct device *dev; 407 struct regmap *regmap; 408 int irq; 409 410 struct mutex fifo_lock; 411 struct mutex conf_lock; 412 struct mutex page_lock; 413 414 u8 suspend_mask; 415 u8 enable_mask; 416 u8 fifo_mask; 417 s64 ts_gain; 418 u8 ts_sip; 419 u8 sip; 420 421 const struct st_lsm6dsx_reg *irq_routing; 422 u8 event_threshold; 423 u8 enable_event; 424 425 u8 *buff; 426 427 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX]; 428 429 const struct st_lsm6dsx_settings *settings; 430 431 struct iio_mount_matrix orientation; 432 /* Ensure natural alignment of buffer elements */ 433 struct { 434 __le16 channels[3]; 435 s64 ts __aligned(8); 436 } scan[ST_LSM6DSX_ID_MAX]; 437 }; 438 439 static __maybe_unused const struct iio_event_spec st_lsm6dsx_event = { 440 .type = IIO_EV_TYPE_THRESH, 441 .dir = IIO_EV_DIR_EITHER, 442 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 443 BIT(IIO_EV_INFO_ENABLE) 444 }; 445 446 static __maybe_unused const unsigned long st_lsm6dsx_available_scan_masks[] = { 447 0x7, 0x0, 448 }; 449 450 extern const struct dev_pm_ops st_lsm6dsx_pm_ops; 451 452 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, 453 struct regmap *regmap); 454 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor, 455 bool enable); 456 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw); 457 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val); 458 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, 459 u16 watermark); 460 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable); 461 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw); 462 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw); 463 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw); 464 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw); 465 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val); 466 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name); 467 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable); 468 int st_lsm6dsx_shub_read_output(struct st_lsm6dsx_hw *hw, u8 *data, int len); 469 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable); 470 471 static inline int 472 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 473 unsigned int mask, unsigned int val) 474 { 475 int err; 476 477 mutex_lock(&hw->page_lock); 478 err = regmap_update_bits(hw->regmap, addr, mask, val); 479 mutex_unlock(&hw->page_lock); 480 481 return err; 482 } 483 484 static inline int 485 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 486 void *val, unsigned int len) 487 { 488 int err; 489 490 mutex_lock(&hw->page_lock); 491 err = regmap_bulk_read(hw->regmap, addr, val, len); 492 mutex_unlock(&hw->page_lock); 493 494 return err; 495 } 496 497 static inline int 498 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 499 unsigned int val) 500 { 501 int err; 502 503 mutex_lock(&hw->page_lock); 504 err = regmap_write(hw->regmap, addr, val); 505 mutex_unlock(&hw->page_lock); 506 507 return err; 508 } 509 510 static inline const struct iio_mount_matrix * 511 st_lsm6dsx_get_mount_matrix(const struct iio_dev *iio_dev, 512 const struct iio_chan_spec *chan) 513 { 514 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); 515 struct st_lsm6dsx_hw *hw = sensor->hw; 516 517 return &hw->orientation; 518 } 519 520 static inline int 521 st_lsm6dsx_device_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable) 522 { 523 if (sensor->id == ST_LSM6DSX_ID_EXT0 || 524 sensor->id == ST_LSM6DSX_ID_EXT1 || 525 sensor->id == ST_LSM6DSX_ID_EXT2) 526 return st_lsm6dsx_shub_set_enable(sensor, enable); 527 528 return st_lsm6dsx_sensor_set_enable(sensor, enable); 529 } 530 531 static const 532 struct iio_chan_spec_ext_info __maybe_unused st_lsm6dsx_ext_info[] = { 533 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_lsm6dsx_get_mount_matrix), 534 { } 535 }; 536 537 #endif /* ST_LSM6DSX_H */ 538