drivers: sensor: bmi160: Prepare to support I2C
At present this driver only supports SPI. Refactor it so that SPI is just one of the options. This does not change any functionality. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
e43c181934
commit
0de655e371
2 changed files with 73 additions and 15 deletions
|
@ -21,6 +21,11 @@
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(BMI160, CONFIG_SENSOR_LOG_LEVEL);
|
LOG_MODULE_REGISTER(BMI160, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
|
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
|
||||||
|
#warning "BMI160 driver enabled without any devices"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if BMI160_BUS_SPI
|
||||||
static int bmi160_transceive(const struct device *dev, uint8_t reg,
|
static int bmi160_transceive(const struct device *dev, uint8_t reg,
|
||||||
bool write, void *buf, size_t length)
|
bool write, void *buf, size_t length)
|
||||||
{
|
{
|
||||||
|
@ -47,17 +52,41 @@ static int bmi160_transceive(const struct device *dev, uint8_t reg,
|
||||||
.count = 2
|
.count = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
return spi_transceive(data->bus, &cfg->spi_cfg, &tx, &rx);
|
return spi_transceive(data->bus, cfg->bus_cfg.spi_cfg, &tx,
|
||||||
|
&rx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return spi_write(data->bus, &cfg->spi_cfg, &tx);
|
return spi_write(data->bus, cfg->bus_cfg.spi_cfg, &tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_read(const struct device *dev, uint8_t reg_addr, void *data,
|
int bmi160_read_spi(const struct device *dev,
|
||||||
|
const struct bmi160_bus_cfg *bus_config, uint8_t reg_addr,
|
||||||
|
void *buf, uint8_t len)
|
||||||
|
{
|
||||||
|
return bmi160_transceive(dev, reg_addr | BMI160_REG_READ, false,
|
||||||
|
buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bmi160_write_spi(const struct device *dev,
|
||||||
|
const struct bmi160_bus_cfg *bus_config,
|
||||||
|
uint8_t reg_addr, void *buf, uint8_t len)
|
||||||
|
{
|
||||||
|
return bmi160_transceive(dev, reg_addr & BMI160_REG_MASK, true,
|
||||||
|
buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct bmi160_reg_io bmi160_reg_io_spi = {
|
||||||
|
.read = bmi160_read_spi,
|
||||||
|
.write = bmi160_write_spi,
|
||||||
|
};
|
||||||
|
#endif /* BMI160_BUS_SPI */
|
||||||
|
|
||||||
|
int bmi160_read(const struct device *dev, uint8_t reg_addr, void *buf,
|
||||||
uint8_t len)
|
uint8_t len)
|
||||||
{
|
{
|
||||||
return bmi160_transceive(dev, reg_addr | BMI160_REG_READ, false, data,
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
len);
|
|
||||||
|
return cfg->reg_io->read(dev, &cfg->bus_cfg, reg_addr, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_byte_read(const struct device *dev, uint8_t reg_addr, uint8_t *byte)
|
int bmi160_byte_read(const struct device *dev, uint8_t reg_addr, uint8_t *byte)
|
||||||
|
@ -80,11 +109,12 @@ static int bmi160_word_read(const struct device *dev, uint8_t reg_addr,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_write(const struct device *dev, uint8_t reg_addr, void *data,
|
int bmi160_write(const struct device *dev, uint8_t reg_addr, void *buf,
|
||||||
uint8_t len)
|
uint8_t len)
|
||||||
{
|
{
|
||||||
return bmi160_transceive(dev, reg_addr & BMI160_REG_MASK, true, data,
|
const struct bmi160_cfg *cfg = to_config(dev);
|
||||||
len);
|
|
||||||
|
return cfg->reg_io->write(dev, &cfg->bus_cfg, reg_addr, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bmi160_byte_write(const struct device *dev, uint8_t reg_addr,
|
int bmi160_byte_write(const struct device *dev, uint8_t reg_addr,
|
||||||
|
@ -923,20 +953,25 @@ int bmi160_init(const struct device *dev)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define BMI160_DEVICE_INIT(inst) \
|
#define BMI160_DEVICE_INIT(inst) \
|
||||||
DEVICE_AND_API_INIT(bmi160, DT_INST_LABEL(inst), bmi160_init, \
|
DEVICE_AND_API_INIT(bmi160_##inst, DT_INST_LABEL(inst), \
|
||||||
&bmi160_data_##inst, &bmi160_cfg_##inst, \
|
bmi160_init, &bmi160_data_##inst, &bmi160_cfg_##inst, \
|
||||||
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
|
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
|
||||||
&bmi160_api);
|
&bmi160_api);
|
||||||
|
|
||||||
|
/* Instantiation macros used when a device is on a SPI bus */
|
||||||
#define BMI160_DEFINE_SPI(inst) \
|
#define BMI160_DEFINE_SPI(inst) \
|
||||||
static struct bmi160_data bmi160_data_##inst; \
|
static struct bmi160_data bmi160_data_##inst; \
|
||||||
static const struct bmi160_cfg bmi160_cfg_##inst = { \
|
static const struct bmi160_cfg bmi160_cfg_##inst = { \
|
||||||
BMI160_TRIGGER_CFG(inst) \
|
BMI160_TRIGGER_CFG(inst) \
|
||||||
|
.reg_io = &bmi160_reg_io_spi, \
|
||||||
.bus_label = DT_INST_BUS_LABEL(inst), \
|
.bus_label = DT_INST_BUS_LABEL(inst), \
|
||||||
.spi_cfg = { \
|
.bus_cfg = { \
|
||||||
.operation = SPI_WORD_SET(8), \
|
.spi_cfg = (&(struct spi_config) { \
|
||||||
.frequency = DT_INST_PROP(inst, spi_max_frequency), \
|
.operation = SPI_WORD_SET(8), \
|
||||||
.slave = DT_INST_REG_ADDR(inst), \
|
.frequency = DT_INST_PROP(inst, \
|
||||||
|
spi_max_frequency), \
|
||||||
|
.slave = DT_INST_REG_ADDR(inst), \
|
||||||
|
}), \
|
||||||
}, \
|
}, \
|
||||||
}; \
|
}; \
|
||||||
BMI160_DEVICE_INIT(inst)
|
BMI160_DEVICE_INIT(inst)
|
||||||
|
|
|
@ -398,8 +398,31 @@ struct bmi160_range {
|
||||||
uint8_t reg_val;
|
uint8_t reg_val;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define BMI160_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||||
|
|
||||||
|
struct bmi160_bus_cfg {
|
||||||
|
union {
|
||||||
|
#if BMI160_BUS_SPI
|
||||||
|
const struct spi_config *spi_cfg;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef int (*bmi160_reg_read_fn)(const struct device *bus,
|
||||||
|
const struct bmi160_bus_cfg *bus_cfg,
|
||||||
|
uint8_t reg_addr, void *data, uint8_t len);
|
||||||
|
typedef int (*bmi160_reg_write_fn)(const struct device *bus,
|
||||||
|
const struct bmi160_bus_cfg *bus_cfg,
|
||||||
|
uint8_t reg_addr, void *data, uint8_t len);
|
||||||
|
|
||||||
|
struct bmi160_reg_io {
|
||||||
|
bmi160_reg_read_fn read;
|
||||||
|
bmi160_reg_write_fn write;
|
||||||
|
};
|
||||||
|
|
||||||
struct bmi160_cfg {
|
struct bmi160_cfg {
|
||||||
struct spi_config spi_cfg;
|
struct bmi160_bus_cfg bus_cfg;
|
||||||
|
const struct bmi160_reg_io *reg_io;
|
||||||
const char *bus_label;
|
const char *bus_label;
|
||||||
#if defined(CONFIG_BMI160_TRIGGER)
|
#if defined(CONFIG_BMI160_TRIGGER)
|
||||||
const char *gpio_port;
|
const char *gpio_port;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue