Commit aa7618ba authored by Peter Wang's avatar Peter Wang Committed by Daniel DeGrasse
Browse files

sensor: add nxp_lpadc_temp40 temperature sensor driver



1. add nxp_lpadc_temp40 temperature sensor driver
2. tested with frdm_mcxa166 and frdm_mcxa276 board

Signed-off-by: default avatarPeter Wang <chaoyi.wang@nxp.com>
parent 22210724
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
add_subdirectory_ifdef(CONFIG_FXAS21002 fxas21002)
add_subdirectory_ifdef(CONFIG_FXLS8974 fxls8974)
add_subdirectory_ifdef(CONFIG_FXOS8700 fxos8700)
add_subdirectory_ifdef(CONFIG_LPADC_TEMP40 nxp_lpadc_temp40)
add_subdirectory_ifdef(CONFIG_MCUX_LPCMP mcux_lpcmp)
add_subdirectory_ifdef(CONFIG_NXP_TEMPMON nxp_tempmon)
add_subdirectory_ifdef(CONFIG_P3T1755 p3t1755)
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ source "drivers/sensor/nxp/fxos8700/Kconfig"
source "drivers/sensor/nxp/mcux_acmp/Kconfig"
source "drivers/sensor/nxp/mcux_lpcmp/Kconfig"
source "drivers/sensor/nxp/nxp_kinetis_temp/Kconfig"
source "drivers/sensor/nxp/nxp_lpadc_temp40/Kconfig"
source "drivers/sensor/nxp/nxp_tempmon/Kconfig"
source "drivers/sensor/nxp/p3t1755/Kconfig"
source "drivers/sensor/nxp/qdec_mcux/Kconfig"
+5 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

zephyr_library()

zephyr_library_sources(lpadc_temp40.c)
+12 −0
Original line number Diff line number Diff line
# NXP LPADC_TEMP40 temperature sensor configuration options

# Copyright 2025 NXP
# SPDX-License-Identifier: Apache-2.0

config LPADC_TEMP40
	bool "NXP LPADC_TEMP40 Temperature Sensor"
	default y
	depends on DT_HAS_NXP_LPADC_TEMP40_ENABLED
	select ADC
	help
	  Enable driver for NXP LPADC_TEMP40 temperature sensor.
+149 −0
Original line number Diff line number Diff line
/*
 * Copyright 2025 NXP
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define DT_DRV_COMPAT nxp_lpadc_temp40

#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/logging/log.h>
#include <zephyr/devicetree.h>

LOG_MODULE_REGISTER(lpadc_temp40, CONFIG_SENSOR_LOG_LEVEL);

/* Four ADC samples: first two are dropped, then VBE1 and VBE8 */
#define TEMP_ADC_SAMPLES 1

struct lpadc_temp40_config {
	const struct device *adc;
	struct adc_sequence adc_seq;
	struct adc_channel_cfg ch_cfg;
};

struct lpadc_temp40_data {
	float temperature;
	uint16_t buffer[TEMP_ADC_SAMPLES];
};

static int lpadc_temp40_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
	const struct lpadc_temp40_config *config = dev->config;
	struct lpadc_temp40_data *data = dev->data;
	uint32_t conv_result_shift = 0U;
	uint16_t vbe1 = 0U;
	uint16_t vbe8 = 0U;
	int err;

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

#if defined(FSL_FEATURE_LPADC_TEMP_SENS_BUFFER_SIZE) &&                                            \
	(FSL_FEATURE_LPADC_TEMP_SENS_BUFFER_SIZE == 4U)
	/* The first two results are useless. */
	err = adc_read(config->adc, &config->adc_seq);
	if (err) {
		LOG_ERR("Failed to read ADC channels (err %d)", err);
		return err;
	}
	err = adc_read(config->adc, &config->adc_seq);
	if (err) {
		LOG_ERR("Failed to read ADC channels (err %d)", err);
		return err;
	}
