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