1 /* ST Microelectronics LIS2DW12 3-axis accelerometer driver 2 * 3 * Copyright (c) 2019 STMicroelectronics 4 * 5 * SPDX-License-Identifier: Apache-2.0 6 * 7 * Datasheet: 8 * https://www.st.com/resource/en/datasheet/lis2dw12.pdf 9 */ 10 11 #ifndef ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ 12 #define ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ 13 14 #include <zephyr/drivers/spi.h> 15 #include <zephyr/drivers/gpio.h> 16 #include <zephyr/sys/util.h> 17 #include <zephyr/drivers/sensor.h> 18 #include <stmemsc.h> 19 #include "lis2dw12_reg.h" 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 /* Return ODR reg value based on data rate set */ 30 #define LIS2DW12_ODR_TO_REG(_odr) \ 31 ((_odr <= 1) ? LIS2DW12_XL_ODR_1Hz6_LP_ONLY : \ 32 (_odr <= 12) ? LIS2DW12_XL_ODR_12Hz5 : \ 33 ((31 - __builtin_clz(_odr / 25))) + 3) 34 35 /* Return data rate in Hz for given register value */ 36 #define LIS2DW12_REG_TO_ODR(_reg) \ 37 ((_reg == 0) ? 0 : \ 38 (_reg == 1) ? 1 : \ 39 (_reg == 2) ? 12 : \ 40 (_reg > 9) ? 1600 : \ 41 (1 << (_reg - 3)) * 25) 42 43 /* FS reg value from Full Scale */ 44 #define LIS2DW12_FS_TO_REG(_fs) (30 - __builtin_clz(_fs)) 45 46 /* Acc Gain value in ug/LSB in High Perf mode */ 47 #define LIS2DW12_FS_2G_GAIN 244 48 #define LIS2DW12_FS_4G_GAIN 488 49 #define LIS2DW12_FS_8G_GAIN 976 50 #define LIS2DW12_FS_16G_GAIN 1952 51 52 #define LIS2DW12_SHFT_GAIN_NOLP1 2 53 #define LIS2DW12_ACCEL_GAIN_DEFAULT_VAL LIS2DW12_FS_2G_GAIN 54 #define LIS2DW12_FS_TO_GAIN(_fs, _lp1) \ 55 (LIS2DW12_FS_2G_GAIN << ((_fs) + (_lp1))) 56 57 /* shift value for power mode */ 58 #define LIS2DW12_SHIFT_PM1 4 59 #define LIS2DW12_SHIFT_PMOTHER 2 60 61 /* shift value for 12 bit resolution */ 62 #define LIS2DW12_SHIFT_TEMP 4 63 /* Temperature 12 bit scale factor in uC = 1000000/16 as 1 LSB = C/16 */ 64 #define LIS2DW12_TEMP_SCALE_FACTOR 62500 65 66 /** 67 * struct lis2dw12_device_config - lis2dw12 hw configuration 68 * @bus_name: Pointer to bus master identifier. 69 * @pm: Power mode (lis2dh_powermode). 70 * @int_pin: Sensor int pin (int1/int2). 71 */ 72 struct lis2dw12_device_config { 73 stmdev_ctx_t ctx; 74 union { 75 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 76 const struct i2c_dt_spec i2c; 77 #endif 78 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 79 const struct spi_dt_spec spi; 80 #endif 81 } stmemsc_cfg; 82 lis2dw12_mode_t pm; 83 uint16_t odr; 84 uint8_t range; 85 uint8_t bw_filt; 86 bool low_noise; 87 bool hp_filter_path; 88 bool hp_ref_mode; 89 bool drdy_pulsed; 90 #ifdef CONFIG_LIS2DW12_TRIGGER 91 struct gpio_dt_spec gpio_int; 92 uint8_t int_pin; 93 #ifdef CONFIG_LIS2DW12_TAP 94 uint8_t tap_mode; 95 uint8_t tap_threshold[3]; 96 uint8_t tap_shock; 97 uint8_t tap_latency; 98 uint8_t tap_quiet; 99 #endif /* CONFIG_LIS2DW12_TAP */ 100 #ifdef CONFIG_LIS2DW12_SLEEP 101 uint8_t sleep_duration; 102 #endif 103 #ifdef CONFIG_LIS2DW12_FREEFALL 104 uint8_t freefall_duration; 105 uint8_t freefall_threshold; 106 #endif /* CONFIG_LIS2DW12_FREEFALL */ 107 #ifdef CONFIG_LIS2DW12_WAKEUP 108 uint8_t wakeup_duration; 109 #endif /* CONFIG_LIS2DW12_WAKEUP */ 110 #endif /* CONFIG_LIS2DW12_TRIGGER */ 111 }; 112 113 /* sensor data */ 114 struct lis2dw12_data { 115 /* temperature raw data */ 116 int16_t temp; 117 /* accelerometer raw data */ 118 int16_t acc[3]; 119 120 /* save sensitivity */ 121 uint16_t gain; 122 /* output data rate */ 123 uint16_t odr; 124 125 #ifdef CONFIG_LIS2DW12_TRIGGER 126 const struct device *dev; 127 128 struct gpio_callback gpio_cb; 129 sensor_trigger_handler_t drdy_handler; 130 const struct sensor_trigger *drdy_trig; 131 #ifdef CONFIG_LIS2DW12_TAP 132 sensor_trigger_handler_t tap_handler; 133 const struct sensor_trigger *tap_trig; 134 sensor_trigger_handler_t double_tap_handler; 135 const struct sensor_trigger *double_tap_trig; 136 #endif /* CONFIG_LIS2DW12_TAP */ 137 #ifdef CONFIG_LIS2DW12_WAKEUP 138 sensor_trigger_handler_t motion_handler; 139 const struct sensor_trigger *motion_trig; 140 #endif /* CONFIG_LIS2DW12_WAKEUP */ 141 #ifdef CONFIG_LIS2DW12_SLEEP 142 sensor_trigger_handler_t stationary_handler; 143 const struct sensor_trigger *stationary_trig; 144 #endif 145 #ifdef CONFIG_LIS2DW12_FREEFALL 146 sensor_trigger_handler_t freefall_handler; 147 const struct sensor_trigger *freefall_trig; 148 #endif /* CONFIG_LIS2DW12_FREEFALL */ 149 #if defined(CONFIG_LIS2DW12_TRIGGER_OWN_THREAD) 150 K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DW12_THREAD_STACK_SIZE); 151 struct k_thread thread; 152 struct k_sem gpio_sem; 153 #elif defined(CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD) 154 struct k_work work; 155 #endif /* CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD */ 156 #endif /* CONFIG_LIS2DW12_TRIGGER */ 157 }; 158 159 #ifdef CONFIG_LIS2DW12_TRIGGER 160 int lis2dw12_init_interrupt(const struct device *dev); 161 int lis2dw12_trigger_set(const struct device *dev, 162 const struct sensor_trigger *trig, 163 sensor_trigger_handler_t handler); 164 #endif /* CONFIG_LIS2DW12_TRIGGER */ 165 166 /* LIS2DW12 specific channels */ 167 enum sensor_channel_lis2dw12 { 168 SENSOR_CHAN_LIS2DW12_INT_STATUS = SENSOR_CHAN_PRIV_START, 169 }; 170 171 #endif /* ZEPHYR_DRIVERS_SENSOR_LIS2DW12_LIS2DW12_H_ */ 172