diff --git a/drivers/sensor/max30101/max30101.c b/drivers/sensor/max30101/max30101.c index 6f7c314d6f6..731bf7d9038 100644 --- a/drivers/sensor/max30101/max30101.c +++ b/drivers/sensor/max30101/max30101.c @@ -13,6 +13,7 @@ LOG_MODULE_REGISTER(MAX30101, CONFIG_SENSOR_LOG_LEVEL); static int max30101_sample_fetch(struct device *dev, enum sensor_channel chan) { struct max30101_data *data = dev->driver_data; + const struct max30101_config *config = dev->config->config_info; u8_t buffer[MAX30101_MAX_BYTES_PER_SAMPLE]; u32_t fifo_data; int fifo_chan; @@ -21,7 +22,7 @@ static int max30101_sample_fetch(struct device *dev, enum sensor_channel chan) /* Read all the active channels for one sample */ num_bytes = data->num_channels * MAX30101_BYTES_PER_CHANNEL; - if (i2c_burst_read(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_burst_read(data->i2c, config->i2c_addr, MAX30101_REG_FIFO_DATA, buffer, num_bytes)) { LOG_ERR("Could not fetch sample"); return -EIO; @@ -98,14 +99,14 @@ static int max30101_init(struct device *dev) int fifo_chan; /* Get the I2C device */ - data->i2c = device_get_binding(DT_INST_0_MAX_MAX30101_BUS_NAME); + data->i2c = device_get_binding(config->i2c_label); if (!data->i2c) { LOG_ERR("Could not find I2C device"); return -EINVAL; } /* Check the part id to make sure this is MAX30101 */ - if (i2c_reg_read_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_read_byte(data->i2c, config->i2c_addr, MAX30101_REG_PART_ID, &part_id)) { LOG_ERR("Could not get Part ID"); return -EIO; @@ -117,7 +118,7 @@ static int max30101_init(struct device *dev) } /* Reset the sensor */ - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_MODE_CFG, MAX30101_MODE_CFG_RESET_MASK)) { return -EIO; @@ -125,7 +126,7 @@ static int max30101_init(struct device *dev) /* Wait for reset to be cleared */ do { - if (i2c_reg_read_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_read_byte(data->i2c, config->i2c_addr, MAX30101_REG_MODE_CFG, &mode_cfg)) { LOG_ERR("Could read mode cfg after reset"); return -EIO; @@ -133,33 +134,33 @@ static int max30101_init(struct device *dev) } while (mode_cfg & MAX30101_MODE_CFG_RESET_MASK); /* Write the FIFO configuration register */ - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_FIFO_CFG, config->fifo)) { return -EIO; } /* Write the mode configuration register */ - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_MODE_CFG, config->mode)) { return -EIO; } /* Write the SpO2 configuration register */ - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_SPO2_CFG, config->spo2)) { return -EIO; } /* Write the LED pulse amplitude registers */ - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_LED1_PA, config->led_pa[0])) { return -EIO; } - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_LED2_PA, config->led_pa[1])) { return -EIO; } - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_LED3_PA, config->led_pa[2])) { return -EIO; } @@ -171,11 +172,11 @@ static int max30101_init(struct device *dev) multi_led[0] = (config->slot[1] << 4) | (config->slot[0]); multi_led[1] = (config->slot[3] << 4) | (config->slot[2]); - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_MULTI_LED, multi_led[0])) { return -EIO; } - if (i2c_reg_write_byte(data->i2c, MAX30101_I2C_ADDRESS, + if (i2c_reg_write_byte(data->i2c, config->i2c_addr, MAX30101_REG_MULTI_LED + 1, multi_led[1])) { return -EIO; } @@ -203,6 +204,8 @@ static int max30101_init(struct device *dev) } static struct max30101_config max30101_config = { + .i2c_label = DT_INST_0_MAX_MAX30101_BUS_NAME, + .i2c_addr = DT_INST_0_MAX_MAX30101_BASE_ADDRESS, .fifo = (CONFIG_MAX30101_SMP_AVE << MAX30101_FIFO_CFG_SMP_AVE_SHIFT) | #ifdef CONFIG_MAX30101_FIFO_ROLLOVER_EN MAX30101_FIFO_CFG_ROLLOVER_EN_MASK | diff --git a/drivers/sensor/max30101/max30101.h b/drivers/sensor/max30101/max30101.h index 954d5644d21..641f8fc6b01 100644 --- a/drivers/sensor/max30101/max30101.h +++ b/drivers/sensor/max30101/max30101.h @@ -8,8 +8,6 @@ #include #include -#define MAX30101_I2C_ADDRESS 0x57 - #define MAX30101_REG_INT_STS1 0x00 #define MAX30101_REG_INT_STS2 0x01 #define MAX30101_REG_INT_EN1 0x02 @@ -88,6 +86,8 @@ enum max30101_pw { }; struct max30101_config { + const char *i2c_label; + u16_t i2c_addr; u8_t fifo; u8_t spo2; u8_t led_pa[MAX30101_MAX_NUM_CHANNELS]; diff --git a/tests/drivers/build_all/dts_fixup.h b/tests/drivers/build_all/dts_fixup.h index dc31b786bab..804be5bb2d5 100644 --- a/tests/drivers/build_all/dts_fixup.h +++ b/tests/drivers/build_all/dts_fixup.h @@ -143,6 +143,7 @@ #ifndef DT_INST_0_MAX_MAX30101_LABEL #define DT_INST_0_MAX_MAX30101_BUS_NAME "" #define DT_INST_0_MAX_MAX30101_LABEL "" +#define DT_INST_0_MAX_MAX30101_BASE_ADDRESS 0x57 #endif #ifndef DT_INST_0_SEMTECH_SX1509B_LABEL