Skip to content

Commit 3aebc9d

Browse files
xodus7kartben
authored andcommitted
drivers: sensor: tmp112: add support for setting ALERT thresholds
Allows setting tLow/tHigh threasholds via the SENSOR_ATTR_LOWER_THRESH/ SENSOR_ATTR_UPPER_THRESH attributes. Signed-off-by: Corey Wharton <xodus7@cwharton.com>
1 parent 7425070 commit 3aebc9d

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

drivers/sensor/ti/tmp112/tmp112.c

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ static int tmp112_update_config(const struct device *dev, uint16_t mask, uint16_
5858
return rc;
5959
}
6060

61+
static int tmp112_set_threshold(const struct device *dev, uint8_t reg, int64_t micro_c)
62+
{
63+
struct tmp112_data *drv_data = dev->data;
64+
int64_t v;
65+
uint16_t reg_value;
66+
67+
v = DIV_ROUND_CLOSEST(micro_c, TMP112_TEMP_SCALE);
68+
69+
if (drv_data->config_reg & TMP112_CONFIG_EM) {
70+
if (!IN_RANGE(v, TMP112_TEMP_MIN_EM, TMP112_TEMP_MAX_EM)) {
71+
return -EINVAL;
72+
}
73+
reg_value = (uint16_t)v << TMP112_DATA_EXTENDED_SHIFT;
74+
} else {
75+
if (!IN_RANGE(v, TMP112_TEMP_MIN, TMP112_TEMP_MAX)) {
76+
return -EINVAL;
77+
}
78+
reg_value = (uint16_t)v << TMP112_DATA_NORMAL_SHIFT;
79+
}
80+
81+
return tmp112_reg_write(dev->config, reg, reg_value);
82+
}
83+
6184
static int tmp112_attr_set(const struct device *dev, enum sensor_channel chan,
6285
enum sensor_attribute attr, const struct sensor_value *val)
6386
{
@@ -87,8 +110,9 @@ static int tmp112_attr_set(const struct device *dev, enum sensor_channel chan,
87110
}
88111
break;
89112
#endif
90-
case SENSOR_ATTR_SAMPLING_FREQUENCY:
113+
91114
#if CONFIG_TMP112_SAMPLING_FREQUENCY_RUNTIME
115+
case SENSOR_ATTR_SAMPLING_FREQUENCY:
92116
/* conversion rate in mHz */
93117
cr = val->val1 * 1000 + val->val2 / 1000;
94118

@@ -123,6 +147,12 @@ static int tmp112_attr_set(const struct device *dev, enum sensor_channel chan,
123147
break;
124148
#endif
125149

150+
case SENSOR_ATTR_LOWER_THRESH:
151+
return tmp112_set_threshold(dev, TMP112_REG_TLOW, sensor_value_to_micro(val));
152+
153+
case SENSOR_ATTR_UPPER_THRESH:
154+
return tmp112_set_threshold(dev, TMP112_REG_THIGH, sensor_value_to_micro(val));
155+
126156
default:
127157
return -ENOTSUP;
128158
}
@@ -155,17 +185,12 @@ static int tmp112_channel_get(const struct device *dev, enum sensor_channel chan
155185
struct sensor_value *val)
156186
{
157187
struct tmp112_data *drv_data = dev->data;
158-
int32_t uval;
159188

160189
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
161190
return -ENOTSUP;
162191
}
163192

164-
uval = (int32_t)drv_data->sample * TMP112_TEMP_SCALE;
165-
val->val1 = uval / 1000000;
166-
val->val2 = uval % 1000000;
167-
168-
return 0;
193+
return sensor_value_from_micro(val, (int32_t)drv_data->sample * TMP112_TEMP_SCALE);
169194
}
170195

