drivers: sensor: ti_hdc: 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-24 11:36:53 -05:00 committed by Carles Cufí
commit c738c99b33
2 changed files with 22 additions and 18 deletions

View file

@ -39,6 +39,7 @@ static int ti_hdc_sample_fetch(const struct device *dev,
enum sensor_channel chan)
{
struct ti_hdc_data *drv_data = dev->data;
const struct ti_hdc_config *cfg = dev->config;
uint8_t buf[4];
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
@ -50,8 +51,7 @@ static int ti_hdc_sample_fetch(const struct device *dev,
#endif
buf[0] = TI_HDC_REG_TEMP;
if (i2c_write(drv_data->i2c, buf, 1,
DT_INST_REG_ADDR(0)) < 0) {
if (i2c_write_dt(&cfg->i2c, buf, 1) < 0) {
LOG_DBG("Failed to write address pointer");
return -EIO;
}
@ -63,7 +63,7 @@ static int ti_hdc_sample_fetch(const struct device *dev,
k_msleep(HDC_CONVERSION_TIME);
#endif
if (i2c_read(drv_data->i2c, buf, 4, DT_INST_REG_ADDR(0)) < 0) {
if (i2c_read_dt(&cfg->i2c, buf, 4) < 0) {
LOG_DBG("Failed to read sample data");
return -EIO;
}
@ -110,10 +110,10 @@ static const struct sensor_driver_api ti_hdc_driver_api = {
.channel_get = ti_hdc_channel_get,
};
static uint16_t read16(const struct device *dev, uint8_t a, uint8_t d)
static uint16_t read16(const struct i2c_dt_spec *i2c, uint8_t d)
{
uint8_t buf[2];
if (i2c_burst_read(dev, a, d, (uint8_t *)buf, 2) < 0) {
if (i2c_burst_read_dt(i2c, d, (uint8_t *)buf, 2) < 0) {
LOG_ERR("Error reading register.");
}
return (buf[0] << 8 | buf[1]);
@ -121,30 +121,27 @@ static uint16_t read16(const struct device *dev, uint8_t a, uint8_t d)
static int ti_hdc_init(const struct device *dev)
{
struct ti_hdc_data *drv_data = dev->data;
const struct ti_hdc_config *cfg = dev->config;
uint16_t tmp;
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;
}
if (read16(drv_data->i2c, DT_INST_REG_ADDR(0),
TI_HDC_REG_MANUFID) != TI_HDC_MANUFID) {
if (read16(&cfg->i2c, TI_HDC_REG_MANUFID) != TI_HDC_MANUFID) {
LOG_ERR("Failed to get correct manufacturer ID");
return -EINVAL;
}
tmp = read16(drv_data->i2c, DT_INST_REG_ADDR(0),
TI_HDC_REG_DEVICEID);
tmp = read16(&cfg->i2c, TI_HDC_REG_DEVICEID);
if (tmp != TI_HDC1000_DEVID && tmp != TI_HDC1050_DEVID) {
LOG_ERR("Unsupported device ID");
return -EINVAL;
}
#if DT_INST_NODE_HAS_PROP(0, drdy_gpios)
struct ti_hdc_data *drv_data = dev->data;
k_sem_init(&drv_data->data_sem, 0, K_SEM_MAX_LIMIT);
/* setup data ready gpio interrupt */
@ -178,8 +175,12 @@ static int ti_hdc_init(const struct device *dev)
return 0;
}
static const struct ti_hdc_config ti_hdc_config = {
.i2c = I2C_DT_SPEC_INST_GET(0),
};
static struct ti_hdc_data ti_hdc_data;
DEVICE_DT_INST_DEFINE(0, ti_hdc_init, NULL, &ti_hdc_data,
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&ti_hdc_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&ti_hdc_driver_api);

View file

@ -21,8 +21,11 @@
/* For 14bit conversion RH needs 6.5ms and Temp 6.35ms */
#define HDC_CONVERSION_TIME 13
struct ti_hdc_config {
struct i2c_dt_spec i2c;
};
struct ti_hdc_data {
const struct device *i2c;
uint16_t t_sample;
uint16_t rh_sample;