drivers: sensor: add NXP TEMPMON driver

Add driver for the NXP TEMPMON to retrieve on-die operational
temperature.

Signed-off-by: Benjamin Lemouzy <blemouzy@centralp.fr>
This commit is contained in:
Benjamin Lemouzy 2023-08-03 14:18:10 +02:00 committed by Carles Cufí
commit d2e420029b
9 changed files with 157 additions and 0 deletions

View file

@ -96,6 +96,7 @@ add_subdirectory_ifdef(CONFIG_MS5607 ms5607)
add_subdirectory_ifdef(CONFIG_MS5837 ms5837)
add_subdirectory_ifdef(CONFIG_NPM1300_CHARGER npm1300_charger)
add_subdirectory_ifdef(CONFIG_NTC_THERMISTOR ntc_thermistor)
add_subdirectory_ifdef(CONFIG_NXP_TEMPMON nxp_tempmon)
add_subdirectory_ifdef(CONFIG_OPT3001 opt3001)
add_subdirectory_ifdef(CONFIG_PCNT_ESP32 pcnt_esp32)
add_subdirectory_ifdef(CONFIG_PMS7003 pms7003)

View file

@ -157,6 +157,7 @@ source "drivers/sensor/ntc_thermistor/Kconfig"
source "drivers/sensor/nuvoton_adc_cmp_npcx/Kconfig"
source "drivers/sensor/nuvoton_tach_npcx/Kconfig"
source "drivers/sensor/nxp_kinetis_temp/Kconfig"
source "drivers/sensor/nxp_tempmon/Kconfig"
source "drivers/sensor/opt3001/Kconfig"
source "drivers/sensor/pcnt_esp32/Kconfig"
source "drivers/sensor/pms7003/Kconfig"

View file

@ -0,0 +1,4 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources(nxp_tempmon.c)

View file

@ -0,0 +1,12 @@
# NXP temperature monitor (TEMPMON)
# Copyright (c) 2023 Centralp
# SPDX-License-Identifier: Apache-2.0
config NXP_TEMPMON
bool "NXP temperature monitor (TEMPMON)"
default y
depends on DT_HAS_NXP_TEMPMON_ENABLED
help
Enable driver for the NXP temperature monitor (TEMPMON).
This is used to retrieve on-die operational temperature.

View file

@ -0,0 +1,114 @@
/*
* Copyright (c) 2023 Centralp
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_tempmon
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>
/* OTP Controller Analog Register 1 for calibration values */
#define OCOTP_ANA1_ROOM_COUNT_SHIFT 20
#define OCOTP_ANA1_ROOM_COUNT_MASK (BIT_MASK(12) << OCOTP_ANA1_ROOM_COUNT_SHIFT)
#define OCOTP_ANA1_HOT_COUNT_SHIFT 8
#define OCOTP_ANA1_HOT_COUNT_MASK (BIT_MASK(12) << OCOTP_ANA1_HOT_COUNT_SHIFT)
#define OCOTP_ANA1_HOT_TEMP_SHIFT 0
#define OCOTP_ANA1_HOT_TEMP_MASK (BIT_MASK(8) << OCOTP_ANA1_HOT_TEMP_SHIFT)
#define TEMPMON_ROOM_TEMP 25.0f
struct nxp_tempmon_data {
uint8_t hot_temp;
uint16_t hot_cnt;
uint16_t room_cnt;
uint16_t temp_cnt;
};
struct nxp_tempmon_config {
TEMPMON_Type *base;
};
static int nxp_tempmon_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
struct nxp_tempmon_data *data = dev->data;
const struct nxp_tempmon_config *cfg = dev->config;
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_DIE_TEMP) {
return -ENOTSUP;
}
/* Start the measure */
cfg->base->TEMPSENSE0 |= TEMPMON_TEMPSENSE0_MEASURE_TEMP_MASK;
/* Wait until measure is ready */
while (!(cfg->base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_FINISHED_MASK)) {
}
data->temp_cnt = (cfg->base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >>
TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
/* Stop the measure */
cfg->base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_MEASURE_TEMP_MASK;
return 0;
}
static int nxp_tempmon_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
const struct nxp_tempmon_data *data = dev->data;
float temp;
if (chan != SENSOR_CHAN_DIE_TEMP) {
return -ENOTSUP;
}
temp = (float)data->hot_temp - ((float)data->temp_cnt - (float)data->hot_cnt) *
(((float)data->hot_temp - TEMPMON_ROOM_TEMP) /
((float)data->room_cnt - (float)data->hot_cnt));
return sensor_value_from_double(val, temp);
}
static const struct sensor_driver_api nxp_tempmon_driver_api = {
.sample_fetch = nxp_tempmon_sample_fetch,
.channel_get = nxp_tempmon_channel_get,
};
static int nxp_tempmon_init(const struct device *dev)
{
struct nxp_tempmon_data *data = dev->data;
const struct nxp_tempmon_config *cfg = dev->config;
uint32_t ocotp_ana1;
/* Enable the temperature sensor */
cfg->base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
/* Single measure with no repeat mode */
cfg->base->TEMPSENSE1 = TEMPMON_TEMPSENSE1_MEASURE_FREQ(0);
/* Read calibration data */
ocotp_ana1 = OCOTP->ANA1;
data->hot_temp = (ocotp_ana1 & OCOTP_ANA1_HOT_TEMP_MASK) >> OCOTP_ANA1_HOT_TEMP_SHIFT;
data->hot_cnt = (ocotp_ana1 & OCOTP_ANA1_HOT_COUNT_MASK) >> OCOTP_ANA1_HOT_COUNT_SHIFT;
data->room_cnt = (ocotp_ana1 & OCOTP_ANA1_ROOM_COUNT_MASK) >> OCOTP_ANA1_ROOM_COUNT_SHIFT;
return 0;
}
static struct nxp_tempmon_data nxp_tempmon_dev_data = {
.hot_temp = 0,
.hot_cnt = 0,
.room_cnt = 0,
.temp_cnt = 0,
};
static const struct nxp_tempmon_config nxp_tempmon_dev_config = {
.base = (TEMPMON_Type *)DT_INST_REG_ADDR(0),
};
SENSOR_DEVICE_DT_INST_DEFINE(0, nxp_tempmon_init, NULL, &nxp_tempmon_dev_data,
&nxp_tempmon_dev_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&nxp_tempmon_driver_api);

View file

@ -15,6 +15,7 @@
/ {
chosen {
zephyr,entropy = &trng;
die-temp0 = &tempmon;
};
cpus {
@ -1073,6 +1074,12 @@
interrupts = <50 0>, <51 0>;
status = "okay";
};
tempmon: tempmon@400d8000 {
compatible = "nxp,tempmon";
reg = <0x400d8000 0x2a0>;
status = "disabled";
};
};
};

View file

@ -0,0 +1,8 @@
# Copyright (c) 2023 Centralp
# SPDX-License-Identifier: Apache-2.0
description: NXP on-die temperature monitor
compatible: "nxp,tempmon"
include: sensor-device.yaml

View file

@ -0,0 +1 @@
CONFIG_NEWLIB_LIBC=y

View file

@ -0,0 +1,9 @@
/*
* Copyright (c) 2023 Centralp
*
* SPDX-License-Identifier: Apache-2.0
*/
&tempmon {
status = "okay";
};