Skip to content

Commit 6318d29

Browse files
benjaminbjornssoncarlescufi
authored andcommitted
drivers: sensor: max17055: Update driver to use i2c_dt_spec
Simplify driver by using i2c_dt_spec for bus access. Signed-off-by: Benjamin Björnsson <[email protected]>
1 parent 805ad30 commit 6318d29

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

drivers/sensor/max17055/max17055.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ LOG_MODULE_REGISTER(max17055, CONFIG_SENSOR_LOG_LEVEL);
2828
static int max17055_reg_read(const struct device *dev, int reg_addr,
2929
int16_t *valp)
3030
{
31-
struct max17055_data *priv = dev->data;
31+
const struct max17055_config *config = dev->config;
3232
uint8_t i2c_data[2];
3333
int rc;
3434

35-
rc = i2c_burst_read(priv->i2c, DT_INST_REG_ADDR(0), reg_addr, i2c_data, 2);
35+
rc = i2c_burst_read_dt(&config->i2c, reg_addr, i2c_data, 2);
3636
if (rc < 0) {
3737
LOG_ERR("Unable to read register");
3838
return rc;
@@ -45,13 +45,13 @@ static int max17055_reg_read(const struct device *dev, int reg_addr,
4545
static int max17055_reg_write(const struct device *dev, int reg_addr,
4646
uint16_t val)
4747
{
48-
struct max17055_data *priv = dev->data;
48+
const struct max17055_config *config = dev->config;
4949
uint8_t buf[3];
5050

5151
buf[0] = (uint8_t)reg_addr;
5252
sys_put_le16(val, &buf[1]);
5353

54-
return i2c_write(priv->i2c, buf, sizeof(buf), DT_INST_REG_ADDR(0));
54+
return i2c_write_dt(&config->i2c, buf, sizeof(buf));
5555
}
5656

5757
/**
@@ -347,13 +347,11 @@ static int max17055_init_config(const struct device *dev)
347347
static int max17055_gauge_init(const struct device *dev)
348348
{
349349
int16_t tmp;
350-
struct max17055_data *priv = dev->data;
351350
const struct max17055_config *const config = dev->config;
352351

353-
priv->i2c = device_get_binding(config->bus_name);
354-
if (!priv->i2c) {
355-
LOG_ERR("Could not get pointer to %s device", config->bus_name);
356-
return -EINVAL;
352+
if (!device_is_ready(config->i2c.bus)) {
353+
LOG_ERR("Bus device is not ready");
354+
return -ENODEV;
357355
}
358356

359357
if (max17055_reg_read(dev, STATUS, &tmp)) {
@@ -393,7 +391,7 @@ static const struct sensor_driver_api max17055_battery_driver_api = {
393391
static struct max17055_data max17055_driver_##index; \
394392
\
395393
static const struct max17055_config max17055_config_##index = { \
396-
.bus_name = DT_INST_BUS_LABEL(index), \
394+
.i2c = I2C_DT_SPEC_INST_GET(index), \
397395
.design_capacity = DT_INST_PROP(index, design_capacity), \
398396
.design_voltage = DT_INST_PROP(index, design_voltage), \
399397
.desired_charging_current = DT_INST_PROP(index, desired_charging_current), \

drivers/sensor/max17055/max17055.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17055_H_
88
#define ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17055_H_
99

10+
#include <zephyr/drivers/i2c.h>
11+
1012
/* Register addresses */
1113
enum {
1214
STATUS = 0x0,
@@ -41,7 +43,6 @@ enum {
4143
};
4244

4345
struct max17055_data {
44-
const struct device *i2c;
4546
/* Current cell voltage in units of 1.25/16mV */
4647
uint16_t voltage;
4748
/* Average current in units of 1.5625uV / Rsense */
@@ -65,7 +66,7 @@ struct max17055_data {
6566
};
6667

6768
struct max17055_config {
68-
char *bus_name;
69+
struct i2c_dt_spec i2c;
6970
/* Value of Rsense resistor in milliohms (typically 5 or 10) */
7071
uint16_t rsense_mohms;
7172
/* The design capacity (aka label capacity) of the cell in mAh */

0 commit comments

Comments
 (0)