Add TI TMP431 dual-channel temperature sensor driver

This commit is contained in:
Tomaz Zaman 2026-03-03 01:42:55 +01:00
parent 28d6e831d2
commit f535564d2d
5 changed files with 402 additions and 3 deletions

View file

@ -67,6 +67,7 @@ KMOD_LP= ${DRIVERS}/lp5812
KMOD_SFP= ${DRIVERS}/sfp-led
KMOD_PCF= ${DRIVERS}/pcf2131
KMOD_CAAM= ${DRIVERS}/caam
KMOD_TMP= ${DRIVERS}/tmp431
build-cdx:
${MAKE} -C ${KMOD_CDX} ${KMOD_ARGS}
@ -99,8 +100,11 @@ build-caam:
${MAKE} -C ${KMOD_CAAM} ${KMOD_ARGS}
modules: build-cdx build-fci build-auto_bridge build-pf_notify \
build-tmp431:
${MAKE} -C ${KMOD_TMP} ${KMOD_ARGS}
build-emc2302 build-ina2xx build-lp5812 build-sfpled build-pcf2131 \
build-caam
build-caam build-tmp431
# ============================================================
# Userspace components (dependency order: fmlib -> fmc -> dpa_app)
@ -150,6 +154,7 @@ dist: modules userspace
cp ${KMOD_SFP}/sfpled.ko ${DISTDIR}/
cp ${KMOD_PCF}/pcf2131.ko ${DISTDIR}/
cp ${KMOD_CAAM}/caam.ko ${DISTDIR}/
cp ${KMOD_TMP}/tmp431.ko ${DISTDIR}/
cp ${BUILDDIR}/fmc/fmc ${DISTDIR}/
cp ${BUILDDIR}/dpa_app/dpa_app ${DISTDIR}/
cp ${BUILDDIR}/cmm/cmm ${DISTDIR}/
@ -179,6 +184,7 @@ package: clean dist
install -m 644 ${DISTDIR}/ina2xx.ko ${PKG_STAGEDIR}/boot/modules/
install -m 644 ${DISTDIR}/pcf2131.ko ${PKG_STAGEDIR}/boot/modules/
install -m 644 ${DISTDIR}/caam.ko ${PKG_STAGEDIR}/boot/modules/
install -m 644 ${DISTDIR}/tmp431.ko ${PKG_STAGEDIR}/boot/modules/
# Userspace binaries
install -m 755 ${DISTDIR}/cmm ${PKG_STAGEDIR}/usr/local/sbin/
install -m 755 ${DISTDIR}/cmmctl ${PKG_STAGEDIR}/usr/local/sbin/
@ -220,10 +226,11 @@ clean:
${MAKE} -C ${KMOD_SFP} ${KMOD_ARGS} clean
${MAKE} -C ${KMOD_PCF} ${KMOD_ARGS} clean
${MAKE} -C ${KMOD_CAAM} ${KMOD_ARGS} clean
${MAKE} -C ${KMOD_TMP} ${KMOD_ARGS} clean
rm -rf ${BUILDDIR}
.PHONY: all modules userspace dist package clean \
build-cdx build-fci build-auto_bridge build-pf_notify \
build-emc2302 build-ina2xx build-lp5812 build-sfpled build-pcf2131 \
build-caam \
build-caam build-tmp431 \
fmlib fmc dpa_app cmm cmmctl fand

17
drivers/tmp431/Makefile Normal file
View file

@ -0,0 +1,17 @@
# TI TMP431 Temperature Sensor kernel module for FreeBSD
#
# Build:
# make -C /build/opnsense-deps/drivers/tmp431 \
# KERNBUILDDIR=/usr/obj/build/opnsense-src/arm64.aarch64/sys/GATEWAY \
# MACHINE=arm64 MACHINE_ARCH=aarch64 CC=cc LD=ld.lld
#
# Copyright 2026 Mono Technologies Inc.
# SPDX-License-Identifier: BSD-2-Clause
SYSDIR?= ${.CURDIR}/../../../opnsense-src/sys
KMOD= tmp431
SRCS= tmp431.c
SRCS+= opt_platform.h device_if.h bus_if.h iicbus_if.h ofw_bus_if.h
.include <bsd.kmod.mk>

