diff --git a/drivers/sensor/iis2iclx/Kconfig b/drivers/sensor/iis2iclx/Kconfig index 2b735f4aac0d5..6a126412f7b02 100644 --- a/drivers/sensor/iis2iclx/Kconfig +++ b/drivers/sensor/iis2iclx/Kconfig @@ -86,39 +86,4 @@ config IIS2ICLX_EXT_LPS22HB endif # IIS2ICLX_SENSORHUB -menu "Attributes" - -config IIS2ICLX_ACCEL_FS - int "Accelerometer full-scale range" - default 0 - help - Specify the default accelerometer full-scale range. - An X value for the config represents a range of +/- X G. Valid values - are: - 0: Full Scale selected at runtime - 500: +/- 500mg - 1000: +/- 1g - 2000: +/- 2g - 3000: +/- 3g - -config IIS2ICLX_ACCEL_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: 26Hz - 3: 52Hz - 4: 104Hz - 5: 208Hz - 6: 416Hz - 7: 833Hz - 8: 1660Hz - 9: 3330Hz - 10: 6660Hz -endmenu - endif # IIS2ICLX diff --git a/drivers/sensor/iis2iclx/iis2iclx.c b/drivers/sensor/iis2iclx/iis2iclx.c index 66cb5c8ed8ca3..b68f51117464c 100644 --- a/drivers/sensor/iis2iclx/iis2iclx.c +++ b/drivers/sensor/iis2iclx/iis2iclx.c @@ -26,7 +26,6 @@ LOG_MODULE_REGISTER(IIS2ICLX, CONFIG_SENSOR_LOG_LEVEL); static const uint16_t iis2iclx_odr_map[] = {0, 12, 26, 52, 104, 208, 416, 833, 1660, 3330, 6660}; -#if defined(IIS2ICLX_ACCEL_ODR_RUNTIME) static int iis2iclx_freq_to_odr_val(uint16_t freq) { size_t i; @@ -39,7 +38,6 @@ static int iis2iclx_freq_to_odr_val(uint16_t freq) return -EINVAL; } -#endif static int iis2iclx_odr_to_freq_val(uint16_t odr) { @@ -52,7 +50,6 @@ static int iis2iclx_odr_to_freq_val(uint16_t odr) return iis2iclx_odr_map[ARRAY_SIZE(iis2iclx_odr_map) - 1]; } -#ifdef IIS2ICLX_ACCEL_FS_RUNTIME static const uint16_t iis2iclx_accel_fs_map[] = {500, 3000, 1000, 2000}; static const uint16_t iis2iclx_accel_fs_sens[] = {1, 8, 2, 4}; @@ -68,7 +65,6 @@ static int iis2iclx_accel_range_to_fs_val(int32_t range) return -EINVAL; } -#endif static inline int iis2iclx_reboot(const struct device *dev) { @@ -110,7 +106,6 @@ static int iis2iclx_accel_set_odr_raw(const struct device *dev, uint8_t odr) return 0; } -#ifdef IIS2ICLX_ACCEL_ODR_RUNTIME static int iis2iclx_accel_odr_set(const struct device *dev, uint16_t freq) { int odr; @@ -127,9 +122,7 @@ static int iis2iclx_accel_odr_set(const struct device *dev, uint16_t freq) return 0; } -#endif -#ifdef IIS2ICLX_ACCEL_FS_RUNTIME static int iis2iclx_accel_range_set(const struct device *dev, int32_t range) { int fs; @@ -148,7 +141,6 @@ static int iis2iclx_accel_range_set(const struct device *dev, int32_t range) data->acc_gain = (iis2iclx_accel_fs_sens[fs] * GAIN_UNIT_XL); return 0; } -#endif static int iis2iclx_accel_config(const struct device *dev, enum sensor_channel chan, @@ -156,14 +148,10 @@ static int iis2iclx_accel_config(const struct device *dev, const struct sensor_value *val) { switch (attr) { -#ifdef IIS2ICLX_ACCEL_FS_RUNTIME case SENSOR_ATTR_FULL_SCALE: return iis2iclx_accel_range_set(dev, sensor_ms2_to_g(val)); -#endif -#ifdef IIS2ICLX_ACCEL_ODR_RUNTIME case SENSOR_ATTR_SAMPLING_FREQUENCY: return iis2iclx_accel_odr_set(dev, val->val1); -#endif default: LOG_ERR("Accel attribute not supported."); return -ENOTSUP; @@ -544,8 +532,11 @@ static const struct sensor_driver_api iis2iclx_driver_api = { static int iis2iclx_init_chip(const struct device *dev) { + const struct iis2iclx_config * const cfg = dev->config; struct iis2iclx_data *iis2iclx = dev->data; uint8_t chip_id; + uint8_t odr = cfg->odr; + uint8_t fs = cfg->range; iis2iclx->dev = dev; @@ -568,15 +559,15 @@ static int iis2iclx_init_chip(const struct device *dev) k_usleep(100); - if (iis2iclx_accel_set_fs_raw(dev, - IIS2ICLX_DEFAULT_ACCEL_FULLSCALE) < 0) { + LOG_DBG("range is %d", fs); + if (iis2iclx_accel_set_fs_raw(dev, fs) < 0) { LOG_ERR("failed to set accelerometer full-scale"); return -EIO; } - iis2iclx->acc_gain = IIS2ICLX_DEFAULT_ACCEL_SENSITIVITY; + iis2iclx->acc_gain = (iis2iclx_accel_fs_sens[fs] * GAIN_UNIT_XL); - iis2iclx->accel_freq = iis2iclx_odr_to_freq_val(CONFIG_IIS2ICLX_ACCEL_ODR); - if (iis2iclx_accel_set_odr_raw(dev, CONFIG_IIS2ICLX_ACCEL_ODR) < 0) { + LOG_DBG("odr is %d", odr); + if (iis2iclx_accel_set_odr_raw(dev, odr) < 0) { LOG_ERR("failed to set accelerometer sampling rate"); return -EIO; } @@ -709,6 +700,8 @@ static int iis2iclx_init(const struct device *dev) .bus_name = DT_INST_BUS_LABEL(inst), \ .bus_init = iis2iclx_spi_init, \ .bus_cfg = { .spi_cfg = IIS2ICLX_SPI_CFG(inst) }, \ + .odr = DT_INST_PROP(inst, odr), \ + .range = DT_INST_PROP(inst, range), \ COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \ (IIS2ICLX_CFG_IRQ(inst)), ()) \ } @@ -729,6 +722,8 @@ static int iis2iclx_init(const struct device *dev) .bus_name = DT_INST_BUS_LABEL(inst), \ .bus_init = iis2iclx_i2c_init, \ .bus_cfg = { .i2c_slv_addr = DT_INST_REG_ADDR(inst), }, \ + .odr = DT_INST_PROP(inst, odr), \ + .range = DT_INST_PROP(inst, range), \ COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \ (IIS2ICLX_CFG_IRQ(inst)), ()) \ } diff --git a/drivers/sensor/iis2iclx/iis2iclx.h b/drivers/sensor/iis2iclx/iis2iclx.h index 5f1ac9af38794..a47253eef8750 100644 --- a/drivers/sensor/iis2iclx/iis2iclx.h +++ b/drivers/sensor/iis2iclx/iis2iclx.h @@ -35,28 +35,6 @@ #define SENSOR_DEG2RAD_DOUBLE (SENSOR_PI_DOUBLE / 180) #define SENSOR_G_DOUBLE (SENSOR_G / 1000000.0) -#if CONFIG_IIS2ICLX_ACCEL_FS == 0 - #define IIS2ICLX_ACCEL_FS_RUNTIME 1 - #define IIS2ICLX_DEFAULT_ACCEL_FULLSCALE 0 - #define IIS2ICLX_DEFAULT_ACCEL_SENSITIVITY GAIN_UNIT_XL -#elif CONFIG_IIS2ICLX_ACCEL_FS == 500 - #define IIS2ICLX_DEFAULT_ACCEL_FULLSCALE 0 - #define IIS2ICLX_DEFAULT_ACCEL_SENSITIVITY GAIN_UNIT_XL -#elif CONFIG_IIS2ICLX_ACCEL_FS == 1000 - #define IIS2ICLX_DEFAULT_ACCEL_FULLSCALE 2 - #define IIS2ICLX_DEFAULT_ACCEL_SENSITIVITY (2.0 * GAIN_UNIT_XL) -#elif CONFIG_IIS2ICLX_ACCEL_FS == 2000 - #define IIS2ICLX_DEFAULT_ACCEL_FULLSCALE 3 - #define IIS2ICLX_DEFAULT_ACCEL_SENSITIVITY (4.0 * GAIN_UNIT_XL) -#elif CONFIG_IIS2ICLX_ACCEL_FS == 3000 - #define IIS2ICLX_DEFAULT_ACCEL_FULLSCALE 1 - #define IIS2ICLX_DEFAULT_ACCEL_SENSITIVITY (8.0 * GAIN_UNIT_XL) -#endif - -#if (CONFIG_IIS2ICLX_ACCEL_ODR == 0) -#define IIS2ICLX_ACCEL_ODR_RUNTIME 1 -#endif - #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) struct iis2iclx_spi_cfg { struct spi_config spi_conf; @@ -78,6 +56,8 @@ struct iis2iclx_config { char *bus_name; int (*bus_init)(const struct device *dev); const union iis2iclx_bus_cfg bus_cfg; + uint8_t odr; + uint8_t range; #ifdef CONFIG_IIS2ICLX_TRIGGER const char *irq_dev_name; uint8_t irq_pin; diff --git a/dts/bindings/sensor/st,iis2iclx-common.yaml b/dts/bindings/sensor/st,iis2iclx-common.yaml new file mode 100644 index 0000000000000..9b5d0db9a419f --- /dev/null +++ b/dts/bindings/sensor/st,iis2iclx-common.yaml @@ -0,0 +1,57 @@ +# Copyright (c) 2020 STMicroelectronics +# SPDX-License-Identifier: Apache-2.0 + +properties: + drdy-gpios: + type: phandle-array + required: false + description: DRDY pin + + This pin defaults to active high when produced by the sensor. + The property value should ensure the flags properly describe + the signal that is presented to the driver. + + int-pin: + type: int + required: false + default: 1 + enum: + - 1 # drdy is generated from INT1 + - 2 # drdy is generated from INT2 + description: Select DRDY pin number (1 or 2). + + This number represents which of the two interrupt pins + (INT1 or INT2) the drdy line is attached to. This property is not + mandatory and if not present it defaults to 1 which is the + configuration at power-up. + + range: + type: int + required: false + default: 3 + description: Range in g. Default is power-up configuration. + enum: + - 0 # 500mg (0.015 mg/LSB) + - 1 # 3g (0.122 mg/LSB) + - 2 # 1g (0.031 mg/LSB) + - 3 # 2g (0.061 mg/LSB) + + odr: + type: int + required: false + default: 0 + description: + Specify the default accelerometer output data rate expressed in samples per second (Hz). + Default is power-up configuration. + enum: + - 0 # Power-Down + - 1 # 12.5Hz + - 2 # 26Hz + - 3 # 52Hz + - 4 # 104Hz + - 5 # 208Hz + - 6 # 416Hz + - 7 # 833Hz + - 8 # 1660Hz + - 9 # 3330Hz + - 10 # 6660Hz diff --git a/dts/bindings/sensor/st,iis2iclx-i2c.yaml b/dts/bindings/sensor/st,iis2iclx-i2c.yaml index de49481c29271..8bddbc90230d8 100644 --- a/dts/bindings/sensor/st,iis2iclx-i2c.yaml +++ b/dts/bindings/sensor/st,iis2iclx-i2c.yaml @@ -7,24 +7,4 @@ description: | compatible: "st,iis2iclx" -include: i2c-device.yaml - -properties: - drdy-gpios: - type: phandle-array - required: false - description: DRDY pin - - This pin defaults to active high when produced by the sensor. - The property value should ensure the flags properly describe - the signal that is presented to the driver. - - int-pin: - type: int - required: false - default: 1 - description: Device INT pin number (1 or 2) - - This number represents which of the two interrupt pins - (INT1 or INT2) the line is attached to. This property is not - mandatory and if not present it defaults to 1. +include: ["i2c-device.yaml", "st,iis2iclx-common.yaml"] diff --git a/dts/bindings/sensor/st,iis2iclx-spi.yaml b/dts/bindings/sensor/st,iis2iclx-spi.yaml index 0dffa6a0d69bb..82f2b1a7b2efd 100644 --- a/dts/bindings/sensor/st,iis2iclx-spi.yaml +++ b/dts/bindings/sensor/st,iis2iclx-spi.yaml @@ -7,24 +7,4 @@ description: | compatible: "st,iis2iclx" -include: spi-device.yaml - -properties: - drdy-gpios: - type: phandle-array - required: false - description: DRDY pin - - This pin defaults to active high when produced by the sensor. - The property value should ensure the flags properly describe - the signal that is presented to the driver. - - int-pin: - type: int - required: false - default: 1 - description: Device INT pin number (1 or 2) - - This number represents which of the two interrupt pins - (INT1 or INT2) the line is attached to. This property is not - mandatory and if not present it defaults to 1. +include: ["spi-device.yaml", "st,iis2iclx-common.yaml"]