From ea6ad7337cf628aa9fce4daa48cef3ff02107d0f Mon Sep 17 00:00:00 2001 From: Armando Visconti Date: Mon, 6 Sep 2021 16:45:00 +0200 Subject: [PATCH] drivers/sensor: lis2ds12: Move odr Kconfig property into dts Move odr options from Kconfigs to Device Tree. Moreover add in DT a power-mode option to select among 4 possible values (PD, LP, HR, HF). The power mode cannot be currently set from sensor APIs. Signed-off-by: Armando Visconti --- drivers/sensor/lis2ds12/Kconfig | 19 ------ drivers/sensor/lis2ds12/lis2ds12.c | 76 ++++++++++++++++++--- drivers/sensor/lis2ds12/lis2ds12.h | 8 ++- dts/bindings/sensor/st,lis2ds12-common.yaml | 34 +++++++++ 4 files changed, 106 insertions(+), 31 deletions(-) diff --git a/drivers/sensor/lis2ds12/Kconfig b/drivers/sensor/lis2ds12/Kconfig index 9fc32c9b25e..ee8eb041f10 100644 --- a/drivers/sensor/lis2ds12/Kconfig +++ b/drivers/sensor/lis2ds12/Kconfig @@ -55,23 +55,4 @@ config LIS2DS12_ENABLE_TEMP help Enable/disable temperature -menu "Attributes" - -config LIS2DS12_ODR - int "Accelerometer Output data rate frequency" - range 0 10 - default 0 - help - Specify the default accelerometer output data rate expressed in - samples per second (Hz). - 0: ODR selected at runtime - 1: 12.5Hz - 2: 25Hz - 3: 50Hz - 4: 100Hz - 5: 200Hz - 6: 400Hz - 7: 800Hz -endmenu - endif # LIS2DS12 diff --git a/drivers/sensor/lis2ds12/lis2ds12.c b/drivers/sensor/lis2ds12/lis2ds12.c index bdfc1b38b92..182859c5b21 100644 --- a/drivers/sensor/lis2ds12/lis2ds12.c +++ b/drivers/sensor/lis2ds12/lis2ds12.c @@ -23,21 +23,73 @@ LOG_MODULE_REGISTER(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL); -static int lis2ds12_set_odr(const struct device *dev, uint16_t odr) +static int lis2ds12_set_odr(const struct device *dev, uint8_t odr) { const struct lis2ds12_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; - uint8_t val; + lis2ds12_odr_t val; /* check if power off */ if (odr == 0U) { + LOG_DBG("%s: set power-down", dev->name); return lis2ds12_xl_data_rate_set(ctx, LIS2DS12_XL_ODR_OFF); } - val = LIS2DS12_HR_ODR_TO_REG(odr); - if (val > LIS2DS12_XL_ODR_800Hz_HR) { - LOG_ERR("ODR too high"); - return -EINVAL; + /* + * odr >= 1600Hz are available in HF mode only + * 12,5Hz <= odr <= 800Hz are available in LP and HR mode only + * odr == 1Hz is available in LP mode only + */ + if ((odr >= 9 && cfg->pm != 3) || (odr < 9 && cfg->pm == 3) || + (odr == 1 && cfg->pm != 1)) { + LOG_ERR("%s: bad odr and pm combination", dev->name); + return -ENOTSUP; + } + + switch (odr) { + case 1: + val = LIS2DS12_XL_ODR_1Hz_LP; + break; + case 2: + val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_12Hz5_LP : + LIS2DS12_XL_ODR_12Hz5_HR; + break; + case 3: + val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_25Hz_LP : + LIS2DS12_XL_ODR_25Hz_HR; + break; + case 4: + val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_50Hz_LP : + LIS2DS12_XL_ODR_50Hz_HR; + break; + case 5: + val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_100Hz_LP : + LIS2DS12_XL_ODR_100Hz_HR; + break; + case 6: + val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_200Hz_LP : + LIS2DS12_XL_ODR_200Hz_HR; + break; + case 7: + val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_400Hz_LP : + LIS2DS12_XL_ODR_400Hz_HR; + break; + case 8: + val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_800Hz_LP : + LIS2DS12_XL_ODR_800Hz_HR; + break; + case 9: + val = LIS2DS12_XL_ODR_1k6Hz_HF; + break; + case 10: + val = LIS2DS12_XL_ODR_3k2Hz_HF; + break; + case 11: + val = LIS2DS12_XL_ODR_6k4Hz_HF; + break; + default: + LOG_ERR("%s: bad odr %d", dev->name, odr); + return -ENOTSUP; } return lis2ds12_xl_data_rate_set(ctx, val); @@ -82,7 +134,8 @@ static int lis2ds12_accel_config(const struct device *dev, case SENSOR_ATTR_FULL_SCALE: return lis2ds12_set_range(dev, sensor_ms2_to_g(val)); case SENSOR_ATTR_SAMPLING_FREQUENCY: - return lis2ds12_set_odr(dev, val->val1); + LOG_DBG("%s: set odr to %d Hz", dev->name, val->val1); + return lis2ds12_set_odr(dev, LIS2DS12_ODR_TO_REG(val->val1)); default: LOG_DBG("Accel attribute not supported."); return -ENOTSUP; @@ -250,8 +303,9 @@ static int lis2ds12_init(const struct device *dev) } #endif - /* set sensor default odr */ - ret = lis2ds12_set_odr(dev, 12); + /* set sensor default pm and odr */ + LOG_DBG("%s: pm: %d, odr: %d", dev->name, cfg->pm, cfg->odr); + ret = lis2ds12_set_odr(dev, (cfg->pm == 0) ? 0 : cfg->odr); if (ret < 0) { LOG_ERR("%s: odr init error (12.5 Hz)", dev->name); return ret; @@ -319,6 +373,8 @@ static int lis2ds12_init(const struct device *dev) 0), \ }, \ .range = DT_INST_PROP(inst, range), \ + .pm = DT_INST_PROP(inst, power_mode), \ + .odr = DT_INST_PROP(inst, odr), \ COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \ (LIS2DS12_CFG_IRQ(inst)), ()) \ } @@ -341,6 +397,8 @@ static int lis2ds12_init(const struct device *dev) .i2c = I2C_DT_SPEC_INST_GET(inst), \ }, \ .range = DT_INST_PROP(inst, range), \ + .pm = DT_INST_PROP(inst, power_mode), \ + .odr = DT_INST_PROP(inst, odr), \ COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \ (LIS2DS12_CFG_IRQ(inst)), ()) \ } diff --git a/drivers/sensor/lis2ds12/lis2ds12.h b/drivers/sensor/lis2ds12/lis2ds12.h index c2fc70e164f..fb96af37584 100644 --- a/drivers/sensor/lis2ds12/lis2ds12.h +++ b/drivers/sensor/lis2ds12/lis2ds12.h @@ -26,9 +26,9 @@ #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */ /* Return ODR reg value based on data rate set */ -#define LIS2DS12_HR_ODR_TO_REG(_odr) \ - ((_odr <= 12) ? LIS2DS12_XL_ODR_12Hz5_HR : \ - ((31 - __builtin_clz(_odr / 25))) + 2) +#define LIS2DS12_ODR_TO_REG(_odr) \ + ((_odr <= 1) ? 1 : \ + ((31 - __builtin_clz(_odr / 25))) + 3) struct lis2ds12_config { stmdev_ctx_t ctx; @@ -41,6 +41,8 @@ struct lis2ds12_config { #endif } stmemsc_cfg; uint8_t range; + uint8_t pm; + uint8_t odr; #ifdef CONFIG_LIS2DS12_TRIGGER struct gpio_dt_spec gpio_int; #endif diff --git a/dts/bindings/sensor/st,lis2ds12-common.yaml b/dts/bindings/sensor/st,lis2ds12-common.yaml index 83c21a2176e..4311c84e0fa 100644 --- a/dts/bindings/sensor/st,lis2ds12-common.yaml +++ b/dts/bindings/sensor/st,lis2ds12-common.yaml @@ -24,3 +24,37 @@ properties: - 8 # 8g (0.244 mg/LSB) - 4 # 4g (0.122 mg/LSB) - 2 # 2g (0.061 mg/LSB) + + power-mode: + type: int + required: false + default: 0 + description: | + Specify the sensor power mode. Default is power-down mode + + enum: + - 0 # Power Down (PD) + - 1 # Low Power (LP) + - 2 # High Resolution (HR) + - 3 # High Frequency (HF) + + odr: + type: int + required: false + default: 0 + description: | + Specify the default output data rate expressed in samples per second (Hz). + Default is power-down mode + enum: + - 0 # Power-Down + - 1 # 1Hz (available in LP mode only) + - 2 # 12.5Hz (available in LP and HR mode) + - 3 # 25Hz (available in LP and HR mode) + - 4 # 50Hz (available in LP and HR mode) + - 5 # 100Hz (available in LP and HR mode) + - 6 # 200Hz (available in LP and HR mode) + - 7 # 400Hz (available in LP and HR mode) + - 8 # 800Hz (available in LP and HR mode) + - 9 # 1600Hz (available in HF mode only) + - 10 # 3200Hz (available in HF mode only) + - 11 # 6400Hz (available in HF mode only)