1 /* 2 * Copyright (c) 2017 Intel Corporation 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DH_LIS2DH_H_ 8 #define ZEPHYR_DRIVERS_SENSOR_LIS2DH_LIS2DH_H_ 9 10 #include <zephyr/kernel.h> 11 #include <zephyr/device.h> 12 #include <zephyr/sys/util.h> 13 #include <stdint.h> 14 #include <zephyr/drivers/gpio.h> 15 #include <zephyr/drivers/sensor.h> 16 #include <string.h> 17 18 #define LIS2DH_REG_WAI 0x0f 19 #define LIS2DH_CHIP_ID 0x33 20 21 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 22 #include <zephyr/drivers/spi.h> 23 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */ 24 25 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 26 #include <zephyr/drivers/i2c.h> 27 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */ 28 29 #define LIS2DH_AUTOINCREMENT_ADDR BIT(7) 30 31 #define LIS2DH_REG_CTRL0 0x1e 32 #define LIS2DH_SDO_PU_DISC_MASK BIT(7) 33 34 #define LIS2DH_REG_CTRL1 0x20 35 #define LIS2DH_ACCEL_X_EN_BIT BIT(0) 36 #define LIS2DH_ACCEL_Y_EN_BIT BIT(1) 37 #define LIS2DH_ACCEL_Z_EN_BIT BIT(2) 38 #define LIS2DH_ACCEL_EN_BITS (LIS2DH_ACCEL_X_EN_BIT | \ 39 LIS2DH_ACCEL_Y_EN_BIT | \ 40 LIS2DH_ACCEL_Z_EN_BIT) 41 #define LIS2DH_ACCEL_XYZ_MASK BIT_MASK(3) 42 43 #define LIS2DH_LP_EN_BIT_MASK BIT(3) 44 #if defined(CONFIG_LIS2DH_OPER_MODE_LOW_POWER) 45 #define LIS2DH_LP_EN_BIT BIT(3) 46 #else 47 #define LIS2DH_LP_EN_BIT 0 48 #endif 49 50 #define LIS2DH_SUSPEND 0 51 52 #define LIS2DH_ODR_1 1 53 #define LIS2DH_ODR_2 2 54 #define LIS2DH_ODR_3 3 55 #define LIS2DH_ODR_4 4 56 #define LIS2DH_ODR_5 5 57 #define LIS2DH_ODR_6 6 58 #define LIS2DH_ODR_7 7 59 #define LIS2DH_ODR_8 8 60 #define LIS2DH_ODR_9 9 61 62 #if defined(CONFIG_LIS2DH_ODR_1) 63 #define LIS2DH_ODR_IDX LIS2DH_ODR_1 64 #elif defined(CONFIG_LIS2DH_ODR_2) 65 #define LIS2DH_ODR_IDX LIS2DH_ODR_2 66 #elif defined(CONFIG_LIS2DH_ODR_3) 67 #define LIS2DH_ODR_IDX LIS2DH_ODR_3 68 #elif defined(CONFIG_LIS2DH_ODR_4) || defined(CONFIG_LIS2DH_ODR_RUNTIME) 69 #define LIS2DH_ODR_IDX LIS2DH_ODR_4 70 #elif defined(CONFIG_LIS2DH_ODR_5) 71 #define LIS2DH_ODR_IDX LIS2DH_ODR_5 72 #elif defined(CONFIG_LIS2DH_ODR_6) 73 #define LIS2DH_ODR_IDX LIS2DH_ODR_6 74 #elif defined(CONFIG_LIS2DH_ODR_7) 75 #define LIS2DH_ODR_IDX LIS2DH_ODR_7 76 #elif defined(CONFIG_LIS2DH_ODR_8) 77 #define LIS2DH_ODR_IDX LIS2DH_ODR_8 78 #elif defined(CONFIG_LIS2DH_ODR_9_NORMAL) || defined(CONFIG_LIS2DH_ODR_9_LOW) 79 #define LIS2DH_ODR_IDX LIS2DH_ODR_9 80 #endif 81 82 #define LIS2DH_ODR_SHIFT 4 83 #define LIS2DH_ODR_RATE(r) ((r) << LIS2DH_ODR_SHIFT) 84 #define LIS2DH_ODR_BITS (LIS2DH_ODR_RATE(LIS2DH_ODR_IDX)) 85 #define LIS2DH_ODR_MASK (BIT_MASK(4) << LIS2DH_ODR_SHIFT) 86 87 #define LIS2DH_REG_CTRL2 0x21 88 #define LIS2DH_HPIS1_EN_BIT BIT(0) 89 #define LIS2DH_HPIS2_EN_BIT BIT(1) 90 #define LIS2DH_FDS_EN_BIT BIT(3) 91 92 #define LIS2DH_HPIS_EN_MASK BIT_MASK(2) 93 94 #define LIS2DH_REG_CTRL3 0x22 95 #define LIS2DH_EN_CLICK_INT1 BIT(7) 96 #define LIS2DH_EN_IA_INT1 BIT(6) 97 #define LIS2DH_EN_DRDY1_INT1 BIT(4) 98 99 #define LIS2DH_REG_CTRL4 0x23 100 #define LIS2DH_CTRL4_BDU_BIT BIT(7) 101 #define LIS2DH_FS_SHIFT 4 102 #define LIS2DH_FS_MASK (BIT_MASK(2) << LIS2DH_FS_SHIFT) 103 104 #if defined(CONFIG_LIS2DH_ACCEL_RANGE_2G) ||\ 105 defined(CONFIG_LIS2DH_ACCEL_RANGE_RUNTIME) 106 #define LIS2DH_FS_IDX 0 107 #elif defined(CONFIG_LIS2DH_ACCEL_RANGE_4G) 108 #define LIS2DH_FS_IDX 1 109 #elif defined(CONFIG_LIS2DH_ACCEL_RANGE_8G) 110 #define LIS2DH_FS_IDX 2 111 #elif defined(CONFIG_LIS2DH_ACCEL_RANGE_16G) 112 #define LIS2DH_FS_IDX 3 113 #endif 114 115 #define LIS2DH_FS_SELECT(fs) ((fs) << LIS2DH_FS_SHIFT) 116 #define LIS2DH_FS_BITS (LIS2DH_FS_SELECT(LIS2DH_FS_IDX)) 117 #if defined(CONFIG_LIS2DH_OPER_MODE_HIGH_RES) 118 #define LIS2DH_HR_BIT BIT(3) 119 #else 120 #define LIS2DH_HR_BIT 0 121 #endif 122 123 #define LIS2DH_REG_CTRL5 0x24 124 #define LIS2DH_EN_LIR_INT2 BIT(1) 125 #define LIS2DH_EN_LIR_INT1 BIT(3) 126 127 #define LIS2DH_REG_CTRL6 0x25 128 #define LIS2DH_EN_CLICK_INT2 BIT(7) 129 #define LIS2DH_EN_IA_INT2 BIT(5) 130 131 #define LIS2DH_REG_REFERENCE 0x26 132 133 #define LIS2DH_REG_STATUS 0x27 134 #define LIS2DH_STATUS_ZYZ_OVR BIT(7) 135 #define LIS2DH_STATUS_Z_OVR BIT(6) 136 #define LIS2DH_STATUS_Y_OVR BIT(5) 137 #define LIS2DH_STATUS_X_OVR BIT(4) 138 #define LIS2DH_STATUS_OVR_MASK (BIT_MASK(4) << 4) 139 #define LIS2DH_STATUS_ZYX_DRDY BIT(3) 140 #define LIS2DH_STATUS_Z_DRDY BIT(2) 141 #define LIS2DH_STATUS_Y_DRDY BIT(1) 142 #define LIS2DH_STATUS_X_DRDY BIT(0) 143 #define LIS2DH_STATUS_DRDY_MASK BIT_MASK(4) 144 145 #define LIS2DH_REG_ACCEL_X_LSB 0x28 146 #define LIS2DH_REG_ACCEL_Y_LSB 0x2A 147 #define LIS2DH_REG_ACCEL_Z_LSB 0x2C 148 #define LIS2DH_REG_ACCEL_X_MSB 0x29 149 #define LIS2DH_REG_ACCEL_Y_MSB 0x2B 150 #define LIS2DH_REG_ACCEL_Z_MSB 0x2D 151 152 #define LIS2DH_REG_INT1_CFG 0x30 153 #define LIS2DH_REG_INT1_SRC 0x31 154 #define LIS2DH_REG_INT1_THS 0x32 155 #define LIS2DH_REG_INT1_DUR 0x33 156 #define LIS2DH_REG_INT2_CFG 0x34 157 #define LIS2DH_REG_INT2_SRC 0x35 158 #define LIS2DH_REG_INT2_THS 0x36 159 #define LIS2DH_REG_INT2_DUR 0x37 160 161 #define LIS2DH_INT_CFG_MODE_SHIFT 6 162 #define LIS2DH_INT_CFG_AOI_CFG BIT(7) 163 #define LIS2DH_INT_CFG_6D_CFG BIT(6) 164 #define LIS2DH_INT_CFG_ZHIE_ZUPE BIT(5) 165 #define LIS2DH_INT_CFG_ZLIE_ZDOWNE BIT(4) 166 #define LIS2DH_INT_CFG_YHIE_YUPE BIT(3) 167 #define LIS2DH_INT_CFG_YLIE_YDOWNE BIT(2) 168 #define LIS2DH_INT_CFG_XHIE_XUPE BIT(1) 169 #define LIS2DH_INT_CFG_XLIE_XDOWNE BIT(0) 170 171 #define LIS2DH_REG_CFG_CLICK 0x38 172 #define LIS2DH_EN_CLICK_ZD BIT(5) 173 #define LIS2DH_EN_CLICK_ZS BIT(4) 174 #define LIS2DH_EN_CLICK_YD BIT(3) 175 #define LIS2DH_EN_CLICK_YS BIT(2) 176 #define LIS2DH_EN_CLICK_XD BIT(1) 177 #define LIS2DH_EN_CLICK_XS BIT(0) 178 179 #define LIS2DH_REG_CLICK_SRC 0x39 180 #define LIS2DH_CLICK_SRC_DCLICK BIT(5) 181 #define LIS2DH_CLICK_SRC_SCLICK BIT(4) 182 183 #define LIS2DH_REG_CFG_CLICK_THS 0x3A 184 #define LIS2DH_CLICK_LIR BIT(7) 185 186 #define LIS2DH_REG_TIME_LIMIT 0x3B 187 188 /* sample buffer size includes status register */ 189 #define LIS2DH_BUF_SZ 7 190 191 union lis2dh_sample { 192 uint8_t raw[LIS2DH_BUF_SZ]; 193 struct { 194 uint8_t status; 195 int16_t xyz[3]; 196 } __packed; 197 }; 198 199 union lis2dh_bus_cfg { 200 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 201 struct i2c_dt_spec i2c; 202 #endif 203 204 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 205 struct spi_dt_spec spi; 206 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */ 207 }; 208 209 struct temperature { 210 uint8_t cfg_addr; 211 uint8_t enable_mask; 212 uint8_t dout_addr; 213 uint8_t fractional_bits; 214 }; 215 216 struct lis2dh_config { 217 int (*bus_init)(const struct device *dev); 218 const union lis2dh_bus_cfg bus_cfg; 219 #ifdef CONFIG_LIS2DH_TRIGGER 220 const struct gpio_dt_spec gpio_drdy; 221 const struct gpio_dt_spec gpio_int; 222 #endif /* CONFIG_LIS2DH_TRIGGER */ 223 struct { 224 bool is_lsm303agr_dev : 1; 225 bool disc_pull_up : 1; 226 bool anym_on_int1 : 1; 227 bool anym_latch : 1; 228 uint8_t anym_mode : 2; 229 } hw; 230 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE 231 const struct temperature temperature; 232 #endif 233 }; 234 235 struct lis2dh_transfer_function { 236 int (*read_data)(const struct device *dev, uint8_t reg_addr, 237 uint8_t *value, uint8_t len); 238 int (*write_data)(const struct device *dev, uint8_t reg_addr, 239 uint8_t *value, uint8_t len); 240 int (*read_reg)(const struct device *dev, uint8_t reg_addr, 241 uint8_t *value); 242 int (*write_reg)(const struct device *dev, uint8_t reg_addr, 243 uint8_t value); 244 int (*update_reg)(const struct device *dev, uint8_t reg_addr, 245 uint8_t mask, uint8_t value); 246 }; 247 248 struct lis2dh_data { 249 const struct device *bus; 250 const struct lis2dh_transfer_function *hw_tf; 251 252 union lis2dh_sample sample; 253 /* current scaling factor, in micro m/s^2 / lsb */ 254 uint32_t scale; 255 256 #ifdef CONFIG_LIS2DH_MEASURE_TEMPERATURE 257 struct sensor_value temperature; 258 #endif 259 260 #ifdef CONFIG_PM_DEVICE 261 uint8_t reg_ctrl1_active_val; 262 #endif 263 264 #ifdef CONFIG_LIS2DH_TRIGGER 265 const struct device *dev; 266 struct gpio_callback gpio_int1_cb; 267 struct gpio_callback gpio_int2_cb; 268 269 sensor_trigger_handler_t handler_drdy; 270 const struct sensor_trigger *trig_drdy; 271 sensor_trigger_handler_t handler_anymotion; 272 const struct sensor_trigger *trig_anymotion; 273 sensor_trigger_handler_t handler_tap; 274 const struct sensor_trigger *trig_tap; 275 atomic_t trig_flags; 276 enum sensor_channel chan_drdy; 277 278 #if defined(CONFIG_LIS2DH_TRIGGER_OWN_THREAD) 279 K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DH_THREAD_STACK_SIZE); 280 struct k_thread thread; 281 struct k_sem gpio_sem; 282 #elif defined(CONFIG_LIS2DH_TRIGGER_GLOBAL_THREAD) 283 struct k_work work; 284 #endif 285 286 #endif /* CONFIG_LIS2DH_TRIGGER */ 287 }; 288 289 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 290 int lis2dh_spi_access(struct lis2dh_data *ctx, uint8_t cmd, 291 void *data, size_t length); 292 #endif 293 294 #ifdef CONFIG_LIS2DH_TRIGGER 295 int lis2dh_trigger_set(const struct device *dev, 296 const struct sensor_trigger *trig, 297 sensor_trigger_handler_t handler); 298 299 int lis2dh_init_interrupt(const struct device *dev); 300 301 int lis2dh_acc_slope_config(const struct device *dev, 302 enum sensor_attribute attr, 303 const struct sensor_value *val); 304 #endif 305 306 #ifdef CONFIG_LIS2DH_ACCEL_HP_FILTERS 307 int lis2dh_acc_hp_filter_set(const struct device *dev, 308 int32_t val); 309 #endif 310 311 int lis2dh_spi_init(const struct device *dev); 312 int lis2dh_i2c_init(const struct device *dev); 313 314 315 #endif /* __SENSOR_LIS2DH__ */ 316