171196
static DEVICE_API(sensor, tmp112_driver_api) = {
@@ -178,6 +203,7 @@ int tmp112_init(const struct device *dev)
178203
{
179204
const struct tmp112_config *cfg = dev->config;
180205
struct tmp112_data *data = dev->data;
206+
int ret;
181207

182208
if (!device_is_ready(cfg->bus.bus)) {
183209
LOG_ERR("I2C dev %s not ready", cfg->bus.bus->name);
@@ -187,14 +213,34 @@ int tmp112_init(const struct device *dev)
187213
data->config_reg = TMP112_CONV_RATE(cfg->cr) | TMP112_CONV_RES_MASK |
188214
(cfg->extended_mode ? TMP112_CONFIG_EM : 0);
189215

190-
return tmp112_update_config(dev, 0, 0);
216+
ret = tmp112_update_config(dev, 0, 0);
217+
if (ret) {
218+
LOG_ERR("Failed to set configuration (%d)", ret);
219+
return ret;
220+
}
221+
222+
ret = tmp112_set_threshold(dev, TMP112_REG_TLOW, cfg->t_low_micro_c);
223+
if (ret) {
224+
LOG_ERR("Failed to set tLow threshold (%d)", ret);
225+
return ret;
226+
}
227+
228+
ret = tmp112_set_threshold(dev, TMP112_REG_THIGH, cfg->t_high_micro_c);
229+
if (ret) {
230+
LOG_ERR("Failed to set tHigh threshold (%d)", ret);
231+
return ret;
232+
}
233+
234+
return 0;
191235
}
192236

193237
#define TMP112_INST(inst) \
194238
static struct tmp112_data tmp112_data_##inst; \
195239
static const struct tmp112_config tmp112_config_##inst = { \
196240
.bus = I2C_DT_SPEC_INST_GET(inst), \
197241
.cr = DT_INST_ENUM_IDX(inst, conversion_rate), \
242+
.t_low_micro_c = DT_INST_PROP(inst, t_low_micro_c), \
243+
.t_high_micro_c = DT_INST_PROP(inst, t_high_micro_c), \
198244
.extended_mode = DT_INST_PROP(inst, extended_mode), \
199245
}; \
200246
\

drivers/sensor/ti/tmp112/tmp112.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
/* scale in micro degrees Celsius */
4242
#define TMP112_TEMP_SCALE 62500
4343

44+
/* RAW min/max temperature values - LSB=TMP112_TEMP_SCALE */
45+
/* 128C */
46+
#define TMP112_TEMP_MAX 2047
47+
/* -55C */
48+
#define TMP112_TEMP_MIN -880
49+
/* 150C */
50+
#define TMP112_TEMP_MAX_EM 2400
51+
/* -55C */
52+
#define TMP112_TEMP_MIN_EM TMP112_TEMP_MIN
53+
4454
struct tmp112_data {
4555
int16_t sample;
4656
uint16_t config_reg;
@@ -49,6 +59,8 @@ struct tmp112_data {
4959
struct tmp112_config {
5060
const struct i2c_dt_spec bus;
5161
uint8_t cr;
62+
int32_t t_low_micro_c;
63+
int32_t t_high_micro_c;
5264
bool extended_mode : 1;
5365
};
5466

dts/bindings/sensor/ti,tmp112.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ properties:
1919
- 1000
2020
- 4000
2121
- 8000
22+
2223
extended-mode:
23-
description: When true use 13-bit data format allowing measuring temperature up to 128°C
24+
description: When true use 13-bit data format allowing measuring temperature up to 150°C
2425
type: boolean
26+
27+
t-low-micro-c:
28+
description: |
29+
Sets the default tLow threshold in micro-Celsius. The default value is the chip reset
30+
value.
31+
type: int
32+
default: 75000000
33+
34+
t-high-micro-c:
35+
description: |
36+
Sets the default tHigh threshold in micro-Celsius. The default value is the chip reset
37+
value.
38+
type: int
39+
default: 85000000

0 commit comments

Comments
 (0)