Skip to content

Commit

Permalink
sensor: tmp108: optimize resource usage
Browse files Browse the repository at this point in the history
Optimize the resource usage of the driver for the case where
`CONFIG_TMP108_ALERT_INTERRUPTS=n`.

Signed-off-by: Jordan Yates <jordan@embeint.com>
  • Loading branch information
JordanYates authored and kartben committed Jan 15, 2025
1 parent 093f66f commit d36334a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 72 deletions.
3 changes: 2 additions & 1 deletion drivers/sensor/ti/tmp108/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

zephyr_library()

zephyr_library_sources(tmp108.c tmp108_trigger.c)
zephyr_library_sources(tmp108.c)
zephyr_library_sources_ifdef(CONFIG_TMP108_ALERT_INTERRUPTS tmp108_trigger.c)
63 changes: 31 additions & 32 deletions drivers/sensor/ti/tmp108/tmp108.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,17 @@ static int tmp108_attr_set(const struct device *dev,
const struct sensor_value *val)
{
struct tmp108_data *drv_data = dev->data;
uint16_t mode = 0;
uint16_t reg_value = 0;
__maybe_unused uint16_t reg_value;
__maybe_unused int32_t uval;
uint16_t mode;
int result = 0;
int32_t uval;

if (chan != SENSOR_CHAN_AMBIENT_TEMP && chan != SENSOR_CHAN_ALL) {
return -ENOTSUP;
}

switch ((int) attr) {
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
case SENSOR_ATTR_HYSTERESIS:
if (TI_TMP108_HYSTER_0_C(dev) == TI_TMP108_CONF_NA) {
LOG_WRN("AS621x Series lacks Hysterisis setttings");
Expand Down Expand Up @@ -260,6 +261,18 @@ static int tmp108_attr_set(const struct device *dev,
reg_value);
break;

case SENSOR_ATTR_TMP108_ALERT_POLARITY:
if (val->val1 == 1) {
mode = TI_TMP108_CONF_POL_HIGH(dev);
} else {
mode = TI_TMP108_CONF_POL_LOW(dev);
}
result = tmp108_write_config(dev,
TI_TMP108_CONF_POL_MASK(dev),
mode);
break;
#endif /* CONFIG_TMP108_ALERT_INTERRUPTS */

case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->val1 < 1) {
mode = TI_TMP108_FREQ_4_SECS(dev);
Expand Down Expand Up @@ -296,17 +309,6 @@ static int tmp108_attr_set(const struct device *dev,
drv_data->one_shot_mode = true;
break;

case SENSOR_ATTR_TMP108_ALERT_POLARITY:
if (val->val1 == 1) {
mode = TI_TMP108_CONF_POL_HIGH(dev);
} else {
mode = TI_TMP108_CONF_POL_LOW(dev);
}
result = tmp108_write_config(dev,
TI_TMP108_CONF_POL_MASK(dev),
mode);
break;

default:
return -ENOTSUP;
}
Expand All @@ -323,7 +325,9 @@ static DEVICE_API(sensor, tmp108_driver_api) = {
.attr_get = tmp108_attr_get,
.sample_fetch = tmp108_sample_fetch,
.channel_get = tmp108_channel_get,
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
.trigger_set = tmp_108_trigger_set,
#endif
};

#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
Expand Down Expand Up @@ -371,18 +375,19 @@ static int setup_interrupts(const struct device *dev)
static int tmp108_init(const struct device *dev)
{
const struct tmp108_config *cfg = dev->config;
struct tmp108_data *drv_data = dev->data;
int result = 0;

if (!device_is_ready(cfg->i2c_spec.bus)) {
LOG_ERR("I2C dev %s not ready", cfg->i2c_spec.bus->name);
return -ENODEV;
}

#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
struct tmp108_data *drv_data = dev->data;

/* save this driver instance for passing to other functions */
drv_data->tmp108_dev = dev;

#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
result = setup_interrupts(dev);

if (result < 0) {
Expand All @@ -396,22 +401,16 @@ static int tmp108_init(const struct device *dev)
return result;
}

#define TMP108_DEFINE(inst, t) \
static struct tmp108_data tmp108_prv_data_##inst##t; \
static const struct tmp108_config tmp108_config_##inst##t = { \
.i2c_spec = I2C_DT_SPEC_INST_GET(inst), \
.alert_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, \
alert_gpios, { 0 }),\
.reg_def = t##_CONF \
}; \
SENSOR_DEVICE_DT_INST_DEFINE(inst, \
&tmp108_init, \
NULL, \
&tmp108_prv_data_##inst##t, \
&tmp108_config_##inst##t, \
POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&tmp108_driver_api);
#define TMP108_DEFINE(inst, t) \
static struct tmp108_data tmp108_prv_data_##inst##t; \
static const struct tmp108_config tmp108_config_##inst##t = { \
.i2c_spec = I2C_DT_SPEC_INST_GET(inst), \
IF_ENABLED(CONFIG_TMP108_ALERT_INTERRUPTS, \
(.alert_gpio = GPIO_DT_SPEC_INST_GET(inst, alert_gpios),)) \
.reg_def = t##_CONF}; \
SENSOR_DEVICE_DT_INST_DEFINE(inst, &tmp108_init, NULL, &tmp108_prv_data_##inst##t, \
&tmp108_config_##inst##t, POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, &tmp108_driver_api);

#define TMP108_INIT(n) TMP108_DEFINE(n, TI_TMP108)
#undef DT_DRV_COMPAT
Expand Down
83 changes: 44 additions & 39 deletions drivers/sensor/ti/tmp108/tmp108.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,29 @@
#define TI_TMP108_REG_LOW_LIMIT 0x02 /** Low alert set register */
#define TI_TMP108_REG_HIGH_LIMIT 0x03 /** High alert set register */

#define AMS_AS6212_CONF {.CONF_HYS1 = TI_TMP108_CONF_NA,\
.CONF_HYS0 = TI_TMP108_CONF_NA,\
.CONF_CR0 = 0x0040, \
.CONF_CR1 = 0x0080, \
.CONF_SLEEP = 0x0100, \
.CONF_M1 = 0x0000, \
.CONF_TM = 0x0200, \
.CONF_POL = 0x0400, \
.CONF_M0 = 0x8000, \
.CONF_RST = 0x0080, \
.TEMP_MULT = 15625, \
.TEMP_DIV = 2 }

#define TI_TMP108_CONF {.CONF_HYS0 = 0x0010, \
.CONF_HYS1 = 0x0020, \
.CONF_POL = 0x0080, \
.CONF_M0 = 0x0100, \
.CONF_M1 = 0x0200, \
.CONF_TM = 0x0400, \
.CONF_CR0 = 0x2000, \
.CONF_CR1 = 0x4000, \
.CONF_RST = 0x0022, \
.TEMP_MULT = 15625, \
.TEMP_DIV = 4 }
#define AMS_AS6212_CONF \
{.CONF_CR0 = 0x0040, \
.CONF_CR1 = 0x0080, \
.CONF_SLEEP = 0x0100, \
.CONF_M1 = 0x0000, \
.CONF_TM = 0x0200, \
.CONF_M0 = 0x8000, \
.CONF_RST = 0x0080, \
.TEMP_MULT = 15625, \
.TEMP_DIV = 2, \
IF_ENABLED(CONFIG_TMP108_ALERT_INTERRUPTS, (.CONF_POL = 0x0400))}

#define TI_TMP108_CONF \
{.CONF_M0 = 0x0100, \
.CONF_M1 = 0x0200, \
.CONF_TM = 0x0400, \
.CONF_CR0 = 0x2000, \
.CONF_CR1 = 0x4000, \
.CONF_RST = 0x0022, \
.TEMP_MULT = 15625, \
.TEMP_DIV = 4, \
IF_ENABLED(CONFIG_TMP108_ALERT_INTERRUPTS, \
(.CONF_HYS0 = 0x0010, .CONF_HYS1 = 0x0020, .CONF_POL = 0x0080))}

#define TI_TMP108_MODE_SHUTDOWN(x) 0
#define TI_TMP108_MODE_ONE_SHOT(x) (TI_TMP108_CONF_M0(x) | TI_TMP108_CONF_SLEEP(x))
Expand Down Expand Up @@ -87,39 +86,45 @@
#define TI_TMP108_CONF_NA 0x0000

struct tmp_108_reg_def {
uint16_t CONF_M0; /** Mode 1 configuration bit */
uint16_t CONF_M1; /** Mode 2 configuration bit */
uint16_t CONF_SLEEP; /** Sleep mode configuration bit */
uint16_t CONF_CR0; /** Conversion rate 1 configuration bit */
uint16_t CONF_CR1; /** Conversion rate 2 configuration bit */
uint16_t CONF_POL; /** Alert pin Polarity configuration bit */
uint16_t CONF_TM; /** Thermostat mode setting bit */
uint16_t CONF_HYS1; /** Temperature hysteresis config 1 bit */
uint16_t CONF_HYS0; /** Temperature hysteresis config 2 bit */
int32_t TEMP_MULT; /** Temperature multiplier */
int32_t TEMP_DIV; /** Temperature divisor */
uint16_t CONF_RST; /** default reset values on init */
uint16_t CONF_M0; /** Mode 1 configuration bit */
uint16_t CONF_M1; /** Mode 2 configuration bit */
uint16_t CONF_SLEEP; /** Sleep mode configuration bit */
uint16_t CONF_CR0; /** Conversion rate 1 configuration bit */
uint16_t CONF_CR1; /** Conversion rate 2 configuration bit */
uint16_t CONF_TM; /** Thermostat mode setting bit */
int32_t TEMP_MULT; /** Temperature multiplier */
int32_t TEMP_DIV; /** Temperature divisor */
uint16_t CONF_RST; /** default reset values on init */
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
uint16_t CONF_POL; /** Alert pin Polarity configuration bit */
uint16_t CONF_HYS1; /** Temperature hysteresis config 1 bit */
uint16_t CONF_HYS0; /** Temperature hysteresis config 2 bit */
#endif
};

#define TI_TMP108_GET_CONF(x, cfg) ((struct tmp108_config *)(x->config))->reg_def.cfg

struct tmp108_config {
const struct i2c_dt_spec i2c_spec;
const struct gpio_dt_spec alert_gpio;
struct tmp_108_reg_def reg_def;
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
const struct gpio_dt_spec alert_gpio;
#endif /* CONFIG_TMP108_ALERT_INTERRUPTS */
};

struct tmp108_data {
const struct device *tmp108_dev;

int16_t sample;

bool one_shot_mode;

#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
const struct device *tmp108_dev;

const struct sensor_trigger *temp_alert_trigger;
sensor_trigger_handler_t temp_alert_handler;

struct gpio_callback temp_alert_gpio_cb;
#endif /* CONFIG_TMP108_ALERT_INTERRUPTS */
};

int tmp_108_trigger_set(const struct device *dev,
Expand Down

0 comments on commit d36334a

Please sign in to comment.