drivers: sensor: max17262: 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-25 17:53:48 +02:00 committed by Maureen Helm
commit 00e22733a1
2 changed files with 9 additions and 11 deletions

View file

@ -31,8 +31,7 @@ static int max17262_reg_read(const struct device *dev, uint8_t reg_addr,
uint8_t i2c_data[2];
int rc;
rc = i2c_burst_read(cfg->i2c, cfg->i2c_addr, reg_addr,
i2c_data, 2);
rc = i2c_burst_read_dt(&cfg->i2c, reg_addr, i2c_data, 2);
if (rc < 0) {
LOG_ERR("Unable to read register");
return rc;
@ -58,8 +57,7 @@ static int max17262_reg_write(const struct device *dev, uint8_t reg_addr,
const struct max17262_config *cfg = dev->config;
uint8_t i2c_data[3] = {reg_addr, val & 0xFF, (uint16_t)val >> 8};
return i2c_write(cfg->i2c, i2c_data, sizeof(i2c_data),
cfg->i2c_addr);
return i2c_write_dt(&cfg->i2c, i2c_data, sizeof(i2c_data));
}
/**
@ -227,9 +225,9 @@ static int max17262_gauge_init(const struct device *dev)
const struct max17262_config *const config = dev->config;
int16_t tmp, hibcfg;
if (!device_is_ready(config->i2c)) {
LOG_ERR("Could not get pointer to %s device", config->i2c->name);
return -EINVAL;
if (!device_is_ready(config->i2c.bus)) {
LOG_ERR("Bus device is not ready");
return -ENODEV;
}
/* Read Status register */
@ -321,8 +319,7 @@ static const struct sensor_driver_api max17262_battery_driver_api = {
static struct max17262_data max17262_data_##n; \
\
static const struct max17262_config max17262_config_##n = { \
.i2c = DEVICE_DT_GET(DT_INST_BUS(n)), \
.i2c_addr = DT_INST_REG_ADDR(n), \
.i2c = I2C_DT_SPEC_INST_GET(n), \
.design_voltage = DT_INST_PROP(n, design_voltage), \
.desired_voltage = DT_INST_PROP(n, desired_voltage), \
.desired_charging_current = \

View file

@ -7,6 +7,8 @@
#ifndef ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17262_H_
#define ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17262_H_
#include <zephyr/drivers/i2c.h>
#define VOLTAGE_MULTIPLIER_UV 1250 / 16
#define CURRENT_MULTIPLIER_NA 156250
#define TIME_MULTIPLIER_MS 5625
@ -73,8 +75,7 @@ struct max17262_data {
};
struct max17262_config {
const struct device *i2c;
uint16_t i2c_addr;
struct i2c_dt_spec i2c;
/* Value of Rsense resistor in milliohms (typically 5 or 10) */
uint16_t rsense_mohms;
/* Design voltage of cell in mV */