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 22 enum st_lsm6dsx_hw_id { 23 ST_LSM6DS3_ID, 24 ST_LSM6DS3H_ID, 25 ST_LSM6DSL_ID, 26 ST_LSM6DSM_ID, 27 ST_LSM6DSX_MAX_ID, 28 }; 29 30 #define ST_LSM6DSX_BUFF_SIZE 400 31 #define ST_LSM6DSX_CHAN_SIZE 2 32 #define ST_LSM6DSX_SAMPLE_SIZE 6 33 #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \ 34 ST_LSM6DSX_SAMPLE_SIZE) 35 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask)) 36 37 struct st_lsm6dsx_reg { 38 u8 addr; 39 u8 mask; 40 }; 41 42 /** 43 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings 44 * @fifo_th: FIFO threshold register info (addr + mask). 45 * @fifo_diff: FIFO diff status register info (addr + mask). 46 * @th_wl: FIFO threshold word length. 47 */ 48 struct st_lsm6dsx_fifo_ops { 49 struct { 50 u8 addr; 51 u16 mask; 52 } fifo_th; 53 struct { 54 u8 addr; 55 u16 mask; 56 } fifo_diff; 57 u8 th_wl; 58 }; 59 60 /** 61 * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings 62 * @timer_en: Hw timer enable register info (addr + mask). 63 * @hr_timer: Hw timer resolution register info (addr + mask). 64 * @fifo_en: Hw timer FIFO enable register info (addr + mask). 65 * @decimator: Hw timer FIFO decimator register info (addr + mask). 66 */ 67 struct st_lsm6dsx_hw_ts_settings { 68 struct st_lsm6dsx_reg timer_en; 69 struct st_lsm6dsx_reg hr_timer; 70 struct st_lsm6dsx_reg fifo_en; 71 struct st_lsm6dsx_reg decimator; 72 }; 73 74 /** 75 * struct st_lsm6dsx_settings - ST IMU sensor settings 76 * @wai: Sensor WhoAmI default value. 77 * @max_fifo_size: Sensor max fifo length in FIFO words. 78 * @id: List of hw id supported by the driver configuration. 79 * @decimator: List of decimator register info (addr + mask). 80 * @fifo_ops: Sensor hw FIFO parameters. 81 * @ts_settings: Hw timer related settings. 82 */ 83 struct st_lsm6dsx_settings { 84 u8 wai; 85 u16 max_fifo_size; 86 enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID]; 87 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID]; 88 struct st_lsm6dsx_fifo_ops fifo_ops; 89 struct st_lsm6dsx_hw_ts_settings ts_settings; 90 }; 91 92 enum st_lsm6dsx_sensor_id { 93 ST_LSM6DSX_ID_ACC, 94 ST_LSM6DSX_ID_GYRO, 95 ST_LSM6DSX_ID_MAX, 96 }; 97 98 enum st_lsm6dsx_fifo_mode { 99 ST_LSM6DSX_FIFO_BYPASS = 0x0, 100 ST_LSM6DSX_FIFO_CONT = 0x6, 101 }; 102 103 /** 104 * struct st_lsm6dsx_sensor - ST IMU sensor instance 105 * @name: Sensor name. 106 * @id: Sensor identifier. 107 * @hw: Pointer to instance of struct st_lsm6dsx_hw. 108 * @gain: Configured sensor sensitivity. 109 * @odr: Output data rate of the sensor [Hz]. 110 * @watermark: Sensor watermark level. 111 * @sip: Number of samples in a given pattern. 112 * @decimator: FIFO decimation factor. 113 * @ts_ref: Sensor timestamp reference for hw one. 114 */ 115 struct st_lsm6dsx_sensor { 116 char name[32]; 117 enum st_lsm6dsx_sensor_id id; 118 struct st_lsm6dsx_hw *hw; 119 120 u32 gain; 121 u16 odr; 122 123 u16 watermark; 124 u8 sip; 125 u8 decimator; 126 s64 ts_ref; 127 }; 128 129 /** 130 * struct st_lsm6dsx_hw - ST IMU MEMS hw instance 131 * @dev: Pointer to instance of struct device (I2C or SPI). 132 * @regmap: Register map of the device. 133 * @irq: Device interrupt line (I2C or SPI). 134 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO. 135 * @conf_lock: Mutex to prevent concurrent FIFO configuration update. 136 * @fifo_mode: FIFO operating mode supported by the device. 137 * @enable_mask: Enabled sensor bitmask. 138 * @ts_sip: Total number of timestamp samples in a given pattern. 139 * @sip: Total number of samples (acc/gyro/ts) in a given pattern. 140 * @buff: Device read buffer. 141 * @iio_devs: Pointers to acc/gyro iio_dev instances. 142 * @settings: Pointer to the specific sensor settings in use. 143 */ 144 struct st_lsm6dsx_hw { 145 struct device *dev; 146 struct regmap *regmap; 147 int irq; 148 149 struct mutex fifo_lock; 150 struct mutex conf_lock; 151 152 enum st_lsm6dsx_fifo_mode fifo_mode; 153 u8 enable_mask; 154 u8 ts_sip; 155 u8 sip; 156 157 u8 *buff; 158 159 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX]; 160 161 const struct st_lsm6dsx_settings *settings; 162 }; 163 164 extern const struct dev_pm_ops st_lsm6dsx_pm_ops; 165 166 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name, 167 struct regmap *regmap); 168 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor); 169 int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor); 170 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw); 171 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, 172 u16 watermark); 173 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw); 174 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw, 175 enum st_lsm6dsx_fifo_mode fifo_mode); 176 177 #endif /* ST_LSM6DSX_H */ 178