374
drivers/tmp431/tmp431.c Normal file
View file

@ -0,0 +1,374 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2026 Mono Technologies Inc.
*
* TI TMP431 dual-channel temperature sensor driver.
* Each chip has a local (die) and remote (external diode) channel.
* Readings are exported under sysctl hw.temperature.* in decikelvin.
*
* Based on Linux drivers/hwmon/tmp401.c by Hans de Goede and Guenter Roeck.
*/
#include "opt_platform.h"
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/sysctl.h>
#include <dev/iicbus/iicbus.h>
#include <dev/iicbus/iiconf.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
/* Temperature registers (read) */
#define TMP431_LOCAL_TEMP_MSB 0x00
#define TMP431_REMOTE_TEMP_MSB 0x01
#define TMP431_LOCAL_TEMP_LSB 0x15
#define TMP431_REMOTE_TEMP_LSB 0x10
/* Status register */
#define TMP431_STATUS 0x02
#define TMP431_STATUS_REMOTE_OPEN (1 << 2)
/* Configuration register (different read/write addresses) */
#define TMP431_CONFIG_R 0x03
#define TMP431_CONFIG_W 0x09
#define TMP431_CONFIG_RANGE (1 << 2)
#define TMP431_CONFIG_SHUTDOWN (1 << 6)
/* Conversion rate register (different read/write addresses) */
#define TMP431_CONV_RATE_W 0x0A
#define TMP431_CONV_RATE_2HZ 5
/* Identification registers */
#define TMP431_MANUFACTURER_ID 0xFE
#define TMP431_DEVICE_ID 0xFF
#define TMP431_MFR_ID_TI 0x55
#define TMP431_DEV_ID_TMP431 0x31
/* Channel indices for sysctl arg2 */
#define TMP431_CHAN_LOCAL 0
#define TMP431_CHAN_REMOTE 1
struct tmp431_softc {
device_t dev;
struct mtx mtx;
bool extended_range;
char label[64];
char name_local[80];
char name_remote[80];
struct sysctl_ctx_list sysctl_ctx;
};
/* ================================================================
* I2C register helpers
* ================================================================ */
static int
tmp431_read8(device_t dev, uint8_t reg, uint8_t *val)
{
return (iicdev_readfrom(dev, reg, val, 1, IIC_WAIT));
}
static int
tmp431_write8(device_t dev, uint8_t reg, uint8_t val)
{
return (iicdev_writeto(dev, reg, &val, 1, IIC_WAIT));
}
/*
* Read a 16-bit temperature register atomically.
* The TMP431 supports SMBus word reads: reading from the MSB register
* returns MSB in the first byte and LSB in the second byte, all in
* one I2C transaction. This avoids torn reads between conversions.
*/
static int
tmp431_read_temp16(device_t dev, uint8_t msb_reg, uint16_t *val)
{
uint8_t buf[2];
int error;
error = iicdev_readfrom(dev, msb_reg, buf, 2, IIC_WAIT);
if (error != 0)
return (error);
*val = ((uint16_t)buf[0] << 8) | buf[1];
return (0);
}
/* ================================================================
* Temperature conversion
* ================================================================ */
/*
* Convert TMP431 raw 16-bit register value to decikelvin.
*
* Raw format: MSB[7:0] << 8 | LSB[7:0], bits [15:4] meaningful.
* Standard range: unsigned, 0 to 127°C.
* Extended range: subtract 64*256 for signed, -64 to 191°C.
*
* millideg_C = (raw * 125) / 32
* decikelvin = millideg_C / 100 + 2731
* = (raw * 125) / 3200 + 2731
*/
static int
tmp431_raw_to_decikelvin(uint16_t raw, bool extended_range)
{
int32_t reg, milli;
reg = (int32_t)(raw & ~0xf);
if (extended_range)
reg -= 64 * 256;
milli = reg * 125;
if (milli >= 0)
return ((milli + 1600) / 3200 + 2731);
else
return ((milli - 1600) / 3200 + 2731);
}
/* ================================================================
* Sysctl handler
* ================================================================ */
static int
tmp431_sysctl_temp(SYSCTL_HANDLER_ARGS)
{
struct tmp431_softc *sc = arg1;
int channel = arg2;
uint8_t status;
uint8_t msb_reg;
uint16_t raw;
int error, val;
if (req->newptr != NULL)
return (EINVAL);
if (channel == TMP431_CHAN_LOCAL)
msb_reg = TMP431_LOCAL_TEMP_MSB;
else
msb_reg = TMP431_REMOTE_TEMP_MSB;
mtx_lock(&sc->mtx);
error = tmp431_read_temp16(sc->dev, msb_reg, &raw);
if (error != 0) {
mtx_unlock(&sc->mtx);
return (EIO);
}
/*
* For the remote channel, check for open/fault diode.
* The chip sets STATUS_REMOTE_OPEN when it detects an open circuit,
* but doesn't always do so reliably. A raw reading of 0x0000
* (0°C exactly) also indicates no diode is connected — this board
* never operates at 0°C ambient so the false positive is acceptable.
*/
if (channel == TMP431_CHAN_REMOTE) {
error = tmp431_read8(sc->dev, TMP431_STATUS, &status);
if (error != 0) {
mtx_unlock(&sc->mtx);
return (EIO);
}
if ((status & TMP431_STATUS_REMOTE_OPEN) ||
(raw & ~0xf) == 0) {
mtx_unlock(&sc->mtx);
return (ENXIO);
}
}
mtx_unlock(&sc->mtx);
val = tmp431_raw_to_decikelvin(raw, sc->extended_range);
return (sysctl_handle_int(oidp, &val, 0, req));
}
/* ================================================================
* Probe / Attach / Detach
* ================================================================ */
static struct ofw_compat_data compat_data[] = {
{ "ti,tmp431", 1 },
{ NULL, 0 },
};
static int
tmp431_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
return (ENXIO);
device_set_desc(dev, "TI TMP431 Temperature Sensor");
return (BUS_PROBE_DEFAULT);
}
static int
tmp431_attach(device_t dev)
{
struct tmp431_softc *sc;
struct sysctl_oid *temp_oid;
phandle_t node;
uint8_t mfr_id = 0, dev_id = 0, config;
int error, unit;
sc = device_get_softc(dev);
sc->dev = dev;
/* Verify manufacturer and device ID */
error = tmp431_read8(dev, TMP431_MANUFACTURER_ID, &mfr_id);
if (error != 0 || mfr_id != TMP431_MFR_ID_TI) {
device_printf(dev, "unexpected manufacturer ID: 0x%02x\n",
mfr_id);
return (ENXIO);
}
error = tmp431_read8(dev, TMP431_DEVICE_ID, &dev_id);
if (error != 0 || dev_id != TMP431_DEV_ID_TMP431) {
device_printf(dev, "unexpected device ID: 0x%02x\n", dev_id);
return (ENXIO);
}
mtx_init(&sc->mtx, "tmp431", NULL, MTX_DEF);
/* Set conversion rate to 2 Hz */
error = tmp431_write8(dev, TMP431_CONV_RATE_W, TMP431_CONV_RATE_2HZ);
if (error != 0) {
device_printf(dev, "failed to set conversion rate\n");
goto fail;
}
/* Clear shutdown bit, optionally enable extended range */
error = tmp431_read8(dev, TMP431_CONFIG_R, &config);
if (error != 0) {
device_printf(dev, "failed to read config\n");
goto fail;
}
config &= ~TMP431_CONFIG_SHUTDOWN;
node = ofw_bus_get_node(dev);
if (node > 0 && OF_hasprop(node, "ti,extended-range-enable"))
config |= TMP431_CONFIG_RANGE;
sc->extended_range = !!(config & TMP431_CONFIG_RANGE);
error = tmp431_write8(dev, TMP431_CONFIG_W, config);
if (error != 0) {
device_printf(dev, "failed to write config\n");
goto fail;
}
/* Read label from DT, fall back to "tmpN" */
sc->label[0] = '\0';
if (node > 0)
OF_getprop(node, "label", sc->label, sizeof(sc->label));
sc->label[sizeof(sc->label) - 1] = '\0';
unit = device_get_unit(dev);
if (sc->label[0] != '\0') {
snprintf(sc->name_local, sizeof(sc->name_local),
"%s-local", sc->label);
snprintf(sc->name_remote, sizeof(sc->name_remote),
"%s-remote", sc->label);
} else {
snprintf(sc->name_local, sizeof(sc->name_local),
"tmp%d-local", unit);
snprintf(sc->name_remote, sizeof(sc->name_remote),
"tmp%d-remote", unit);
}
/* Create sysctl entries under hw.temperature */
sysctl_ctx_init(&sc->sysctl_ctx);
temp_oid = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, "temperature",
CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
"Temperature sensors");
if (temp_oid == NULL) {
device_printf(dev, "failed to create hw.temperature node\n");
goto fail_sysctl;
}
SYSCTL_ADD_PROC(&sc->sysctl_ctx,
SYSCTL_CHILDREN(temp_oid), OID_AUTO, sc->name_local,
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
sc, TMP431_CHAN_LOCAL, tmp431_sysctl_temp,
"IK", "TMP431 local die temperature");
SYSCTL_ADD_PROC(&sc->sysctl_ctx,
SYSCTL_CHILDREN(temp_oid), OID_AUTO, sc->name_remote,
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
sc, TMP431_CHAN_REMOTE, tmp431_sysctl_temp,
"IK", "TMP431 remote diode temperature");
device_printf(dev, "TMP431 %s, %s range\n",
sc->label[0] != '\0' ? sc->label : "unlabeled",
sc->extended_range ? "extended" : "standard");
return (0);
fail_sysctl:
sysctl_ctx_free(&sc->sysctl_ctx);
fail:
mtx_destroy(&sc->mtx);
return (ENXIO);
}
static int
tmp431_detach(device_t dev)
{
struct tmp431_softc *sc;
uint8_t config;
sc = device_get_softc(dev);
/* Remove sysctl entries first to drain any in-progress readers */
sysctl_ctx_free(&sc->sysctl_ctx);
/* Put chip in shutdown mode */
if (tmp431_read8(dev, TMP431_CONFIG_R, &config) == 0) {
config |= TMP431_CONFIG_SHUTDOWN;
tmp431_write8(dev, TMP431_CONFIG_W, config);
}
mtx_destroy(&sc->mtx);
return (0);
}
/* ================================================================
* Module registration
* ================================================================ */
static device_method_t tmp431_methods[] = {
DEVMETHOD(device_probe, tmp431_probe),
DEVMETHOD(device_attach, tmp431_attach),
DEVMETHOD(device_detach, tmp431_detach),
DEVMETHOD_END
};
static driver_t tmp431_driver = {
"tmp431",
tmp431_methods,
sizeof(struct tmp431_softc),
};
DRIVER_MODULE(tmp431, iicbus, tmp431_driver, NULL, NULL);
MODULE_VERSION(tmp431, 1);
MODULE_DEPEND(tmp431, iicbus, IICBUS_MODVER, IICBUS_MODVER, IICBUS_MODVER);
IICBUS_FDT_PNP_INFO(compat_data);

View file

@ -8,6 +8,7 @@
/boot/modules/ina2xx.ko
/boot/modules/pcf2131.ko
/boot/modules/caam.ko
/boot/modules/tmp431.ko
/usr/local/sbin/cmm
/usr/local/sbin/cmmctl
/usr/local/sbin/dpa_app

View file

@ -2,7 +2,7 @@
# OPNsense rc.syshook early script — load Mono Gateway kernel modules
# Installed to /usr/local/etc/rc.syshook.d/early/02-mono-modules
for mod in cdx auto_bridge pf_notify fci lp5812 sfpled emc2302 pcf2131 cryptodev caam; do
for mod in cdx auto_bridge pf_notify fci lp5812 sfpled emc2302 ina2xx pcf2131 tmp431 cryptodev caam; do
kldload ${mod} 2>/dev/null
done