#endif /* FSL_FEATURE_LPADC_TEMP_SENS_BUFFER_SIZE */

	err = adc_read(config->adc, &config->adc_seq);
	if (err) {
		LOG_ERR("Failed to read ADC channels (err %d)", err);
		return err;
	}
	vbe1 = data->buffer[0] >> conv_result_shift;

	err = adc_read(config->adc, &config->adc_seq);
	if (err) {
		LOG_ERR("Failed to read ADC channels (err %d)", err);
		return err;
	}
	vbe8 = data->buffer[0] >> conv_result_shift;

	/* Calculate temperature using exact formula */
	data->temperature =
		FSL_FEATURE_LPADC_TEMP_PARAMETER_A *
			(FSL_FEATURE_LPADC_TEMP_PARAMETER_ALPHA * ((float)vbe8 - (float)vbe1) /
			 ((float)vbe8 +
			  FSL_FEATURE_LPADC_TEMP_PARAMETER_ALPHA * ((float)vbe8 - (float)vbe1))) -
		FSL_FEATURE_LPADC_TEMP_PARAMETER_B;

	LOG_DBG("VBE1=%d VBE8=%d Temp=%.3f", vbe1, vbe8, (double)data->temperature);
	return 0;
}

static int lpadc_temp40_channel_get(const struct device *dev, enum sensor_channel chan,
				    struct sensor_value *val)
{
	struct lpadc_temp40_data *data = dev->data;

	if (chan != SENSOR_CHAN_DIE_TEMP) {
		return -ENOTSUP;
	}

	val->val1 = (int32_t)data->temperature;
	val->val2 = (int32_t)((data->temperature - (float)val->val1) * 1000000);

	return 0;
}

static DEVICE_API(sensor, lpadc_temp40_driver_api) = {
	.sample_fetch = lpadc_temp40_sample_fetch,
	.channel_get = lpadc_temp40_channel_get,
};

static int lpadc_temp40_init(const struct device *dev)
{
	const struct lpadc_temp40_config *config = dev->config;
	int err;

	if (!device_is_ready(config->adc)) {
		LOG_ERR("ADC device not ready");
		return -ENODEV;
	}

	err = adc_channel_setup(config->adc, &config->ch_cfg);
	if (err) {
		LOG_ERR("Failed to setup ADC channel (err %d)", err);
		return err;
	}

	return 0;
}

#define LPADC_TEMP40_INIT(inst)                                                                    \
	static struct lpadc_temp40_data lpadc_temp40_data_##inst = {                               \
		.buffer = {0},                                                                     \
		.temperature = -273.15f,                                                           \
	};                                                                                         \
                                                                                                   \
	static const struct lpadc_temp40_config lpadc_temp40_config_##inst = {                     \
		.adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)),                              \
		.adc_seq =                                                                         \
			{                                                                          \
				.channels = BIT(DT_INST_IO_CHANNELS_INPUT(inst)),                  \
				.buffer = &lpadc_temp40_data_##inst.buffer,                        \
				.buffer_size = sizeof(lpadc_temp40_data_##inst.buffer),            \
				.resolution = 16,                                                  \
				.oversampling = 7,                                                 \
			},                                                                         \
		.ch_cfg = ADC_CHANNEL_CFG_DT(                                                      \
			DT_CHILD(DT_INST_IO_CHANNELS_CTLR(inst),                                   \
				 UTIL_CAT(channel_, DT_INST_IO_CHANNELS_INPUT(inst)))),            \
	};                                                                                         \
                                                                                                   \
	SENSOR_DEVICE_DT_INST_DEFINE(inst, lpadc_temp40_init, NULL, &lpadc_temp40_data_##inst,     \
				     &lpadc_temp40_config_##inst, POST_KERNEL,                     \
				     CONFIG_SENSOR_INIT_PRIORITY, &lpadc_temp40_driver_api);

DT_INST_FOREACH_STATUS_OKAY(LPADC_TEMP40_INIT)
Loading