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 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 enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID]; 211 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID]; 212 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID]; 213 struct st_lsm6dsx_fifo_ops fifo_ops; 214 struct st_lsm6dsx_hw_ts_settings ts_settings; 215 struct st_lsm6dsx_shub_settings shub_settings; 216 }; 217 218 enum st_lsm6dsx_sensor_id { 219 ST_LSM6DSX_ID_GYRO, 220 ST_LSM6DSX_ID_ACC, 221 ST_LSM6DSX_ID_EXT0, 222 ST_LSM6DSX_ID_EXT1, 223 ST_LSM6DSX_ID_EXT2, 224 ST_LSM6DSX_ID_MAX, 225 }; 226 227 enum st_lsm6dsx_fifo_mode { 228 ST_LSM6DSX_FIFO_BYPASS = 0x0, 229 ST_LSM6DSX_FIFO_CONT = 0x6, 230 }; 231 232 /** 233 * struct st_lsm6dsx_sensor - ST IMU sensor instance 234 * @name: Sensor name. 235 * @id: Sensor identifier. 236 * @hw: Pointer to instance of struct st_lsm6dsx_hw. 237 * @gain: Configured sensor sensitivity. 238 * @odr: Output data rate of the sensor [Hz]. 239 * @watermark: Sensor watermark level. 240 * @sip: Number of samples in a given pattern. 241 * @decimator: FIFO decimation factor. 242 * @ts_ref: Sensor timestamp reference for hw one. 243 * @ext_info: Sensor settings if it is connected to i2c controller 244 */ 245 struct st_lsm6dsx_sensor { 246 char name[32]; 247 enum st_lsm6dsx_sensor_id id; 248 struct st_lsm6dsx_hw *hw; 249 250 u32 gain; 251 u16 odr; 252 253 u16 watermark; 254 u8 sip; 255 u8 decimator; 256 s64 ts_ref; 257 258 struct { 259 const struct st_lsm6dsx_ext_dev_settings *settings; 260 u8 addr; 261 } ext_info; 262 }; 263 264 /** 265 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance 266 * @dev: Pointer to instance of struct device (I2C or SPI). 267 * @regmap: Register map of the device. 268 * @irq: Device interrupt line (I2C or SPI). 269 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO. 270 * @conf_lock: Mutex to prevent concurrent FIFO configuration update. 271 * @page_lock: Mutex to prevent concurrent memory page configuration. 272 * @fifo_mode: FIFO operating mode supported by the device. 273 * @enable_mask: Enabled sensor bitmask. 274 * @ts_sip: Total number of timestamp samples in a given pattern. 275 * @sip: Total number of samples (acc/gyro/ts) in a given pattern. 276 * @buff: Device read buffer. 277 * @iio_devs: Pointers to acc/gyro iio_dev instances. 278 * @settings: Pointer to the specific sensor settings in use. 279 */ 280 struct st_lsm6dsx_hw { 281 struct device *dev; 282 struct regmap *regmap; 283 int irq; 284 285 struct mutex fifo_lock; 286 struct mutex conf_lock; 287 struct mutex page_lock; 288 289 enum st_lsm6dsx_fifo_mode fifo_mode; 290 u8 enable_mask; 291 u8 ts_sip; 292 u8 sip; 293 294 u8 *buff; 295 296 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX]; 297 298 const struct st_lsm6dsx_settings *settings; 299 }; 300 301 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0}; 302 extern const struct dev_pm_ops st_lsm6dsx_pm_ops; 303 304 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name, 305 struct regmap *regmap); 306 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor, 307 bool enable); 308 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw); 309 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val); 310 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, 311 u16 watermark); 312 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw); 313 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw, 314 enum st_lsm6dsx_fifo_mode fifo_mode); 315 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw); 316 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw); 317 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val); 318 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name); 319 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable); 320 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable); 321 322 static inline int 323 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 324 unsigned int mask, unsigned int val) 325 { 326 int err; 327 328 mutex_lock(&hw->page_lock); 329 err = regmap_update_bits(hw->regmap, addr, mask, val); 330 mutex_unlock(&hw->page_lock); 331 332 return err; 333 } 334 335 static inline int 336 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 337 void *val, unsigned int len) 338 { 339 int err; 340 341 mutex_lock(&hw->page_lock); 342 err = regmap_bulk_read(hw->regmap, addr, val, len); 343 mutex_unlock(&hw->page_lock); 344 345 return err; 346 } 347 348 static inline int 349 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr, 350 unsigned int val) 351 { 352 int err; 353 354 mutex_lock(&hw->page_lock); 355 err = regmap_write(hw->regmap, addr, val); 356 mutex_unlock(&hw->page_lock); 357 358 return err; 359 } 360 361 #endif /* ST_LSM6DSX_H */ 362