From 6318d290358ed7980fd7737ea642bc07f78abdeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Bj=C3=B6rnsson?= Date: Sat, 25 Jun 2022 17:43:52 +0200 Subject: [PATCH] drivers: sensor: max17055: Update driver to use i2c_dt_spec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify driver by using i2c_dt_spec for bus access. Signed-off-by: Benjamin Björnsson --- drivers/sensor/max17055/max17055.c | 18 ++++++++---------- drivers/sensor/max17055/max17055.h | 5 +++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/drivers/sensor/max17055/max17055.c b/drivers/sensor/max17055/max17055.c index 0c0c3e13b4b..88a1af08954 100644 --- a/drivers/sensor/max17055/max17055.c +++ b/drivers/sensor/max17055/max17055.c @@ -28,11 +28,11 @@ LOG_MODULE_REGISTER(max17055, CONFIG_SENSOR_LOG_LEVEL); static int max17055_reg_read(const struct device *dev, int reg_addr, int16_t *valp) { - struct max17055_data *priv = dev->data; + const struct max17055_config *config = dev->config; uint8_t i2c_data[2]; int rc; - rc = i2c_burst_read(priv->i2c, DT_INST_REG_ADDR(0), reg_addr, i2c_data, 2); + rc = i2c_burst_read_dt(&config->i2c, reg_addr, i2c_data, 2); if (rc < 0) { LOG_ERR("Unable to read register"); return rc; @@ -45,13 +45,13 @@ static int max17055_reg_read(const struct device *dev, int reg_addr, static int max17055_reg_write(const struct device *dev, int reg_addr, uint16_t val) { - struct max17055_data *priv = dev->data; + const struct max17055_config *config = dev->config; uint8_t buf[3]; buf[0] = (uint8_t)reg_addr; sys_put_le16(val, &buf[1]); - return i2c_write(priv->i2c, buf, sizeof(buf), DT_INST_REG_ADDR(0)); + return i2c_write_dt(&config->i2c, buf, sizeof(buf)); } /** @@ -347,13 +347,11 @@ static int max17055_init_config(const struct device *dev) static int max17055_gauge_init(const struct device *dev) { int16_t tmp; - struct max17055_data *priv = dev->data; const struct max17055_config *const config = dev->config; - priv->i2c = device_get_binding(config->bus_name); - if (!priv->i2c) { - LOG_ERR("Could not get pointer to %s device", config->bus_name); - return -EINVAL; + if (!device_is_ready(config->i2c.bus)) { + LOG_ERR("Bus device is not ready"); + return -ENODEV; } if (max17055_reg_read(dev, STATUS, &tmp)) { @@ -393,7 +391,7 @@ static const struct sensor_driver_api max17055_battery_driver_api = { static struct max17055_data max17055_driver_##index; \ \ static const struct max17055_config max17055_config_##index = { \ - .bus_name = DT_INST_BUS_LABEL(index), \ + .i2c = I2C_DT_SPEC_INST_GET(index), \ .design_capacity = DT_INST_PROP(index, design_capacity), \ .design_voltage = DT_INST_PROP(index, design_voltage), \ .desired_charging_current = DT_INST_PROP(index, desired_charging_current), \ diff --git a/drivers/sensor/max17055/max17055.h b/drivers/sensor/max17055/max17055.h index 9dade31eb03..377d660cc69 100644 --- a/drivers/sensor/max17055/max17055.h +++ b/drivers/sensor/max17055/max17055.h @@ -7,6 +7,8 @@ #ifndef ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17055_H_ #define ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17055_H_ +#include + /* Register addresses */ enum { STATUS = 0x0, @@ -41,7 +43,6 @@ enum { }; struct max17055_data { - const struct device *i2c; /* Current cell voltage in units of 1.25/16mV */ uint16_t voltage; /* Average current in units of 1.5625uV / Rsense */ @@ -65,7 +66,7 @@ struct max17055_data { }; struct max17055_config { - char *bus_name; + struct i2c_dt_spec i2c; /* Value of Rsense resistor in milliohms (typically 5 or 10) */ uint16_t rsense_mohms; /* The design capacity (aka label capacity) of the cell in mAh */