1 /* ST Microelectronics IIS2DLPC 3-axis accelerometer driver 2 * 3 * Copyright (c) 2020 STMicroelectronics 4 * 5 * SPDX-License-Identifier: Apache-2.0 6 * 7 * Datasheet: 8 * https://www.st.com/resource/en/datasheet/iis2dlpc.pdf 9 */ 10 11 #ifndef ZEPHYR_DRIVERS_SENSOR_IIS2DLPC_IIS2DLPC_H_ 12 #define ZEPHYR_DRIVERS_SENSOR_IIS2DLPC_IIS2DLPC_H_ 13 14 #include <zephyr/drivers/gpio.h> 15 #include <zephyr/sys/util.h> 16 #include <zephyr/drivers/sensor.h> 17 #include <stmemsc.h> 18 #include "iis2dlpc_reg.h" 19 20 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 21 #include <zephyr/drivers/spi.h> 22 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */ 23 24 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 25 #include <zephyr/drivers/i2c.h> 26 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */ 27 28 /* Return ODR reg value based on data rate set */ 29 #define IIS2DLPC_ODR_TO_REG(_odr) \ 30 ((_odr <= 1) ? IIS2DLPC_XL_ODR_1Hz6_LP_ONLY : \ 31 (_odr <= 12) ? IIS2DLPC_XL_ODR_12Hz5 : \ 32 ((31 - __builtin_clz(_odr / 25))) + 3) 33 34 /* FS reg value from Full Scale */ 35 #define IIS2DLPC_FS_TO_REG(_fs) (30 - __builtin_clz(_fs)) 36 37 /* Acc Gain value in ug/LSB in High Perf mode */ 38 #define IIS2DLPC_FS_2G_GAIN 244 39 #define IIS2DLPC_FS_4G_GAIN 488 40 #define IIS2DLPC_FS_8G_GAIN 976 41 #define IIS2DLPC_FS_16G_GAIN 1952 42 43 #define IIS2DLPC_SHFT_GAIN_NOLP1 2 44 #define IIS2DLPC_ACCEL_GAIN_DEFAULT_VAL IIS2DLPC_FS_2G_GAIN 45 #define IIS2DLPC_FS_TO_GAIN(_fs, _lp1) \ 46 (IIS2DLPC_FS_2G_GAIN << ((_fs) + (_lp1))) 47 48 /* shift value for power mode */ 49 #define IIS2DLPC_SHIFT_PM1 4 50 #define IIS2DLPC_SHIFT_PMOTHER 2 51 52 /** 53 * struct iis2dlpc_dev_config - iis2dlpc hw configuration 54 * @bus_name: Pointer to bus master identifier. 55 * @pm: Power mode (lis2dh_powermode). 56 * @irq_dev_name: Pointer to GPIO PORT identifier. 57 * @irq_pin: GPIO pin number connected to sensor int pin. 58 * @drdy_int: Sensor drdy int (int1/int2). 59 */ 60 struct iis2dlpc_config { 61 stmdev_ctx_t ctx; 62 union { 63 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) 64 const struct i2c_dt_spec i2c; 65 #endif 66 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) 67 const struct spi_dt_spec spi; 68 #endif 69 } stmemsc_cfg; 70 iis2dlpc_mode_t pm; 71 uint8_t range; 72 #ifdef CONFIG_IIS2DLPC_TRIGGER 73 const struct gpio_dt_spec gpio_drdy; 74 uint8_t drdy_int; 75 #ifdef CONFIG_IIS2DLPC_TAP 76 uint8_t tap_mode; 77 uint8_t tap_threshold[3]; 78 uint8_t tap_shock; 79 uint8_t tap_latency; 80 uint8_t tap_quiet; 81 #endif /* CONFIG_IIS2DLPC_TAP */ 82 #endif /* CONFIG_IIS2DLPC_TRIGGER */ 83 }; 84 85 /* sensor data */ 86 struct iis2dlpc_data { 87 const struct device *dev; 88 int16_t acc[3]; 89 90 /* save sensitivity */ 91 uint16_t gain; 92 93 #ifdef CONFIG_IIS2DLPC_TRIGGER 94 const struct device *gpio; 95 uint8_t gpio_pin; 96 struct gpio_callback gpio_cb; 97 sensor_trigger_handler_t drdy_handler; 98 const struct sensor_trigger *drdy_trig; 99 #ifdef CONFIG_IIS2DLPC_TAP 100 sensor_trigger_handler_t tap_handler; 101 const struct sensor_trigger *tap_trig; 102 sensor_trigger_handler_t double_tap_handler; 103 const struct sensor_trigger *double_tap_trig; 104 #endif /* CONFIG_IIS2DLPC_TAP */ 105 #ifdef CONFIG_IIS2DLPC_ACTIVITY 106 sensor_trigger_handler_t activity_handler; 107 const struct sensor_trigger *activity_trig; 108 #endif /* CONFIG_IIS2DLPC_ACTIVITY */ 109 #if defined(CONFIG_IIS2DLPC_TRIGGER_OWN_THREAD) 110 K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_IIS2DLPC_THREAD_STACK_SIZE); 111 struct k_thread thread; 112 struct k_sem gpio_sem; 113 #elif defined(CONFIG_IIS2DLPC_TRIGGER_GLOBAL_THREAD) 114 struct k_work work; 115 #endif /* CONFIG_IIS2DLPC_TRIGGER_GLOBAL_THREAD */ 116 #endif /* CONFIG_IIS2DLPC_TRIGGER */ 117 }; 118 119 #ifdef CONFIG_IIS2DLPC_TRIGGER 120 int iis2dlpc_init_interrupt(const struct device *dev); 121 int iis2dlpc_trigger_set(const struct device *dev, 122 const struct sensor_trigger *trig, 123 sensor_trigger_handler_t handler); 124 #endif /* CONFIG_IIS2DLPC_TRIGGER */ 125 126 #endif /* ZEPHYR_DRIVERS_SENSOR_IIS2DLPC_IIS2DLPC_H_ */ 127