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