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