Skip to content

Commit c3a9fb1

Browse files
Jeppe Odgaardkartben
authored andcommitted
driver: sensor: tmp11x: support get offset
Allow reading the offset register. This allows reading the offset before setting it if offset is set more than once. Signed-off-by: Jeppe Odgaard <[email protected]>
1 parent 8cebd99 commit c3a9fb1

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

drivers/sensor/ti/tmp11x/tmp11x.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ int tmp11x_write_config(const struct device *dev, uint16_t mask, uint16_t conf)
6666
return tmp11x_reg_write(dev, TMP11X_REG_CFGR, config);
6767
}
6868

69+
static inline bool tmp11x_is_offset_supported(const struct tmp11x_data *drv_data)
70+
{
71+
return drv_data->id == TMP117_DEVICE_ID || drv_data->id == TMP119_DEVICE_ID;
72+
}
73+
6974
static bool check_eeprom_bounds(const struct device *dev, off_t offset,
7075
size_t len)
7176
{
@@ -220,24 +225,30 @@ static int tmp11x_sample_fetch(const struct device *dev,
220225
return 0;
221226
}
222227

228+
/*
229+
* See datasheet "Temperature Results and Limits" section for more
230+
* details on processing sample data.
231+
*/
232+
static void tmp11x_temperature_to_sensor_value(int16_t temperature, struct sensor_value *val)
233+
{
234+
int32_t tmp;
235+
236+
tmp = (temperature * (int32_t)TMP11X_RESOLUTION) / 10;
237+
val->val1 = tmp / 1000000; /* uCelsius */
238+
val->val2 = tmp % 1000000;
239+
}
240+
223241
static int tmp11x_channel_get(const struct device *dev,
224242
enum sensor_channel chan,
225243
struct sensor_value *val)
226244
{
227245
struct tmp11x_data *drv_data = dev->data;
228-
int32_t tmp;
229246

230247
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
231248
return -ENOTSUP;
232249
}
233250

234-
/*
235-
* See datasheet "Temperature Results and Limits" section for more
236-
* details on processing sample data.
237-
*/
238-
tmp = ((int16_t)drv_data->sample * (int32_t)TMP11X_RESOLUTION) / 10;
239-
val->val1 = tmp / 1000000; /* uCelsius */
240-
val->val2 = tmp % 1000000;
251+
tmp11x_temperature_to_sensor_value(drv_data->sample, val);
241252

242253
return 0;
243254
}
@@ -292,9 +303,8 @@ static int tmp11x_attr_set(const struct device *dev,
292303
return tmp11x_write_config(dev, TMP11X_CFGR_CONV, value);
293304

294305
case SENSOR_ATTR_OFFSET:
295-
if (drv_data->id != TMP117_DEVICE_ID && drv_data->id != TMP119_DEVICE_ID) {
296-
LOG_ERR("%s: Offset is only supported by TMP117 and TMP119",
297-
dev->name);
306+
if (!tmp11x_is_offset_supported(drv_data)) {
307+
LOG_ERR("%s: Offset is not supported", dev->name);
298308
return -EINVAL;
299309
}
300310
/*
@@ -359,6 +369,19 @@ static int tmp11x_attr_get(const struct device *dev, enum sensor_channel chan,
359369
return rc;
360370
}
361371
break;
372+
case SENSOR_ATTR_OFFSET:
373+
if (!tmp11x_is_offset_supported(dev->data)) {
374+
LOG_ERR("%s: Offset is not supported", dev->name);
375+
return -EINVAL;
376+
}
377+
378+
rc = tmp11x_reg_read(dev, TMP117_REG_TEMP_OFFSET, &data);
379+
if (rc < 0) {
380+
return rc;
381+
}
382+
383+
tmp11x_temperature_to_sensor_value(data, val);
384+
break;
362385
default:
363386
return -ENOTSUP;
364387
}

0 commit comments

Comments
 (0)