drivers: sensor: add ams AS621x Temperature Sensor driver support

Added support for the AMS AS621x series of temperature sensors as a
variant of the TI TMP108 temperature sensor.

Signed-off-by: Jared Baumann <jared.baumann8@t-mobile.com>
Signed-off-by: James Johnson <james.johnson672@t-mobile.com>
This commit is contained in:
James Johnson 2022-07-20 16:09:42 -07:00 committed by Carles Cufí
parent 2da82552f5
commit 0eaff5a11d
8 changed files with 195 additions and 106 deletions

View file

@ -1,15 +1,18 @@
# TMP108 temperature sensor configuration options
# Copyright (c) 2021 Jimmy Johnson <catch22@fastmail.net>
# Copyright (c) 2022 T-Mobile USA, Inc.
# SPDX-License-Identifier: Apache-2.0
menuconfig TMP108
bool "TMP108 Temperature Sensor"
default y
depends on DT_HAS_TI_TMP108_ENABLED
depends on DT_HAS_TI_TMP108_ENABLED || DT_HAS_AMS_AS6212_ENABLED
select I2C
help
Enable driver for the TMP108 temperature sensor.
Enable driver for the TMP108 temperature sensor and/or it's variant
the AMS AS621.
if TMP108

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021 Jimmy Johnson <catch22@fastmail.net>
* Copyright (c) 2022 T-Mobile USA, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -18,17 +19,6 @@
LOG_MODULE_REGISTER(TMP108, CONFIG_SENSOR_LOG_LEVEL);
/** TI conversion scale from 16 bit int temp value to float */
#define TMP108_TEMP_MULTIPLIER 62500
/** TMP typical conversion time of 27 ms after waking from sleep */
#define TMP108_WAKEUP_TIME_IN_MS 30
struct tmp108_config {
const struct i2c_dt_spec i2c_spec;
const struct gpio_dt_spec alert_gpio;
};
int tmp108_reg_read(const struct device *dev, uint8_t reg, uint16_t *val)
{
const struct tmp108_config *cfg = dev->config;
@ -122,8 +112,8 @@ static int tmp108_sample_fetch(const struct device *dev,
if (drv_data->one_shot_mode == true) {
result = tmp108_write_config(dev,
TI_TMP108_MODE_MASK,
TI_TMP108_MODE_ONE_SHOT);
TI_TMP108_MODE_MASK(dev),
TI_TMP108_MODE_ONE_SHOT(dev));
if (result < 0) {
return result;
@ -133,7 +123,7 @@ static int tmp108_sample_fetch(const struct device *dev,
* the typical wakeup time given in the data sheet is 27
*/
result = k_work_schedule(&drv_data->scheduled_work,
K_MSEC(TMP108_WAKEUP_TIME_IN_MS));
K_MSEC(TMP108_WAKEUP_TIME_IN_MS(dev)));
if (result < 0) {
return result;
@ -162,7 +152,7 @@ static int tmp108_channel_get(const struct device *dev,
return -ENOTSUP;
}
uval = (int32_t)(drv_data->sample >> 4U) * TMP108_TEMP_MULTIPLIER;
uval = ((int32_t)(drv_data->sample) * TMP108_TEMP_MULTIPLIER(dev)) >> 4U;
val->val1 = uval / 1000000U;
val->val2 = uval % 1000000U;
@ -202,6 +192,7 @@ static int tmp108_attr_set(const struct device *dev,
uint16_t mode = 0;
uint16_t reg_value = 0;
int result = 0;
int32_t uval;
if (chan != SENSOR_CHAN_AMBIENT_TEMP && chan != SENSOR_CHAN_ALL) {
return -ENOTSUP;
@ -209,43 +200,49 @@ static int tmp108_attr_set(const struct device *dev,
switch ((int) attr) {
case SENSOR_ATTR_HYSTERESIS:
if (TI_TMP108_HYSTER_0_C(dev) == TI_TMP108_CONF_NA) {
LOG_WRN("AS621x Series lacks Hysterisis setttings");
return -ENOTSUP;
}
if (val->val1 < 1) {
mode = TI_TMP108_HYSTER_0_C;
mode = TI_TMP108_HYSTER_0_C(dev);
} else if (val->val1 < 2) {
mode = TI_TMP108_HYSTER_1_C;
mode = TI_TMP108_HYSTER_1_C(dev);
} else if (val->val1 < 4) {
mode = TI_TMP108_HYSTER_2_C;
mode = TI_TMP108_HYSTER_2_C(dev);
} else {
mode = TI_TMP108_HYSTER_4_C;
mode = TI_TMP108_HYSTER_4_C(dev);
}
result = tmp108_write_config(dev,
TI_TMP108_HYSTER_MASK,
TI_TMP108_HYSTER_MASK(dev),
mode);
break;
case SENSOR_ATTR_ALERT:
/* Spec Sheet Errata: TM is set on reset not cleared */
if (val->val1 == 1) {
mode = TI_TMP108_CONF_TM_INT;
mode = TI_TMP108_CONF_TM_INT(dev);
} else {
mode = TI_TMP108_CONF_TM_CMP;
mode = TI_TMP108_CONF_TM_CMP(dev);
}
result = tmp108_write_config(dev,
TI_TMP108_CONF_TM_MASK,
TI_TMP108_CONF_TM_MASK(dev),
mode);
break;
case SENSOR_ATTR_LOWER_THRESH:
reg_value = (val->val1 << 8) | (0x00FF & val->val2);
uval = val->val1 * 1000000 + val->val2;
reg_value = (uval << 4U) / TMP108_TEMP_MULTIPLIER(dev);
result = tmp108_reg_write(dev,
TI_TMP108_REG_LOW_LIMIT,
reg_value);
break;
case SENSOR_ATTR_UPPER_THRESH:
reg_value = (val->val1 << 8) | (0x00FF & val->val2);
uval = val->val1 * 1000000 + val->val2;
reg_value = (uval << 4U) / TMP108_TEMP_MULTIPLIER(dev);
result = tmp108_reg_write(dev,
TI_TMP108_REG_HIGH_LIMIT,
reg_value);
@ -253,48 +250,48 @@ static int tmp108_attr_set(const struct device *dev,
case SENSOR_ATTR_SAMPLING_FREQUENCY:
if (val->val1 < 1) {
mode = TI_TMP108_FREQ_4_SECS;
mode = TI_TMP108_FREQ_4_SECS(dev);
} else if (val->val1 < 4) {
mode = TI_TMP108_FREQ_1_HZ;
mode = TI_TMP108_FREQ_1_HZ(dev);
} else if (val->val1 < 16) {
mode = TI_TMP108_FREQ_4_HZ;
mode = TI_TMP108_FREQ_4_HZ(dev);
} else {
mode = TI_TMP108_FREQ_16_HZ;
mode = TI_TMP108_FREQ_16_HZ(dev);
}
result = tmp108_write_config(dev,
TI_TMP108_FREQ_MASK,
TI_TMP108_FREQ_MASK(dev),
mode);
break;
case SENSOR_ATTR_TMP108_SHUTDOWN_MODE:
result = tmp108_write_config(dev,
TI_TMP108_MODE_MASK,
TI_TMP108_MODE_SHUTDOWN);
TI_TMP108_MODE_MASK(dev),
TI_TMP108_MODE_SHUTDOWN(dev));
drv_data->one_shot_mode = false;
break;
case SENSOR_ATTR_TMP108_CONTINUOUS_CONVERSION_MODE:
result = tmp108_write_config(dev,
TI_TMP108_MODE_MASK,
TI_TMP108_MODE_CONTINUOUS);
TI_TMP108_MODE_MASK(dev),
TI_TMP108_MODE_CONTINUOUS(dev));
drv_data->one_shot_mode = false;
break;
case SENSOR_ATTR_TMP108_ONE_SHOT_MODE:
result = tmp108_write_config(dev,
TI_TMP108_MODE_MASK,
TI_TMP108_MODE_ONE_SHOT);
TI_TMP108_MODE_MASK(dev),
TI_TMP108_MODE_ONE_SHOT(dev));
drv_data->one_shot_mode = true;
break;
case SENSOR_ATTR_TMP108_ALERT_POLARITY:
if (val->val1 == 1) {
mode = TI_TMP108_CONF_POL_HIGH;
mode = TI_TMP108_CONF_POL_HIGH(dev);
} else {
mode = TI_TMP108_CONF_POL_LOW;
mode = TI_TMP108_CONF_POL_LOW(dev);
}
result = tmp108_write_config(dev,
TI_TMP108_CONF_POL_MASK,
TI_TMP108_CONF_POL_MASK(dev),
mode);
break;
@ -377,25 +374,41 @@ static int tmp108_init(const struct device *dev)
#ifdef CONFIG_TMP108_ALERT_INTERRUPTS
result = setup_interrupts(dev);
#endif
if (result < 0) {
return result;
}
#endif
/* clear and set configuration registers back to default values */
result = tmp108_write_config(dev,
0x0000,
TMP108_CONF_RST(dev));
return result;
}
#define TMP108_DEFINE(inst) \
static struct tmp108_data tmp108_prv_data_##inst; \
static const struct tmp108_config tmp108_config_##inst = { \
#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 }) \
alert_gpios, { 0 }),\
.reg_def = t##_CONF \
}; \
DEVICE_DT_INST_DEFINE(inst, \
&tmp108_init, \
NULL, \
&tmp108_prv_data_##inst, \
&tmp108_config_##inst, \
&tmp108_prv_data_##inst##t, \
&tmp108_config_##inst##t, \
POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&tmp108_driver_api);
DT_INST_FOREACH_STATUS_OKAY(TMP108_DEFINE)
#define TMP108_INIT(n) TMP108_DEFINE(n, TI_TMP108)
#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT ti_tmp108
DT_INST_FOREACH_STATUS_OKAY(TMP108_INIT)
#define AS6212_INIT(n) TMP108_DEFINE(n, AMS_AS6212)
#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT ams_as6212
DT_INST_FOREACH_STATUS_OKAY(AS6212_INIT)

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021 Jimmy Johnson <catch22@fastmail.net>
* Copyright (c) 2022 T-Mobile USA, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -9,6 +10,7 @@
#include <stdint.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/sensor/tmp108.h>
@ -18,42 +20,89 @@
#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_M1 = 0x0000, \
.CONF_TM = 0x0200, \
.CONF_POL = 0x0400, \
.CONF_M0 = 0x8000, \
.CONF_RST = 0x0080, \
.TEMP_MULT = 125000, \
.WAKEUP_TIME_IN_MS = 120 }
#define TI_TMP108_CONF_M0 0x0100 /** Mode 1 configuration bit */
#define TI_TMP108_CONF_M1 0x0200 /** Mode 2 configuration bit */
#define TI_TMP108_CONF_CR0 0x2000 /** Conversion rate 1 configuration bit */
#define TI_TMP108_CONF_CR1 0x4000 /** Conversion rate 2 configuration bit */
#define TI_TMP108_CONF_POL 0x0080 /** Alert pin Polarity configuration bit */
#define TI_TMP108_CONF_TM 0x0400 /** Thermostat mode setting bit */
#define TI_TMP108_CONF_HYS1 0x0020 /** Temperature hysteresis config 1 bit */
#define TI_TMP108_CONF_HYS0 0x0010 /** Temperature hysteresis config 2 bit */
#define TI_TMP108_CONF_WFH OVER_TEMP_MASK
#define TI_TMP108_CONF_WFL UNDER_TEMP_MASK
#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 = 62500, \
.WAKEUP_TIME_IN_MS = 30 }
#define TI_TMP108_MODE_SHUTDOWN 0
#define TI_TMP108_MODE_ONE_SHOT TI_TMP108_CONF_M0
#define TI_TMP108_MODE_CONTINUOUS TI_TMP108_CONF_M1
#define TI_TMP108_MODE_MASK ~(TI_TMP108_CONF_M1 | TI_TMP108_CONF_M1)
#define TI_TMP108_MODE_SHUTDOWN(x) 0
#define TI_TMP108_MODE_ONE_SHOT(x) TI_TMP108_CONF_M0(x)
#define TI_TMP108_MODE_CONTINUOUS(x) TI_TMP108_CONF_M1(x)
#define TI_TMP108_MODE_MASK(x) ~(TI_TMP108_CONF_M0(x) | TI_TMP108_CONF_M1(x))
#define TI_TMP108_FREQ_4_SECS 0
#define TI_TMP108_FREQ_1_HZ TI_TMP108_CONF_CR0
#define TI_TMP108_FREQ_4_HZ TI_TMP108_CONF_CR1
#define TI_TMP108_FREQ_16_HZ (TI_TMP108_CONF_CR1 | TI_TMP108_CONF_CR0)
#define TI_TMP108_FREQ_MASK ~(TI_TMP108_CONF_M1 | TI_TMP108_CONF_M1)
#define TI_TMP108_FREQ_4_SECS(x) 0
#define TI_TMP108_FREQ_1_HZ(x) TI_TMP108_GET_CONF(x, CONF_CR0)
#define TI_TMP108_FREQ_4_HZ(x) TI_TMP108_GET_CONF(x, CONF_CR1)
#define TI_TMP108_FREQ_16_HZ(x) (TI_TMP108_GET_CONF(x, CONF_CR1) | \
TI_TMP108_GET_CONF(x, CONF_CR0))
#define TI_TMP108_FREQ_MASK(x) ~(TI_TMP108_GET_CONF(x, CONF_CR1) | \
TI_TMP108_GET_CONF(x, CONF_CR0))
#define TI_TMP108_CONF_POL_LOW 0
#define TI_TMP108_CONF_POL_HIGH TI_TMP108_CONF_POL
#define TI_TMP108_CONF_POL_MASK ~(TI_TMP108_CONF_POL)
#define TI_TMP108_CONF_POL_LOW(x) 0
#define TI_TMP108_CONF_POL_HIGH(x) TI_TMP108_GET_CONF(x, CONF_POL)
#define TI_TMP108_CONF_POL_MASK(x) ~(TI_TMP108_GET_CONF(x, CONF_POL))
#define TI_TMP108_CONF_TM_CMP 0
#define TI_TMP108_CONF_TM_INT TI_TMP108_CONF_TM
#define TI_TMP108_CONF_TM_MASK ~(TI_TMP108_CONF_TM)
#define TI_TMP108_CONF_TM_CMP(x) 0
#define TI_TMP108_CONF_TM_INT(x) TI_TMP108_GET_CONF(x, CONF_TM)
#define TI_TMP108_CONF_TM_MASK(x) ~(TI_TMP108_GET_CONF(x, CONF_TM))
#define TI_TMP108_HYSTER_0_C 0
#define TI_TMP108_HYSTER_1_C TI_TMP108_CONF_HYS0
#define TI_TMP108_HYSTER_2_C TI_TMP108_CONF_HYS1
#define TI_TMP108_HYSTER_4_C (TI_TMP108_CONF_HYS1 | TI_TMP108_CONF_HYS0)
#define TI_TMP108_HYSTER_MASK ~(TI_TMP108_CONF_HYS1 | TI_TMP108_CONF_HYS0)
#define TI_TMP108_HYSTER_0_C(x) 0
#define TI_TMP108_HYSTER_1_C(x) TI_TMP108_GET_CONF(x, CONF_HYS0)
#define TI_TMP108_HYSTER_2_C(x) TI_TMP108_GET_CONF(x, CONF_HYS1)
#define TI_TMP108_HYSTER_4_C(x) (TI_TMP108_GET_CONF(x, CONF_HYS1) | \
TI_TMP108_GET_CONF(x, CONF_HYS0))
#define TI_TMP108_HYSTER_MASK(x) ~(TI_TMP108_GET_CONF(x, CONF_HYS1) | \
TI_TMP108_GET_CONF(x, CONF_HYS0))
#define TI_TMP108_CONF_M1(x) TI_TMP108_GET_CONF(x, CONF_M1)
#define TI_TMP108_CONF_M0(x) TI_TMP108_GET_CONF(x, CONF_M0)
#define TMP108_TEMP_MULTIPLIER(x) TI_TMP108_GET_CONF(x, TEMP_MULT)
#define TMP108_WAKEUP_TIME_IN_MS(x) TI_TMP108_GET_CONF(x, WAKEUP_TIME_IN_MS)
#define TMP108_CONF_RST(x) TI_TMP108_GET_CONF(x, CONF_RST)
#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_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 */
uint32_t TEMP_MULT; /** Temperature multiplier */
uint16_t WAKEUP_TIME_IN_MS; /** Wake up and conversion time from one shot */
uint16_t CONF_RST; /** default reset values on init */
};
#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;
};
struct tmp108_data {
const struct device *tmp108_dev;

View file

@ -35,16 +35,16 @@ void tmp108_trigger_handle_one_shot(struct k_work *work)
tmp108_reg_read(drv_data->tmp108_dev, TI_TMP108_REG_CONF, &config);
/* check shutdown mode which indicates a one shot read was successful */
shutdown_mode = (config & (TI_TMP108_CONF_M1 | TI_TMP108_CONF_M0)) == 0;
shutdown_mode = (config & (TI_TMP108_CONF_M1(drv_data->tmp108_dev) |
TI_TMP108_CONF_M0(drv_data->tmp108_dev))) == 0;
if (shutdown_mode == true) {
ti_tmp108_read_temp(drv_data->tmp108_dev);
} else {
LOG_ERR("Temperature one shot mode read failed, retrying");
/* Typical wake up time is 27 ms, retry if the read fails
* assuming the chip should wake up and take a reading by the time
* 27 ms for the initial wake up time and call of this thread
* plus 10 ms time has passed
/* Wait for typical wake up time, retry if the read fails
* assuming the chip should wake up and take a reading after the typical
* wake up time and call of this thread plus 10 ms time has passed
*/
k_work_reschedule(&drv_data->scheduled_work,
K_MSEC(TMP108_ONE_SHOT_RETRY_TIME_IN_MS));

View file

@ -0,0 +1,18 @@
# Copyright (c) 2022 T-Mobile USA, Inc.
# SPDX-License-Identifier: Apache-2.0
description: |
AMS AS6212 Digital Temperature Sensor. See more info at
https://ams.com/en/as621x
compatible: "ams,as6212"
include: i2c-device.yaml
properties:
alert-gpios:
type: phandle-array
required: false
description: |
Identifies the ALERT signal, which is active-low open drain when
produced by the sensor.

View file

@ -1,4 +1,5 @@
# Copyright (c) 2021 Jimmy Johnson <catch22@fastmail.net>
# Copyright (c) 2022 T-Mobile USA, Inc.
# SPDX-License-Identifier: Apache-2.0
description: |

View file

@ -30,11 +30,14 @@ enum sensor_attribute_tmp_108 {
SENSOR_ATTR_TMP108_ALERT_POLARITY
};
/** a mask for the over temp alert bit in the status word*/
#define OVER_TEMP_MASK 0x1000U
/** a tmp108 mask for the over temp alert bit in the status word*/
#define TMP108_OVER_TEMP_MASK 0x1000U
/** a mask for the under temp alert bit in the status word*/
#define UNDER_TEMP_MASK 0x0800U
/** a tmp108 mask for the under temp alert bit in the status word*/
#define TMP108_UNDER_TEMP_MASK 0x0800U
/** a as6212 mask for the over temp alert bit in the status word*/
#define A6212_ALERT_TEMP_MASK 0x0020U
#ifdef __cplusplus
}

View file

@ -41,13 +41,9 @@ void temperature_alert(const struct device *dev,
SENSOR_ATTR_CONFIGURATION,
&temp_flags);
if (OVER_TEMP_MASK & temp_flags.val1) {
printf("High temperature alert!\n");
} else if (UNDER_TEMP_MASK & temp_flags.val1) {
printf("Low temperature alert!\n");
} else {
printf("Temperature alert cleared!\n");
}
/* use a mask to pull your specific chip set bits out */
printf("Temperature alert config register = %x!\n", temp_flags.val1);
}
void enable_temp_alerts(const struct device *tmp108)
@ -134,39 +130,45 @@ void get_temperature_continuous(const struct device *tmp108)
void main(void)
{
const struct device *tmp108;
const struct device *temp_sensor;
int result;
printf("TI TMP108 Example, %s\n", CONFIG_ARCH);
tmp108 = DEVICE_DT_GET_ANY(ti_tmp108);
temp_sensor = DEVICE_DT_GET_ANY(ti_tmp108);
if (!tmp108) {
printf("error: no tmp108 device found\n");
return;
if (!temp_sensor) {
printf("warning: tmp108 device not found checking for compatible ams device\n");
temp_sensor = DEVICE_DT_GET_ANY(ams_as6212);
if (!temp_sensor) {
printf("error: tmp108 compatible devices not found\n");
return;
}
}
if (!device_is_ready(tmp108)) {
if (!device_is_ready(temp_sensor)) {
printf("error: tmp108 device not ready\n");
return;
}
sensor_attr_set(tmp108,
sensor_attr_set(temp_sensor,
SENSOR_CHAN_AMBIENT_TEMP,
SENSOR_ATTR_TMP108_CONTINUOUS_CONVERSION_MODE,
NULL);
#if CONFIG_APP_ENABLE_ONE_SHOT
enable_one_shot(tmp108);
enable_one_shot(temp_sensor);
#endif
#if CONFIG_APP_REPORT_TEMP_ALERTS
enable_temp_alerts(tmp108);
enable_temp_alerts(temp_sensor);
#endif
while (1) {
result = sensor_sample_fetch(tmp108);
result = sensor_sample_fetch(temp_sensor);
if (result) {
printf("error: sensor_sample_fetch failed: %d\n", result);
@ -174,7 +176,7 @@ void main(void)
}
#if !CONFIG_APP_ENABLE_ONE_SHOT
get_temperature_continuous(tmp108);
get_temperature_continuous(temp_sensor);
#endif
k_sleep(K_MSEC(3000));
}