driver: sensor: Add i2c_label and i2c_addr in the max30101_config struct

Set the label and addr from the device tree.

Signed-off-by: Philémon Jaermann <p.jaermann@gmail.com>
This commit is contained in:
Philémon Jaermann 2020-02-15 17:49:58 +01:00 committed by Johan Hedberg
commit 73ae0d8410
3 changed files with 19 additions and 15 deletions

View file

@ -13,6 +13,7 @@ LOG_MODULE_REGISTER(MAX30101, CONFIG_SENSOR_LOG_LEVEL);
static int max30101_sample_fetch(struct device *dev, enum sensor_channel chan) static int max30101_sample_fetch(struct device *dev, enum sensor_channel chan)
{ {
struct max30101_data *data = dev->driver_data; struct max30101_data *data = dev->driver_data;
const struct max30101_config *config = dev->config->config_info;
u8_t buffer[MAX30101_MAX_BYTES_PER_SAMPLE]; u8_t buffer[MAX30101_MAX_BYTES_PER_SAMPLE];
u32_t fifo_data; u32_t fifo_data;
int fifo_chan; 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 */ /* Read all the active channels for one sample */
num_bytes = data->num_channels * MAX30101_BYTES_PER_CHANNEL; 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)) { MAX30101_REG_FIFO_DATA, buffer, num_bytes)) {
LOG_ERR("Could not fetch sample"); LOG_ERR("Could not fetch sample");
return -EIO; return -EIO;
@ -98,14 +99,14 @@ static int max30101_init(struct device *dev)
int fifo_chan; int fifo_chan;
/* Get the I2C device */ /* 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) { if (!data->i2c) {
LOG_ERR("Could not find I2C device"); LOG_ERR("Could not find I2C device");
return -EINVAL; return -EINVAL;
} }
/* Check the part id to make sure this is MAX30101 */ /* 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)) { MAX30101_REG_PART_ID, &part_id)) {
LOG_ERR("Could not get Part ID"); LOG_ERR("Could not get Part ID");
return -EIO; return -EIO;
@ -117,7 +118,7 @@ static int max30101_init(struct device *dev)
} }
/* Reset the sensor */ /* 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_REG_MODE_CFG,
MAX30101_MODE_CFG_RESET_MASK)) { MAX30101_MODE_CFG_RESET_MASK)) {
return -EIO; return -EIO;
@ -125,7 +126,7 @@ static int max30101_init(struct device *dev)
/* Wait for reset to be cleared */ /* Wait for reset to be cleared */
do { 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)) { MAX30101_REG_MODE_CFG, &mode_cfg)) {
LOG_ERR("Could read mode cfg after reset"); LOG_ERR("Could read mode cfg after reset");
return -EIO; return -EIO;
@ -133,33 +134,33 @@ static int max30101_init(struct device *dev)
} while (mode_cfg & MAX30101_MODE_CFG_RESET_MASK); } while (mode_cfg & MAX30101_MODE_CFG_RESET_MASK);
/* Write the FIFO configuration register */ /* 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)) { MAX30101_REG_FIFO_CFG, config->fifo)) {
return -EIO; return -EIO;
} }
/* Write the mode configuration register */ /* 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)) { MAX30101_REG_MODE_CFG, config->mode)) {
return -EIO; return -EIO;
} }
/* Write the SpO2 configuration register */ /* 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)) { MAX30101_REG_SPO2_CFG, config->spo2)) {
return -EIO; return -EIO;
} }
/* Write the LED pulse amplitude registers */ /* 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])) { MAX30101_REG_LED1_PA, config->led_pa[0])) {
return -EIO; 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])) { MAX30101_REG_LED2_PA, config->led_pa[1])) {
return -EIO; 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])) { MAX30101_REG_LED3_PA, config->led_pa[2])) {
return -EIO; 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[0] = (config->slot[1] << 4) | (config->slot[0]);
multi_led[1] = (config->slot[3] << 4) | (config->slot[2]); 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])) { MAX30101_REG_MULTI_LED, multi_led[0])) {
return -EIO; 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])) { MAX30101_REG_MULTI_LED + 1, multi_led[1])) {
return -EIO; return -EIO;
} }
@ -203,6 +204,8 @@ static int max30101_init(struct device *dev)
} }
static struct max30101_config max30101_config = { 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) | .fifo = (CONFIG_MAX30101_SMP_AVE << MAX30101_FIFO_CFG_SMP_AVE_SHIFT) |
#ifdef CONFIG_MAX30101_FIFO_ROLLOVER_EN #ifdef CONFIG_MAX30101_FIFO_ROLLOVER_EN
MAX30101_FIFO_CFG_ROLLOVER_EN_MASK | MAX30101_FIFO_CFG_ROLLOVER_EN_MASK |

View file

@ -8,8 +8,6 @@
#include <drivers/i2c.h> #include <drivers/i2c.h>
#include <drivers/gpio.h> #include <drivers/gpio.h>
#define MAX30101_I2C_ADDRESS 0x57
#define MAX30101_REG_INT_STS1 0x00 #define MAX30101_REG_INT_STS1 0x00
#define MAX30101_REG_INT_STS2 0x01 #define MAX30101_REG_INT_STS2 0x01
#define MAX30101_REG_INT_EN1 0x02 #define MAX30101_REG_INT_EN1 0x02
@ -88,6 +86,8 @@ enum max30101_pw {
}; };
struct max30101_config { struct max30101_config {
const char *i2c_label;
u16_t i2c_addr;
u8_t fifo; u8_t fifo;
u8_t spo2; u8_t spo2;
u8_t led_pa[MAX30101_MAX_NUM_CHANNELS]; u8_t led_pa[MAX30101_MAX_NUM_CHANNELS];

View file

@ -143,6 +143,7 @@
#ifndef DT_INST_0_MAX_MAX30101_LABEL #ifndef DT_INST_0_MAX_MAX30101_LABEL
#define DT_INST_0_MAX_MAX30101_BUS_NAME "" #define DT_INST_0_MAX_MAX30101_BUS_NAME ""
#define DT_INST_0_MAX_MAX30101_LABEL "" #define DT_INST_0_MAX_MAX30101_LABEL ""
#define DT_INST_0_MAX_MAX30101_BASE_ADDRESS 0x57
#endif #endif
#ifndef DT_INST_0_SEMTECH_SX1509B_LABEL #ifndef DT_INST_0_SEMTECH_SX1509B_LABEL