drivers: sensor: lis2dh: Update driver to use i2c_dt_spec

Simplify driver by using i2c_dt_spec for bus access.

Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
This commit is contained in:
Benjamin Björnsson 2022-06-30 06:58:21 +02:00 committed by Maureen Helm
commit d2aeb94630
3 changed files with 20 additions and 34 deletions

View file

@ -333,14 +333,11 @@ int lis2dh_init(const struct device *dev)
uint8_t id;
uint8_t raw[6];
lis2dh->bus = device_get_binding(cfg->bus_name);
if (!lis2dh->bus) {
LOG_ERR("master not found: %s", cfg->bus_name);
return -EINVAL;
status = cfg->bus_init(dev);
if (status < 0) {
return status;
}
cfg->bus_init(dev);
status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_WAI, &id);
if (status < 0) {
LOG_ERR("Failed to read chip id.");
@ -421,9 +418,8 @@ int lis2dh_init(const struct device *dev)
}
#endif
LOG_INF("bus=%s fs=%d, odr=0x%x lp_en=0x%x scale=%d",
cfg->bus_name, 1 << (LIS2DH_FS_IDX + 1),
LIS2DH_ODR_IDX, (uint8_t)LIS2DH_LP_EN_BIT, lis2dh->scale);
LOG_INF("fs=%d, odr=0x%x lp_en=0x%x scale=%d", 1 << (LIS2DH_FS_IDX + 1), LIS2DH_ODR_IDX,
(uint8_t)LIS2DH_LP_EN_BIT, lis2dh->scale);
/* enable accel measurements and set power mode and data rate */
return lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_CTRL1,
@ -545,7 +541,6 @@ static int lis2dh_pm_action(const struct device *dev,
#define LIS2DH_CONFIG_SPI(inst) \
{ \
.bus_name = DT_INST_BUS_LABEL(inst), \
.bus_init = lis2dh_spi_init, \
.bus_cfg = { .spi = SPI_DT_SPEC_INST_GET(inst, \
SPI_WORD_SET(8) | \
@ -572,9 +567,8 @@ static int lis2dh_pm_action(const struct device *dev,
#define LIS2DH_CONFIG_I2C(inst) \
{ \
.bus_name = DT_INST_BUS_LABEL(inst), \
.bus_init = lis2dh_i2c_init, \
.bus_cfg = { .i2c_slv_addr = DT_INST_REG_ADDR(inst), }, \
.bus_cfg = { .i2c = I2C_DT_SPEC_INST_GET(inst), }, \
.hw = { .is_lsm303agr_dev = IS_LSM303AGR_DEV(inst), \
.disc_pull_up = DISC_PULL_UP(inst), \
.anym_on_int1 = ANYM_ON_INT1(inst), }, \

View file

@ -182,7 +182,7 @@ union lis2dh_sample {
union lis2dh_bus_cfg {
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
uint16_t i2c_slv_addr;
struct i2c_dt_spec i2c;
#endif
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
@ -198,7 +198,6 @@ struct temperature {
};
struct lis2dh_config {
const char *bus_name;
int (*bus_init)(const struct device *dev);
const union lis2dh_bus_cfg bus_cfg;
#ifdef CONFIG_LIS2DH_TRIGGER

View file

@ -23,56 +23,43 @@ LOG_MODULE_DECLARE(lis2dh, CONFIG_SENSOR_LOG_LEVEL);
static int lis2dh_i2c_read_data(const struct device *dev, uint8_t reg_addr,
uint8_t *value, uint8_t len)
{
struct lis2dh_data *data = dev->data;
const struct lis2dh_config *cfg = dev->config;
return i2c_burst_read(data->bus, cfg->bus_cfg.i2c_slv_addr,
reg_addr | LIS2DH_AUTOINCREMENT_ADDR,
value, len);
return i2c_burst_read_dt(&cfg->bus_cfg.i2c, reg_addr | LIS2DH_AUTOINCREMENT_ADDR, value,
len);
}
static int lis2dh_i2c_write_data(const struct device *dev, uint8_t reg_addr,
uint8_t *value, uint8_t len)
{
struct lis2dh_data *data = dev->data;
const struct lis2dh_config *cfg = dev->config;
return i2c_burst_write(data->bus, cfg->bus_cfg.i2c_slv_addr,
reg_addr | LIS2DH_AUTOINCREMENT_ADDR,
value, len);
return i2c_burst_write_dt(&cfg->bus_cfg.i2c, reg_addr | LIS2DH_AUTOINCREMENT_ADDR, value,
len);
}
static int lis2dh_i2c_read_reg(const struct device *dev, uint8_t reg_addr,
uint8_t *value)
{
struct lis2dh_data *data = dev->data;
const struct lis2dh_config *cfg = dev->config;
return i2c_reg_read_byte(data->bus,
cfg->bus_cfg.i2c_slv_addr,
reg_addr, value);
return i2c_reg_read_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value);
}
static int lis2dh_i2c_write_reg(const struct device *dev, uint8_t reg_addr,
uint8_t value)
{
struct lis2dh_data *data = dev->data;
const struct lis2dh_config *cfg = dev->config;
return i2c_reg_write_byte(data->bus,
cfg->bus_cfg.i2c_slv_addr,
reg_addr, value);
return i2c_reg_write_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value);
}
static int lis2dh_i2c_update_reg(const struct device *dev, uint8_t reg_addr,
uint8_t mask, uint8_t value)
{
struct lis2dh_data *data = dev->data;
const struct lis2dh_config *cfg = dev->config;
return i2c_reg_update_byte(data->bus,
cfg->bus_cfg.i2c_slv_addr,
reg_addr, mask, value);
return i2c_reg_update_byte_dt(&cfg->bus_cfg.i2c, reg_addr, mask, value);
}
static const struct lis2dh_transfer_function lis2dh_i2c_transfer_fn = {
@ -86,6 +73,12 @@ static const struct lis2dh_transfer_function lis2dh_i2c_transfer_fn = {
int lis2dh_i2c_init(const struct device *dev)
{
struct lis2dh_data *data = dev->data;
const struct lis2dh_config *cfg = dev->config;
if (!device_is_ready(cfg->bus_cfg.i2c.bus)) {
LOG_ERR("Bus device is not ready");
return -ENODEV;
}
data->hw_tf = &lis2dh_i2c_transfer_fn;