drivers: sensors: tmp112: Allow multiple instances
Allow to instanciate multiple instance. Use compile-time defined i2c device instance. Signed-off-by: Alexander Wachter <alexander@wachter.cloud>
This commit is contained in:
parent
57e7fba288
commit
704ec188f2
1 changed files with 31 additions and 24 deletions
|
@ -17,7 +17,6 @@
|
|||
|
||||
LOG_MODULE_REGISTER(TMP112, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
#define TMP112_I2C_ADDRESS DT_INST_REG_ADDR(0)
|
||||
|
||||
#define TMP112_REG_TEMPERATURE 0x00
|
||||
#define TMP112_D0_BIT BIT(0)
|
||||
|
@ -31,46 +30,49 @@ LOG_MODULE_REGISTER(TMP112, CONFIG_SENSOR_LOG_LEVEL);
|
|||
#define TMP112_TEMP_SCALE 62500
|
||||
|
||||
struct tmp112_data {
|
||||
const struct device *i2c;
|
||||
int16_t sample;
|
||||
};
|
||||
|
||||
static int tmp112_reg_read(struct tmp112_data *drv_data,
|
||||
struct tmp112_config {
|
||||
struct i2c_dt_spec bus;
|
||||
};
|
||||
|
||||
static int tmp112_reg_read(const struct tmp112_config *cfg,
|
||||
uint8_t reg, uint16_t *val)
|
||||
{
|
||||
if (i2c_burst_read(drv_data->i2c, TMP112_I2C_ADDRESS,
|
||||
reg, (uint8_t *) val, 2) < 0) {
|
||||
uint8_t buf[sizeof(val)];
|
||||
|
||||
if (i2c_burst_read_dt(&cfg->bus, reg, buf, sizeof(buf)) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
*val = sys_be16_to_cpu(*val);
|
||||
*val = sys_get_be16(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tmp112_reg_write(struct tmp112_data *drv_data,
|
||||
static int tmp112_reg_write(const struct tmp112_config *cfg,
|
||||
uint8_t reg, uint16_t val)
|
||||
{
|
||||
uint16_t val_be = sys_cpu_to_be16(val);
|
||||
|
||||
return i2c_burst_write(drv_data->i2c, TMP112_I2C_ADDRESS,
|
||||
reg, (uint8_t *)&val_be, 2);
|
||||
return i2c_burst_write_dt(&cfg->bus, reg, (uint8_t *)&val_be, 2);
|
||||
}
|
||||
|
||||
static int tmp112_reg_update(struct tmp112_data *drv_data, uint8_t reg,
|
||||
static int tmp112_reg_update(const struct tmp112_config *cfg, uint8_t reg,
|
||||
uint16_t mask, uint16_t val)
|
||||
{
|
||||
uint16_t old_val = 0U;
|
||||
uint16_t new_val;
|
||||
|
||||
if (tmp112_reg_read(drv_data, reg, &old_val) < 0) {
|
||||
if (tmp112_reg_read(cfg, reg, &old_val) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
new_val = old_val & ~mask;
|
||||
new_val |= val & mask;
|
||||
|
||||
return tmp112_reg_write(drv_data, reg, new_val);
|
||||
return tmp112_reg_write(cfg, reg, new_val);
|
||||
}
|
||||
|
||||
static int tmp112_attr_set(const struct device *dev,
|
||||
|
@ -78,7 +80,7 @@ static int tmp112_attr_set(const struct device *dev,
|
|||
enum sensor_attribute attr,
|
||||
const struct sensor_value *val)
|
||||
{
|
||||
struct tmp112_data *drv_data = dev->data;
|
||||
const struct tmp112_config *cfg = dev->config;
|
||||
int64_t value;
|
||||
uint16_t cr;
|
||||
|
||||
|
@ -98,7 +100,7 @@ static int tmp112_attr_set(const struct device *dev,
|
|||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
if (tmp112_reg_update(drv_data, TMP112_REG_CONFIG,
|
||||
if (tmp112_reg_update(cfg, TMP112_REG_CONFIG,
|
||||
TMP112_EM_BIT, value) < 0) {
|
||||
LOG_DBG("Failed to set attribute!");
|
||||
return -EIO;
|
||||
|
@ -133,7 +135,7 @@ static int tmp112_attr_set(const struct device *dev,
|
|||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
if (tmp112_reg_update(drv_data, TMP112_REG_CONFIG,
|
||||
if (tmp112_reg_update(cfg, TMP112_REG_CONFIG,
|
||||
TMP112_CR0_BIT | TMP112_CR1_BIT,
|
||||
value) < 0) {
|
||||
LOG_DBG("Failed to set attribute!");
|
||||
|
@ -153,11 +155,12 @@ static int tmp112_sample_fetch(const struct device *dev,
|
|||
enum sensor_channel chan)
|
||||
{
|
||||
struct tmp112_data *drv_data = dev->data;
|
||||
const struct tmp112_config *cfg = dev->config;
|
||||
uint16_t val;
|
||||
|
||||
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
|
||||
|
||||
if (tmp112_reg_read(drv_data, TMP112_REG_TEMPERATURE, &val) < 0) {
|
||||
if (tmp112_reg_read(cfg, TMP112_REG_TEMPERATURE, &val) < 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -196,19 +199,23 @@ static const struct sensor_driver_api tmp112_driver_api = {
|
|||
|
||||
int tmp112_init(const struct device *dev)
|
||||
{
|
||||
struct tmp112_data *drv_data = dev->data;
|
||||
const struct tmp112_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));
|
||||
if (!device_is_ready(cfg->bus.bus)) {
|
||||
LOG_ERR("I2C dev %s not ready", cfg->bus.bus->name);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct tmp112_data tmp112_driver;
|
||||
#define TMP112_INST(inst) \
|
||||
static struct tmp112_data tmp112_data_##inst; \
|
||||
static const struct tmp112_config tmp112_config_##inst = { \
|
||||
.bus = I2C_DT_SPEC_INST_GET(inst) \
|
||||
}; \
|
||||
DEVICE_DT_INST_DEFINE(inst, tmp112_init, NULL, &tmp112_data_##inst, \
|
||||
&tmp112_config_##inst, POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &tmp112_driver_api);
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, tmp112_init, NULL, &tmp112_driver,
|
||||
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &tmp112_driver_api);
|
||||
DT_INST_FOREACH_STATUS_OKAY(TMP112_INST)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue