drivers: sensor: tmp007: Update driver to use i2c_dt_spec

Move driver to use i2c_dt_spec for I2C bus access.

Signed-off-by: Kumar Gala <galak@kernel.org>
This commit is contained in:
Kumar Gala 2022-06-28 09:22:52 -05:00 committed by Maureen Helm
commit d184190863
3 changed files with 32 additions and 28 deletions

View file

@ -20,11 +20,9 @@
LOG_MODULE_REGISTER(TMP007, CONFIG_SENSOR_LOG_LEVEL);
int tmp007_reg_read(struct tmp007_data *drv_data,
uint8_t reg, uint16_t *val)
int tmp007_reg_read(const struct i2c_dt_spec *i2c, uint8_t reg, uint16_t *val)
{
if (i2c_burst_read(drv_data->i2c, TMP007_I2C_ADDRESS,
reg, (uint8_t *) val, 2) < 0) {
if (i2c_burst_read_dt(i2c, reg, (uint8_t *) val, 2) < 0) {
LOG_ERR("I2C read failed");
return -EIO;
}
@ -34,39 +32,39 @@ int tmp007_reg_read(struct tmp007_data *drv_data,
return 0;
}
int tmp007_reg_write(struct tmp007_data *drv_data, uint8_t reg, uint16_t val)
int tmp007_reg_write(const struct i2c_dt_spec *i2c, uint8_t reg, uint16_t val)
{
uint8_t tx_buf[3] = {reg, val >> 8, val & 0xFF};
return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf),
TMP007_I2C_ADDRESS);
return i2c_write_dt(i2c, tx_buf, sizeof(tx_buf));
}
int tmp007_reg_update(struct tmp007_data *drv_data, uint8_t reg,
int tmp007_reg_update(const struct i2c_dt_spec *i2c, uint8_t reg,
uint16_t mask, uint16_t val)
{
uint16_t old_val = 0U;
uint16_t new_val;
if (tmp007_reg_read(drv_data, reg, &old_val) < 0) {
if (tmp007_reg_read(i2c, reg, &old_val) < 0) {
return -EIO;
}
new_val = old_val & ~mask;
new_val |= val & mask;
return tmp007_reg_write(drv_data, reg, new_val);
return tmp007_reg_write(i2c, reg, new_val);
}
static int tmp007_sample_fetch(const struct device *dev,
enum sensor_channel chan)
{
struct tmp007_data *drv_data = dev->data;
const struct tmp007_config *cfg = dev->config;
uint16_t val;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
if (tmp007_reg_read(drv_data, TMP007_REG_TOBJ, &val) < 0) {
if (tmp007_reg_read(&cfg->i2c, TMP007_REG_TOBJ, &val) < 0) {
return -EIO;
}
@ -108,13 +106,11 @@ static const struct sensor_driver_api tmp007_driver_api = {
int tmp007_init(const struct device *dev)
{
struct tmp007_data *drv_data = dev->data;
const struct tmp007_config *cfg = dev->config;
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
if (drv_data->i2c == NULL) {
LOG_DBG("Failed to get pointer to %s device!",
DT_INST_BUS_LABEL(0));
return -EINVAL;
if (!device_is_ready(cfg->i2c.bus)) {
LOG_ERR("Bus device is not ready");
return -ENODEV;
}
#ifdef CONFIG_TMP007_TRIGGER
@ -127,9 +123,13 @@ int tmp007_init(const struct device *dev)
return 0;
}
static const struct tmp007_config tmp007_config = {
.i2c = I2C_DT_SPEC_INST_GET(0),
};
struct tmp007_data tmp007_driver;
DEVICE_DT_INST_DEFINE(0, tmp007_init, NULL,
&tmp007_driver,
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&tmp007_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&tmp007_driver_api);

View file

@ -8,11 +8,10 @@
#define ZEPHYR_DRIVERS_SENSOR_TMP007_TMP007_H_
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/util.h>
#define TMP007_I2C_ADDRESS DT_INST_REG_ADDR(0)
#define TMP007_REG_CONFIG 0x02
#define TMP007_ALERT_EN_BIT BIT(8)
@ -33,8 +32,11 @@
#define TMP007_TEMP_SCALE 31250
#define TMP007_TEMP_TH_SCALE 500000
struct tmp007_config {
struct i2c_dt_spec i2c;
};
struct tmp007_data {
const struct device *i2c;
int16_t sample;
#ifdef CONFIG_TMP007_TRIGGER
@ -60,11 +62,11 @@ struct tmp007_data {
};
#ifdef CONFIG_TMP007_TRIGGER
int tmp007_reg_read(struct tmp007_data *drv_data, uint8_t reg, uint16_t *val);
int tmp007_reg_read(const struct i2c_dt_spec *i2c, uint8_t reg, uint16_t *val);
int tmp007_reg_write(struct tmp007_data *drv_data, uint8_t reg, uint16_t val);
int tmp007_reg_write(const struct i2c_dt_spec *i2c, uint8_t reg, uint16_t val);
int tmp007_reg_update(struct tmp007_data *drv_data, uint8_t reg,
int tmp007_reg_update(const struct i2c_dt_spec *i2c, uint8_t reg,
uint16_t mask, uint16_t val);
int tmp007_attr_set(const struct device *dev,

View file

@ -36,7 +36,7 @@ int tmp007_attr_set(const struct device *dev,
enum sensor_attribute attr,
const struct sensor_value *val)
{
struct tmp007_data *drv_data = dev->data;
const struct tmp007_config *cfg = dev->config;
int64_t value;
uint8_t reg;
@ -55,7 +55,7 @@ int tmp007_attr_set(const struct device *dev,
value = (int64_t)val->val1 * 1000000 + val->val2;
value = (value / TMP007_TEMP_TH_SCALE) << 6;
if (tmp007_reg_write(drv_data, reg, value) < 0) {
if (tmp007_reg_write(&cfg->i2c, reg, value) < 0) {
LOG_DBG("Failed to set attribute!");
return -EIO;
}
@ -81,9 +81,10 @@ static void tmp007_gpio_callback(const struct device *dev,
static void tmp007_thread_cb(const struct device *dev)
{
struct tmp007_data *drv_data = dev->data;
const struct tmp007_config *cfg = dev->config;
uint16_t status;
if (tmp007_reg_read(drv_data, TMP007_REG_STATUS, &status) < 0) {
if (tmp007_reg_read(&cfg->i2c, TMP007_REG_STATUS, &status) < 0) {
return;
}
@ -144,8 +145,9 @@ int tmp007_trigger_set(const struct device *dev,
int tmp007_init_interrupt(const struct device *dev)
{
struct tmp007_data *drv_data = dev->data;
const struct tmp007_config *cfg = dev->config;
if (tmp007_reg_update(drv_data, TMP007_REG_CONFIG,
if (tmp007_reg_update(&cfg->i2c, TMP007_REG_CONFIG,
TMP007_ALERT_EN_BIT, TMP007_ALERT_EN_BIT) < 0) {
LOG_DBG("Failed to enable interrupt pin!");
return -EIO;