ALT Linux kernel packages development
 help / color / mirror / Atom feed
From: Daniil Gnusarev <gnusarevda@basealt.ru>
To: gnusarevda@basealt.ru, devel-kernel@lists.altlinux.org
Subject: [d-kernel] [PATCH 32/35] hwmon: add Baikal-M monitoring driver
Date: Fri, 27 Feb 2026 14:32:33 +0400
Message-ID: <20260227103236.785736-33-gnusarevda@basealt.ru> (raw)
In-Reply-To: <20260227103236.785736-1-gnusarevda@basealt.ru>

Baikal BE-M1000 SoC Process, Voltage, Temperature sensor driver
Taken from SDK ARM64-2509-6.12.

Signed-off-by: Daniil Gnusarev <gnusarevda@basealt.ru>
Do-not-upstream: this is a feature of Baikal-M
---
 drivers/hwmon/Kconfig            |   11 +
 drivers/hwmon/Makefile           |    2 +
 drivers/hwmon/baikal-pvt-core.c  | 1160 ++++++++++++++++++++++++++++++
 drivers/hwmon/baikal-pvt.h       |  273 +++++++
 drivers/hwmon/bm1000-pvt-hwmon.c |  201 ++++++
 5 files changed, 1647 insertions(+)
 create mode 100644 drivers/hwmon/baikal-pvt-core.c
 create mode 100644 drivers/hwmon/baikal-pvt.h
 create mode 100644 drivers/hwmon/bm1000-pvt-hwmon.c

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 2760feb9f83b5d..823c2c1d4de098 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -457,6 +457,17 @@ config SENSORS_ATXP1
 	  This driver can also be built as a module. If so, the module
 	  will be called atxp1.
 
+config SENSORS_BM1000_PVT
+	tristate "Baikal BE-M1000 SoC Process, Voltage, Temperature sensor driver"
+	depends on ARCH_BAIKAL || COMPILE_TEST
+	select POLYNOMIAL
+	help
+	  If you say yes here you get support for Baikal BE-M1000 SoC embedded
+	  PVT sensors.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called bm1000-pvt.
+
 config SENSORS_BT1_PVT
 	tristate "Baikal-T1 Process, Voltage, Temperature sensor driver"
 	depends on MIPS_BAIKAL_T1 || COMPILE_TEST
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index 73b2abdcc6dd9c..098086d3034d29 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -58,6 +58,8 @@ obj-$(CONFIG_SENSORS_ASPEED_G6) += aspeed-g6-pwm-tach.o
 obj-$(CONFIG_SENSORS_ASUS_ROG_RYUJIN)	+= asus_rog_ryujin.o
 obj-$(CONFIG_SENSORS_ATXP1)	+= atxp1.o
 obj-$(CONFIG_SENSORS_AXI_FAN_CONTROL) += axi-fan-control.o
+bm1000-pvt-objs := baikal-pvt-core.o bm1000-pvt-hwmon.o
+obj-$(CONFIG_SENSORS_BM1000_PVT) += bm1000-pvt.o
 obj-$(CONFIG_SENSORS_BT1_PVT)	+= bt1-pvt.o
 obj-$(CONFIG_SENSORS_CGBC)	+= cgbc-hwmon.o
 obj-$(CONFIG_SENSORS_CHIPCAP2) += chipcap2.o
diff --git a/drivers/hwmon/baikal-pvt-core.c b/drivers/hwmon/baikal-pvt-core.c
new file mode 100644
index 00000000000000..7a649b65768f72
--- /dev/null
+++ b/drivers/hwmon/baikal-pvt-core.c
@@ -0,0 +1,1160 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2025 BAIKAL ELECTRONICS, JSC
+ *
+ * Authors:
+ *   Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>
+ *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
+ *
+ * Baikal SoCs Process, Voltage, Temperature sensor drivers common code
+ */
+
+#include <linux/acpi.h>
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/ktime.h>
+#include <linux/limits.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/seqlock.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
+
+#include "baikal-pvt.h"
+
+#define PVT_THERMAL_POLLING_DELAY	5000
+
+#define PVT_THERMAL_CRIT	90000	/* 90 degrees default critical temperature limit */
+#define PVT_THERMAL_CRIT_HYST	2000
+
+static inline u32 pvt_update(struct pvt_hwmon *pvt, u32 reg, u32 mask, u32 data)
+{
+	u32 old;
+
+	old = pvt->ops->read(pvt, reg);
+	pvt->ops->write(pvt, reg, (old & ~mask) | (data & mask));
+
+	return old & mask;
+}
+
+/*
+ * Baikal SoCs PVT mode can be updated only when the controller is disabled.
+ * So first we disable it, then set the new mode together with the controller
+ * getting back enabled. The same concerns the temperature trim and
+ * measurements timeout. If it is necessary the interface mutex is supposed
+ * to be locked at the time the operations are performed.
+ */
+static inline void pvt_set_mode(struct pvt_hwmon *pvt, u32 mode)
+{
+	u32 old;
+
+	mode = FIELD_PREP(PVT_CTRL_MODE_MASK, mode);
+
+	old = pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, 0);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_MODE_MASK | PVT_CTRL_EN, mode | old);
+}
+
+static inline u32 pvt_calc_trim(long temp)
+{
+	temp = clamp_val(temp, 0, PVT_TRIM_TEMP);
+
+	return DIV_ROUND_UP(temp, PVT_TRIM_STEP);
+}
+
+static inline void pvt_set_trim(struct pvt_hwmon *pvt, u32 trim)
+{
+	u32 old;
+
+	trim = FIELD_PREP(PVT_CTRL_TRIM_MASK, trim);
+
+	old = pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, 0);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_TRIM_MASK | PVT_CTRL_EN, trim | old);
+}
+
+static inline void pvt_set_tout(struct pvt_hwmon *pvt, u32 tout)
+{
+	u32 old;
+
+	old = pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, 0);
+	pvt->ops->write(pvt, PVT_TTIMEOUT, tout);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, old);
+}
+
+/*
+ * This driver can optionally provide the hwmon alarms for each sensor the PVT
+ * controller supports. The alarms functionality is made compile-time
+ * configurable due to the hardware interface implementation peculiarity
+ * described further in this comment. So in case if alarms are unnecessary in
+ * your system design it's recommended to have them disabled to prevent the PVT
+ * IRQs being periodically raised to get the data cache/alarms status up to
+ * date.
+ *
+ * Baikal SoCs PVT embedded controller is based on the Analog Bits PVT sensor,
+ * but is equipped with a dedicated control wrapper. It exposes the PVT
+ * sub-block registers space via the APB3 bus. In addition the wrapper provides
+ * a common interrupt vector of the sensors conversion completion events and
+ * threshold value alarms. Alas the wrapper interface hasn't been fully thought
+ * through. There is only one sensor can be activated at a time, for which the
+ * thresholds comparator is enabled right after the data conversion is
+ * completed. Due to this if alarms need to be implemented for all available
+ * sensors we can't just set the thresholds and enable the interrupts. We need
+ * to enable the sensors one after another and let the controller to detect
+ * the alarms by itself at each conversion. This also makes pointless to handle
+ * the alarms interrupts, since in occasion they happen synchronously with
+ * data conversion completion. The best driver design would be to have the
+ * completion interrupts enabled only and keep the converted value in the
+ * driver data cache. This solution is implemented if hwmon alarms are enabled
+ * in this driver. In case if the alarms are disabled, the conversion is
+ * performed on demand at the time a sensors input file is read.
+ */
+
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+
+#define pvt_hard_isr NULL
+
+static irqreturn_t pvt_soft_isr(int irq, void *data)
+{
+	const struct pvt_sensor_info *info;
+	struct pvt_hwmon *pvt = data;
+	struct pvt_cache *cache;
+	u32 val, thres_sts, old;
+
+	/*
+	 * DVALID bit will be cleared by reading the data. We need to save the
+	 * status before the next conversion happens. Threshold events will be
+	 * handled a bit later.
+	 */
+	thres_sts = pvt->ops->read(pvt, PVT_RAW_INTR_STAT);
+
+	/*
+	 * Then lets recharge the PVT interface with the next sampling mode.
+	 * Lock the interface mutex to serialize trim, timeouts and alarm
+	 * thresholds settings.
+	 */
+	cache = &pvt->cache[pvt->sensor];
+	info = &pvt->info[pvt->sensor];
+	pvt->sensor = (pvt->sensor == PVT_SENSOR_LAST) ?
+		      PVT_SENSOR_FIRST : (pvt->sensor + 1);
+
+	/*
+	 * For some reason we have to mask the interrupt before changing the
+	 * mode, otherwise sometimes the temperature mode doesn't get
+	 * activated even though the actual mode in the ctrl register
+	 * corresponds to one. Then we read the data. By doing so we also
+	 * recharge the data conversion. After this the mode corresponding
+	 * to the next sensor in the row is set. Finally we enable the
+	 * interrupts back.
+	 */
+	mutex_lock(&pvt->iface_mtx);
+
+	old = pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_DVALID, PVT_INTR_DVALID);
+
+	val = pvt->ops->read(pvt, PVT_DATA);
+
+	pvt_set_mode(pvt, pvt->info[pvt->sensor].mode);
+
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_DVALID, old);
+
+	mutex_unlock(&pvt->iface_mtx);
+
+	/*
+	 * We can now update the data cache with data just retrieved from the
+	 * sensor. Lock write-seqlock to make sure the reader has a coherent
+	 * data.
+	 */
+	write_seqlock(&cache->data_seqlock);
+
+	cache->data = FIELD_GET(PVT_DATA_DATA_MASK, val);
+
+	write_sequnlock(&cache->data_seqlock);
+
+	/*
+	 * While PVT core is doing the next mode data conversion, we'll check
+	 * whether the alarms were triggered for the current sensor. Note that
+	 * according to the documentation only one threshold IRQ status can be
+	 * set at a time, that's why if-else statement is utilized.
+	 */
+	if ((thres_sts & info->thres_sts_lo) ^ cache->thres_sts_lo) {
+		WRITE_ONCE(cache->thres_sts_lo, thres_sts & info->thres_sts_lo);
+		hwmon_notify_event(pvt->hwmon, info->type, info->attr_min_alarm,
+				   info->channel);
+		if (info->type == hwmon_temp)
+			thermal_zone_device_update(pvt->tzd, THERMAL_EVENT_UNSPECIFIED);
+	} else if ((thres_sts & info->thres_sts_hi) ^ cache->thres_sts_hi) {
+		WRITE_ONCE(cache->thres_sts_hi, thres_sts & info->thres_sts_hi);
+		hwmon_notify_event(pvt->hwmon, info->type, info->attr_max_alarm,
+				   info->channel);
+		if (info->type == hwmon_temp)
+			thermal_zone_device_update(pvt->tzd, THERMAL_EVENT_UNSPECIFIED);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
+{
+	return 0644;
+}
+
+static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
+{
+	return 0444;
+}
+
+static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			 long *val)
+{
+	struct pvt_cache *cache = &pvt->cache[type];
+	unsigned int seq;
+	u32 data;
+
+	do {
+		seq = read_seqbegin(&cache->data_seqlock);
+		data = cache->data;
+	} while (read_seqretry(&cache->data_seqlock, seq));
+
+	*val = pvt->ops->from_pvt(pvt, type, data);
+
+	return 0;
+}
+
+static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			  bool is_low, long *val)
+{
+	u32 data;
+
+	/* No need in serialization, since it is just read from MMIO. */
+	data = pvt->ops->read(pvt, pvt->info[type].thres_base);
+
+	if (is_low)
+		data = FIELD_GET(PVT_THRES_LO_MASK, data);
+	else
+		data = FIELD_GET(PVT_THRES_HI_MASK, data);
+
+	*val = pvt->ops->from_pvt(pvt, type, data);
+
+	return 0;
+}
+
+static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			   bool is_low, long val)
+{
+	u32 data, limit, mask;
+	int ret;
+
+	val = clamp(val, pvt->info[type].value_min, pvt->info[type].value_max);
+	data = pvt->ops->to_pvt(pvt, type, val);
+
+	/* Serialize limit update, since a part of the register is changed. */
+	ret = mutex_lock_interruptible(&pvt->iface_mtx);
+	if (ret)
+		return ret;
+
+	/* Make sure the upper and lower ranges don't intersect. */
+	limit = pvt->ops->read(pvt, pvt->info[type].thres_base);
+	if (is_low) {
+		limit = FIELD_GET(PVT_THRES_HI_MASK, limit);
+		data = clamp_val(data, PVT_DATA_MIN, limit);
+		data = FIELD_PREP(PVT_THRES_LO_MASK, data);
+		mask = PVT_THRES_LO_MASK;
+	} else {
+		limit = FIELD_GET(PVT_THRES_LO_MASK, limit);
+		data = clamp_val(data, limit, PVT_DATA_MAX);
+		data = FIELD_PREP(PVT_THRES_HI_MASK, data);
+		mask = PVT_THRES_HI_MASK;
+	}
+
+	pvt_update(pvt, pvt->info[type].thres_base, mask, data);
+
+	mutex_unlock(&pvt->iface_mtx);
+
+	return 0;
+}
+
+static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			  bool is_low, long *val)
+{
+	if (is_low)
+		*val = !!READ_ONCE(pvt->cache[type].thres_sts_lo);
+	else
+		*val = !!READ_ONCE(pvt->cache[type].thres_sts_hi);
+
+	return 0;
+}
+
+#else /* !CONFIG_SENSORS_BAIKAL_PVT_ALARMS */
+
+static irqreturn_t pvt_hard_isr(int irq, void *data)
+{
+	struct pvt_hwmon *pvt = data;
+	struct pvt_cache *cache;
+	u32 val;
+
+	/*
+	 * Mask the DVALID interrupt so after exiting from the handler a
+	 * repeated conversion wouldn't happen.
+	 */
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_DVALID, PVT_INTR_DVALID);
+
+	/*
+	 * Nothing special for alarm-less driver. Just read the data, update
+	 * the cache and notify a waiter of this event.
+	 */
+	val = pvt->ops->read(pvt, PVT_DATA);
+	if (!(val & PVT_DATA_VALID)) {
+		dev_err(pvt->dev, "Got IRQ when data isn't valid\n");
+		return IRQ_HANDLED;
+	}
+
+	cache = &pvt->cache[pvt->sensor];
+
+	WRITE_ONCE(cache->data, FIELD_GET(PVT_DATA_DATA_MASK, val));
+
+	complete(&cache->conversion);
+
+	return IRQ_HANDLED;
+}
+
+#define pvt_soft_isr NULL
+
+static inline umode_t pvt_limit_is_visible(enum pvt_sensor_type type)
+{
+	return 0;
+}
+
+static inline umode_t pvt_alarm_is_visible(enum pvt_sensor_type type)
+{
+	return 0;
+}
+
+static int pvt_read_data(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			 long *val)
+{
+	struct pvt_cache *cache = &pvt->cache[type];
+	unsigned long timeout;
+	u32 data;
+	int ret;
+
+	/*
+	 * Lock PVT conversion interface until data cache is updated. The
+	 * data read procedure is following: set the requested PVT sensor
+	 * mode, enable IRQ and conversion, wait until conversion is finished,
+	 * then disable conversion and IRQ, and read the cached data.
+	 */
+	ret = mutex_lock_interruptible(&pvt->iface_mtx);
+	if (ret)
+		return ret;
+
+	pvt->sensor = type;
+	pvt_set_mode(pvt, pvt->info[type].mode);
+
+	/*
+	 * Unmask the DVALID interrupt and enable the sensors conversions.
+	 * Do the reverse procedure when conversion is done.
+	 */
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_DVALID, 0);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
+
+	/*
+	 * Wait with timeout since in case if the sensor is suddenly powered
+	 * down the request won't be completed and the caller will hang up on
+	 * this procedure until the power is back up again. Multiply the
+	 * timeout by the factor of two to prevent a false timeout.
+	 */
+	timeout = 2 * usecs_to_jiffies(ktime_to_us(pvt->timeout));
+	ret = wait_for_completion_timeout(&cache->conversion, timeout);
+
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, 0);
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_DVALID, PVT_INTR_DVALID);
+
+	data = READ_ONCE(cache->data);
+
+	mutex_unlock(&pvt->iface_mtx);
+
+	if (!ret)
+		return -ETIMEDOUT;
+
+	*val = pvt->ops->from_pvt(pvt, type, data);
+
+	return 0;
+}
+
+static int pvt_read_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			  bool is_low, long *val)
+{
+	return -EOPNOTSUPP;
+}
+
+static int pvt_write_limit(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			   bool is_low, long val)
+{
+	return -EOPNOTSUPP;
+}
+
+static int pvt_read_alarm(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			  bool is_low, long *val)
+{
+	return -EOPNOTSUPP;
+}
+
+#endif /* !CONFIG_SENSORS_BAIKAL_PVT_ALARMS */
+
+static inline bool pvt_hwmon_channel_is_valid(enum hwmon_sensor_types type,
+					      int ch)
+{
+	switch (type) {
+	case hwmon_temp:
+		if (ch < 0 || ch >= PVT_TEMP_CHS)
+			return false;
+		break;
+	case hwmon_in:
+		if (ch < 0 || ch >= PVT_VOLT_CHS)
+			return false;
+		break;
+	default:
+		break;
+	}
+
+	/* The rest of the types are independent from the channel number. */
+	return true;
+}
+
+static umode_t pvt_hwmon_is_visible(const void *data,
+				    enum hwmon_sensor_types type,
+				    u32 attr, int ch)
+{
+	if (!pvt_hwmon_channel_is_valid(type, ch))
+		return 0;
+
+	switch (type) {
+	case hwmon_chip:
+		switch (attr) {
+		case hwmon_chip_update_interval:
+			return 0644;
+		}
+		break;
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_input:
+		case hwmon_temp_type:
+		case hwmon_temp_label:
+			return 0444;
+		case hwmon_temp_min:
+		case hwmon_temp_max:
+			return pvt_limit_is_visible(PVT_TEMP + ch);
+		case hwmon_temp_min_alarm:
+		case hwmon_temp_max_alarm:
+			return pvt_alarm_is_visible(PVT_TEMP + ch);
+		case hwmon_temp_offset:
+			return 0644;
+		}
+		break;
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+		case hwmon_in_label:
+			return 0444;
+		case hwmon_in_min:
+		case hwmon_in_max:
+			return pvt_limit_is_visible(PVT_VOLT + ch);
+		case hwmon_in_min_alarm:
+		case hwmon_in_max_alarm:
+			return pvt_alarm_is_visible(PVT_VOLT + ch);
+		}
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+static int pvt_read_trim(struct pvt_hwmon *pvt, long *val)
+{
+	u32 data;
+
+	data = pvt->ops->read(pvt, PVT_CTRL);
+	*val = FIELD_GET(PVT_CTRL_TRIM_MASK, data) * PVT_TRIM_STEP;
+
+	return 0;
+}
+
+static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
+{
+	u32 trim;
+	int ret;
+
+	/*
+	 * Serialize trim update, since a part of the register is changed and
+	 * the controller is supposed to be disabled during this operation.
+	 */
+	ret = mutex_lock_interruptible(&pvt->iface_mtx);
+	if (ret)
+		return ret;
+
+	trim = pvt_calc_trim(val);
+	pvt_set_trim(pvt, trim);
+
+	mutex_unlock(&pvt->iface_mtx);
+
+	return 0;
+}
+
+static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
+{
+	int ret;
+
+	ret = mutex_lock_interruptible(&pvt->iface_mtx);
+	if (ret)
+		return ret;
+
+	/* Return the result in msec as hwmon sysfs interface requires. */
+	*val = ktime_to_ms(pvt->timeout);
+
+	mutex_unlock(&pvt->iface_mtx);
+
+	return 0;
+}
+
+static u32 pvt_calc_tout(ktime_t timeout, unsigned long rate)
+{
+	ktime_t kt;
+	u32 tout;
+
+	/*
+	 * Subtract a constant lag, which always persists due to the limited
+	 * PVT sampling rate. Make sure the timeout is not negative.
+	 */
+	kt = ktime_sub_ns(timeout, PVT_TOUT_MIN);
+	if (ktime_to_ns(kt) < 0)
+		kt = ktime_set(0, 0);
+
+	/*
+	 * Recalculate the timeout in terms of the reference clock
+	 * period.
+	 */
+	tout = ktime_divns(kt * rate, NSEC_PER_SEC);
+
+	return tout;
+}
+
+static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
+{
+	unsigned long rate;
+	ktime_t kt, cache;
+	u32 data;
+	int ret;
+
+	rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
+	if (!rate)
+		return -ENODEV;
+
+	/*
+	 * If alarms are enabled, the requested timeout must be divided
+	 * between all available sensors to have the requested delay
+	 * applicable to each individual sensor.
+	 */
+	cache = kt = ms_to_ktime(val);
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+	kt = ktime_divns(kt, PVT_SENSORS_NUM);
+#endif
+
+	data = pvt_calc_tout(kt, rate);
+
+	/*
+	 * Update the measurements delay, but lock the interface first, since
+	 * we have to disable PVT in order to have the new delay actually
+	 * updated.
+	 */
+	ret = mutex_lock_interruptible(&pvt->iface_mtx);
+	if (ret)
+		return ret;
+
+	pvt_set_tout(pvt, data);
+	pvt->timeout = cache;
+
+	mutex_unlock(&pvt->iface_mtx);
+
+	return 0;
+}
+
+static int pvt_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+			  u32 attr, int ch, long *val)
+{
+	struct pvt_hwmon *pvt = dev_get_drvdata(dev);
+
+	if (!pvt_hwmon_channel_is_valid(type, ch))
+		return -EINVAL;
+
+	switch (type) {
+	case hwmon_chip:
+		switch (attr) {
+		case hwmon_chip_update_interval:
+			return pvt_read_timeout(pvt, val);
+		}
+		break;
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_input:
+			return pvt_read_data(pvt, PVT_TEMP + ch, val);
+		case hwmon_temp_type:
+			*val = 1;
+			return 0;
+		case hwmon_temp_min:
+			return pvt_read_limit(pvt, PVT_TEMP + ch, true, val);
+		case hwmon_temp_max:
+			return pvt_read_limit(pvt, PVT_TEMP + ch, false, val);
+		case hwmon_temp_min_alarm:
+			return pvt_read_alarm(pvt, PVT_TEMP + ch, true, val);
+		case hwmon_temp_max_alarm:
+			return pvt_read_alarm(pvt, PVT_TEMP + ch, false, val);
+		case hwmon_temp_offset:
+			return pvt_read_trim(pvt, val);
+		}
+		break;
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+			return pvt_read_data(pvt, PVT_VOLT + ch, val);
+		case hwmon_in_min:
+			return pvt_read_limit(pvt, PVT_VOLT + ch, true, val);
+		case hwmon_in_max:
+			return pvt_read_limit(pvt, PVT_VOLT + ch, false, val);
+		case hwmon_in_min_alarm:
+			return pvt_read_alarm(pvt, PVT_VOLT + ch, true, val);
+		case hwmon_in_max_alarm:
+			return pvt_read_alarm(pvt, PVT_VOLT + ch, false, val);
+		}
+		break;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int pvt_hwmon_read_string(struct device *dev,
+				 enum hwmon_sensor_types type,
+				 u32 attr, int ch, const char **str)
+{
+	struct pvt_hwmon *pvt = dev_get_drvdata(dev);
+
+	if (!pvt_hwmon_channel_is_valid(type, ch))
+		return -EINVAL;
+
+	switch (type) {
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_label:
+			*str = pvt->info[PVT_TEMP + ch].label;
+			return 0;
+		}
+		break;
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_label:
+			*str = pvt->info[PVT_VOLT + ch].label;
+			return 0;
+		}
+		break;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int pvt_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
+			   u32 attr, int ch, long val)
+{
+	struct pvt_hwmon *pvt = dev_get_drvdata(dev);
+
+	if (!pvt_hwmon_channel_is_valid(type, ch))
+		return -EINVAL;
+
+	switch (type) {
+	case hwmon_chip:
+		switch (attr) {
+		case hwmon_chip_update_interval:
+			return pvt_write_timeout(pvt, val);
+		}
+		break;
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_min:
+			return pvt_write_limit(pvt, PVT_TEMP + ch, true, val);
+		case hwmon_temp_max:
+			return pvt_write_limit(pvt, PVT_TEMP + ch, false, val);
+		case hwmon_temp_offset:
+			return pvt_write_trim(pvt, val);
+		}
+		break;
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_min:
+			return pvt_write_limit(pvt, PVT_VOLT + ch, true, val);
+		case hwmon_in_max:
+			return pvt_write_limit(pvt, PVT_VOLT + ch, false, val);
+		}
+		break;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_ops pvt_hwmon_ops = {
+	.is_visible = pvt_hwmon_is_visible,
+	.read = pvt_hwmon_read,
+	.read_string = pvt_hwmon_read_string,
+	.write = pvt_hwmon_write
+};
+
+static struct hwmon_chip_info pvt_hwmon_info = {
+	.ops = &pvt_hwmon_ops,
+};
+
+static int pvt_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
+{
+	struct pvt_hwmon *pvt = thermal_zone_device_priv(tzd);
+	long t;
+	int err;
+
+	err = pvt_read_data(pvt, PVT_TEMP, &t);
+	if (err)
+		return err;
+
+	*temp = t;
+
+	return 0;
+}
+
+static int pvt_thermal_set_trips(struct thermal_zone_device *tzd,
+				 int low, int high)
+{
+	struct pvt_hwmon *pvt = thermal_zone_device_priv(tzd);
+	int err;
+
+	err = pvt_write_limit(pvt, PVT_TEMP, true, low);
+	if (err)
+		return err;
+	return pvt_write_limit(pvt, PVT_TEMP, false, high);
+}
+
+static struct thermal_zone_device_ops pvt_thermal_ops = {
+	.get_temp = pvt_thermal_get_temp,
+	.set_trips = pvt_thermal_set_trips,
+};
+
+static const struct thermal_trip pvt_trips[] = {
+	{
+		.type = THERMAL_TRIP_CRITICAL,
+		.temperature = PVT_THERMAL_CRIT,
+		.hysteresis = PVT_THERMAL_CRIT_HYST,
+	},
+};
+
+static void pvt_thermal_unregister(void *data)
+{
+	struct thermal_zone_device *tzd = data;
+
+	thermal_zone_device_disable(tzd);
+	thermal_zone_device_unregister(tzd);
+}
+
+static struct thermal_zone_device *pvt_thermal_register(struct pvt_hwmon *pvt)
+{
+	struct thermal_zone_device *tzd;
+	struct thermal_zone_params *tzp;
+	struct thermal_trip *trips;
+	int err;
+
+	trips = devm_kmemdup(pvt->dev, pvt_trips, sizeof(pvt_trips),
+			     GFP_KERNEL);
+	if (!trips)
+		return ERR_PTR(-ENOMEM);
+
+	tzp = devm_kzalloc(pvt->dev, sizeof(*tzp), GFP_KERNEL);
+	if (!tzp) {
+		devm_kfree(pvt->dev, trips);
+		return ERR_PTR(-ENOMEM);
+	}
+	tzp->no_hwmon = true;
+	tzp->slope = 1;
+	tzp->offset = 0;
+
+	tzd = thermal_zone_device_register_with_trips("pvt_thermal",
+		trips, ARRAY_SIZE(pvt_trips), pvt, &pvt_thermal_ops,
+		tzp, 0, PVT_THERMAL_POLLING_DELAY);
+	if (IS_ERR(tzd)) {
+		devm_kfree(pvt->dev, tzp);
+		devm_kfree(pvt->dev, trips);
+		return tzd;
+	}
+	err = thermal_zone_device_enable(tzd);
+	if (err)
+		goto out_unregister;
+	err = devm_add_action(pvt->dev, pvt_thermal_unregister, tzd);
+	if (err)
+		goto out_disable;
+	return tzd;
+
+out_disable:
+	thermal_zone_device_disable(tzd);
+out_unregister:
+	thermal_zone_device_unregister(tzd);
+	devm_kfree(pvt->dev, tzp);
+	devm_kfree(pvt->dev, trips);
+	return ERR_PTR(err);
+}
+
+static void pvt_clear_data(void *data)
+{
+	struct pvt_hwmon *pvt = data;
+#if !defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+	int idx;
+
+	for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
+		complete_all(&pvt->cache[idx].conversion);
+#endif
+
+	mutex_destroy(&pvt->iface_mtx);
+}
+
+static struct pvt_hwmon *pvt_create_data(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct pvt_hwmon *pvt;
+	int ret, idx;
+
+	pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
+	if (!pvt)
+		return ERR_PTR(-ENOMEM);
+
+	ret = devm_add_action(dev, pvt_clear_data, pvt);
+	if (ret) {
+		dev_err(dev, "Can't add PVT data clear action\n");
+		return ERR_PTR(ret);
+	}
+
+	pvt->dev = dev;
+	pvt->sensor = PVT_SENSOR_FIRST;
+	mutex_init(&pvt->iface_mtx);
+
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+	for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
+		seqlock_init(&pvt->cache[idx].data_seqlock);
+#else
+	for (idx = 0; idx < PVT_SENSORS_NUM; ++idx)
+		init_completion(&pvt->cache[idx].conversion);
+#endif
+
+	return pvt;
+}
+
+static int pvt_request_regs(struct pvt_hwmon *pvt)
+{
+	struct platform_device *pdev = to_platform_device(pvt->dev);
+
+	if (!pvt->regs) {
+		pvt->regs = devm_platform_ioremap_resource(pdev, 0);
+		if (IS_ERR(pvt->regs))
+			return PTR_ERR(pvt->regs);
+	}
+
+	return 0;
+}
+
+static void pvt_disable_clks(void *data)
+{
+	struct pvt_hwmon *pvt = data;
+
+	clk_bulk_disable_unprepare(PVT_CLOCK_NUM, pvt->clks);
+}
+
+static int pvt_request_clks(struct pvt_hwmon *pvt)
+{
+	int ret;
+
+	pvt->clks[PVT_CLOCK_APB].id = "pclk";
+	pvt->clks[PVT_CLOCK_REF].id = "ref";
+
+	ret = devm_clk_bulk_get_optional(pvt->dev, PVT_CLOCK_NUM, pvt->clks);
+	if (ret) {
+		dev_err(pvt->dev, "Couldn't get PVT clocks descriptors\n");
+		return ret;
+	}
+
+	ret = clk_bulk_prepare_enable(PVT_CLOCK_NUM, pvt->clks);
+	if (ret) {
+		dev_err(pvt->dev, "Couldn't enable the PVT clocks\n");
+		return ret;
+	}
+
+	ret = devm_add_action_or_reset(pvt->dev, pvt_disable_clks, pvt);
+	if (ret) {
+		dev_err(pvt->dev, "Can't add PVT clocks disable action\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int pvt_check_pwr(struct pvt_hwmon *pvt)
+{
+	unsigned long tout;
+	int ret = 0;
+	u32 data;
+
+	/*
+	 * Test out the sensor conversion functionality. If it is not done on
+	 * time then the domain must have been unpowered and we won't be able
+	 * to use the device later in this driver.
+	 * Note If the power source is lost during the normal driver work the
+	 * data read procedure will either return -ETIMEDOUT (for the
+	 * alarm-less driver configuration) or just stop the repeated
+	 * conversion. In the later case alas we won't be able to detect the
+	 * problem.
+	 */
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
+	pvt_set_tout(pvt, 0);
+	pvt->ops->read(pvt, PVT_DATA);
+
+	tout = PVT_TOUT_MIN / NSEC_PER_USEC;
+	usleep_range(tout, 2 * tout);
+
+	data = pvt->ops->read(pvt, PVT_DATA);
+	if (!(data & PVT_DATA_VALID)) {
+		ret = -ENODEV;
+		dev_err(pvt->dev, "Sensor is powered down\n");
+	}
+
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, 0);
+
+	return ret;
+}
+
+static int pvt_init_iface(struct pvt_hwmon *pvt)
+{
+	unsigned long rate;
+	u32 trim, temp, tout;
+
+	rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
+	if (!rate) {
+		dev_err(pvt->dev, "Invalid reference clock rate\n");
+		return -ENODEV;
+	}
+
+	/*
+	 * Make sure all interrupts and controller are disabled so not to
+	 * accidentally have ISR executed before the driver data is fully
+	 * initialized. Clear the IRQ status as well.
+	 */
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_ALL, PVT_INTR_ALL);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, 0);
+	pvt->ops->read(pvt, PVT_CLR_INTR);
+	pvt->ops->read(pvt, PVT_DATA);
+
+	/*
+	 * Preserve the current ref-clock based delay (Ttotal) between the
+	 * sensors data samples in the driver data so not to recalculate it
+	 * each time on the data requests and timeout reads. It consists of the
+	 * delay introduced by the internal ref-clock timer (N / Fclk) and the
+	 * constant timeout caused by each conversion latency (Tmin):
+	 *   Ttotal = N / Fclk + Tmin
+	 * If alarms are enabled the sensors are polled one after another and
+	 * in order to get the next measurement of a particular sensor the
+	 * caller will have to wait for at most until all the others are
+	 * polled. In that case the formulae will look a bit different:
+	 *   Ttotal = 5 * (N / Fclk + Tmin)
+	 */
+	pvt->timeout = ktime_set(0, PVT_TOUT_DEF);
+	tout = pvt_calc_tout(
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+		ktime_divns(pvt->timeout, PVT_SENSORS_NUM)
+#else
+		pvt->timeout
+#endif
+		, rate);
+
+	/* Setup default sensor mode, timeout and temperature trim. */
+	pvt_set_mode(pvt, pvt->info[pvt->sensor].mode);
+	pvt_set_tout(pvt, tout);
+
+	trim = PVT_TRIM_DEF;
+	if (!of_property_read_u32(pvt->dev->of_node,
+	     "baikal,pvt-temp-offset-millicelsius", &temp))
+		trim = pvt_calc_trim(temp);
+
+	pvt_set_trim(pvt, trim);
+
+	return 0;
+}
+
+static int pvt_create_hwmon(struct pvt_hwmon *pvt,
+			    const struct hwmon_channel_info * const *hwmon_info)
+{
+	pvt_hwmon_info.info = hwmon_info;
+	pvt->hwmon = devm_hwmon_device_register_with_info(pvt->dev, "pvt", pvt,
+		&pvt_hwmon_info, NULL);
+	if (IS_ERR(pvt->hwmon)) {
+		dev_err(pvt->dev, "Couldn't create hwmon device\n");
+		return PTR_ERR(pvt->hwmon);
+	}
+
+	if (__is_defined(CONFIG_THERMAL_OF) && acpi_disabled) {
+		pvt->tzd = devm_thermal_of_zone_register(pvt->dev, 0, pvt,
+							 &pvt_thermal_ops);
+		if (IS_ERR(pvt->tzd) && PTR_ERR(pvt->tzd) == -ENODEV) {
+			dev_info(pvt->dev,
+				"temp0_input not attached to any thermal zone\n");
+			return 0;
+		}
+	} else {
+		pvt->tzd = pvt_thermal_register(pvt);
+	}
+	if (IS_ERR(pvt->tzd)) {
+		dev_err(pvt->dev, "Couldn't register to thermal\n");
+		return PTR_ERR(pvt->tzd);
+	}
+
+	return 0;
+}
+
+static int pvt_request_irq(struct pvt_hwmon *pvt)
+{
+	struct platform_device *pdev = to_platform_device(pvt->dev);
+	int ret;
+
+	pvt->irq = platform_get_irq(pdev, 0);
+	if (pvt->irq < 0)
+		return pvt->irq;
+
+	ret = devm_request_threaded_irq(pvt->dev, pvt->irq,
+					pvt_hard_isr,
+					pvt_soft_isr,
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+					IRQF_SHARED |
+					IRQF_TRIGGER_HIGH |
+					IRQF_ONESHOT,
+#else
+					IRQF_SHARED |
+					IRQF_TRIGGER_HIGH,
+#endif
+					"pvt", pvt);
+	if (ret) {
+		dev_err(pvt->dev, "Couldn't request PVT IRQ\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+
+static void pvt_disable_iface(void *data)
+{
+	struct pvt_hwmon *pvt = data;
+
+	mutex_lock(&pvt->iface_mtx);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, 0);
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_DVALID, PVT_INTR_DVALID);
+	mutex_unlock(&pvt->iface_mtx);
+}
+
+static int pvt_enable_iface(struct pvt_hwmon *pvt)
+{
+	int ret;
+
+	ret = devm_add_action(pvt->dev, pvt_disable_iface, pvt);
+	if (ret) {
+		dev_err(pvt->dev, "Can't add PVT disable interface action\n");
+		return ret;
+	}
+
+	/*
+	 * Enable sensors data conversion and IRQ. We need to lock the
+	 * interface mutex since hwmon has just been created and the
+	 * corresponding sysfs files are accessible from user-space,
+	 * which theoretically may cause races.
+	 */
+	mutex_lock(&pvt->iface_mtx);
+	pvt_update(pvt, PVT_INTR_MASK, PVT_INTR_DVALID, 0);
+	pvt_update(pvt, PVT_CTRL, PVT_CTRL_EN, PVT_CTRL_EN);
+	mutex_unlock(&pvt->iface_mtx);
+
+	return 0;
+}
+
+#else /* !CONFIG_SENSORS_BAIKAL_PVT_ALARMS */
+
+static int pvt_enable_iface(struct pvt_hwmon *pvt)
+{
+	return 0;
+}
+
+#endif /* !CONFIG_SENSORS_BAIKAL_PVT_ALARMS */
+
+int baikal_pvt_create(struct platform_device *pdev, struct pvt_ops *ops,
+		      const struct pvt_sensor_info *info,
+		      const struct hwmon_channel_info * const *hwmon_info)
+{
+	struct pvt_hwmon *pvt;
+	int ret;
+
+	pvt = pvt_create_data(pdev);
+	if (IS_ERR(pvt))
+		return PTR_ERR(pvt);
+
+	pvt->ops = ops;
+	pvt->info = info;
+	platform_set_drvdata(pdev, pvt);
+
+	if (ops->init) {
+		ret = ops->init(pvt);
+		if (ret)
+			return ret;
+	}
+
+	ret = pvt_request_regs(pvt);
+	if (ret)
+		return ret;
+
+	ret = pvt_request_clks(pvt);
+	if (ret)
+		return ret;
+
+	ret = pvt_check_pwr(pvt);
+	if (ret)
+		return ret;
+
+	ret = pvt_init_iface(pvt);
+	if (ret)
+		return ret;
+
+	ret = pvt_create_hwmon(pvt, hwmon_info);
+	if (ret)
+		return ret;
+
+	ret = pvt_request_irq(pvt);
+	if (ret)
+		return ret;
+
+	ret = pvt_enable_iface(pvt);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL(baikal_pvt_create);
diff --git a/drivers/hwmon/baikal-pvt.h b/drivers/hwmon/baikal-pvt.h
new file mode 100644
index 00000000000000..ca1c786f872dfe
--- /dev/null
+++ b/drivers/hwmon/baikal-pvt.h
@@ -0,0 +1,273 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020-2025 BAIKAL ELECTRONICS, JSC
+ *
+ * Baikal SoCs Process, Voltage, Temperature sensor driver
+ */
+#ifndef __HWMON_BAIKAL_PVT_H__
+#define __HWMON_BAIKAL_PVT_H__
+
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/hwmon.h>
+#include <linux/kernel.h>
+#include <linux/ktime.h>
+#include <linux/mutex.h>
+#include <linux/sched.h>
+#include <linux/seqlock.h>
+#include <linux/thermal.h>
+
+/* Baikal SoC's PVT registers and their bitfields */
+#define PVT_CTRL			0x00
+#define PVT_CTRL_EN			BIT(0)
+#define PVT_CTRL_MODE_FLD		1
+#define PVT_CTRL_MODE_MASK		GENMASK(3, PVT_CTRL_MODE_FLD)
+#define PVT_CTRL_MODE_TEMP		0x0
+#define PVT_CTRL_MODE_VOLT		0x1
+#define PVT_CTRL_MODE_LVT		0x2
+#define PVT_CTRL_MODE_HVT		0x4
+#define PVT_CTRL_MODE_ULVT		PVT_CTRL_MODE_HVT
+#define PVT_CTRL_MODE_SVT		0x6
+#define PVT_CTRL_TRIM_FLD		4
+#define PVT_CTRL_TRIM_MASK		GENMASK(8, PVT_CTRL_TRIM_FLD)
+#define PVT_DATA			0x04
+#define PVT_DATA_VALID			BIT(10)
+#define PVT_DATA_DATA_FLD		0
+#define PVT_DATA_DATA_MASK		GENMASK(9, PVT_DATA_DATA_FLD)
+#define PVT_TTHRES			0x08
+#define PVT_VTHRES			0x0C
+#define PVT_LTHRES			0x10
+#define PVT_HTHRES			0x14
+#define PVT_UTHRES			PVT_HTHRES
+#define PVT_STHRES			0x18
+#define PVT_THRES_LO_FLD		0
+#define PVT_THRES_LO_MASK		GENMASK(9, PVT_THRES_LO_FLD)
+#define PVT_THRES_HI_FLD		10
+#define PVT_THRES_HI_MASK		GENMASK(19, PVT_THRES_HI_FLD)
+#define PVT_TTIMEOUT			0x1C
+#define PVT_INTR_STAT			0x20
+#define PVT_INTR_MASK			0x24
+#define PVT_RAW_INTR_STAT		0x28
+#define PVT_INTR_DVALID			BIT(0)
+#define PVT_INTR_TTHRES_LO		BIT(1)
+#define PVT_INTR_TTHRES_HI		BIT(2)
+#define PVT_INTR_VTHRES_LO		BIT(3)
+#define PVT_INTR_VTHRES_HI		BIT(4)
+#define PVT_INTR_LTHRES_LO		BIT(5)
+#define PVT_INTR_LTHRES_HI		BIT(6)
+#define PVT_INTR_HTHRES_LO		BIT(7)
+#define PVT_INTR_HTHRES_HI		BIT(8)
+#define PVT_INTR_UTHRES_LO		PVT_INTR_HTHRES_LO
+#define PVT_INTR_UTHRES_HI		PVT_INTR_HTHRES_HI
+#define PVT_INTR_STHRES_LO		BIT(9)
+#define PVT_INTR_STHRES_HI		BIT(10)
+#define PVT_INTR_ALL			GENMASK(10, 0)
+#define PVT_CLR_INTR			0x2C
+
+/*
+ * PVT sensors-related limits and default values
+ * @PVT_TEMP_MIN: Minimal temperature in millidegrees of Celsius.
+ * @PVT_TEMP_MAX: Maximal temperature in millidegrees of Celsius.
+ * @PVT_TEMP_CHS: Number of temperature hwmon channels.
+ * @PVT_VOLT_MIN: Minimal voltage in mV.
+ * @PVT_VOLT_MAX: Maximal voltage in mV.
+ * @PVT_VOLT_CHS: Number of voltage hwmon channels.
+ * @PVT_DATA_MIN: Minimal PVT raw data value.
+ * @PVT_DATA_MAX: Maximal PVT raw data value.
+ * @PVT_TRIM_MIN: Minimal temperature sensor trim value.
+ * @PVT_TRIM_MAX: Maximal temperature sensor trim value.
+ * @PVT_TRIM_DEF: Default temperature sensor trim value (set a proper value
+ *		  when one is determined for Baikal SoC).
+ * @PVT_TRIM_TEMP: Maximum temperature encoded by the trim factor.
+ * @PVT_TRIM_STEP: Temperature stride corresponding to the trim value.
+ * @PVT_TOUT_MIN: Minimal timeout between samples in nanoseconds.
+ * @PVT_TOUT_DEF: Default data measurements timeout in nanoseconds.
+ *		  In case if alarms are activated the PVT IRQ is enabled to be
+ *		  raised after each conversion in order to have the thresholds
+ *		  checked and the converted value cached. Too frequent
+ *		  conversions may cause the system CPU overload. Lets set the
+ *		  50ms delay between them by default to prevent this.
+ */
+#define PVT_TEMP_MIN		-48380L
+#define PVT_TEMP_MAX		147438L
+#define PVT_TEMP_CHS		1
+#define PVT_VOLT_MIN		620L
+#define PVT_VOLT_MAX		1168L
+#define PVT_LVT_MIN		PVT_VOLT_MIN
+#define PVT_LVT_MAX		PVT_VOLT_MAX
+#define PVT_HVT_MIN		PVT_VOLT_MIN
+#define PVT_HVT_MAX		PVT_VOLT_MAX
+#define PVT_ULVT_MIN		PVT_VOLT_MIN
+#define PVT_ULVT_MAX		PVT_VOLT_MAX
+#define PVT_SVT_MIN		PVT_VOLT_MIN
+#define PVT_SVT_MAX		PVT_VOLT_MAX
+#define PVT_VOLT_CHS		4
+#define PVT_DATA_MIN		0
+#define PVT_DATA_MAX		(PVT_DATA_DATA_MASK >> PVT_DATA_DATA_FLD)
+#define PVT_TRIM_MIN		0
+#define PVT_TRIM_MAX		(PVT_CTRL_TRIM_MASK >> PVT_CTRL_TRIM_FLD)
+#define PVT_TRIM_TEMP		7130
+#define PVT_TRIM_STEP		(PVT_TRIM_TEMP / PVT_TRIM_MAX)
+#define PVT_TRIM_DEF		0
+#define PVT_TOUT_MIN		(NSEC_PER_SEC / 3000)
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+# define PVT_TOUT_DEF		250000000
+#else
+# define PVT_TOUT_DEF		PVT_TOUT_MIN
+#endif
+
+/*
+ * enum pvt_sensor_type - Baikal SoC PVT sensor types (correspond to each PVT
+ *			  sampling mode)
+ * @PVT_SENSOR*: helpers to traverse the sensors in loops.
+ * @PVT_TEMP: PVT Temperature sensor.
+ * @PVT_VOLT: PVT Voltage sensor.
+ * @PVT_LVT: PVT Low-Voltage threshold sensor.
+ * @PVT_HVT: PVT High-Voltage threshold sensor.
+ * @PVT_ULVT: PVT Ultra-Low-Voltage threshold sensor.
+ * @PVT_SVT: PVT Standard-Voltage threshold sensor.
+ */
+enum pvt_sensor_type {
+	PVT_SENSOR_FIRST,
+	PVT_TEMP = PVT_SENSOR_FIRST,
+	PVT_VOLT,
+	PVT_LVT,
+	PVT_HVT,
+	PVT_ULVT = PVT_HVT,
+	PVT_SVT,
+	PVT_SENSOR_LAST = PVT_SVT,
+	PVT_SENSORS_NUM
+};
+
+/*
+ * enum pvt_clock_type - Baikal SoC PVT clocks.
+ * @PVT_CLOCK_APB: APB clock.
+ * @PVT_CLOCK_REF: PVT reference clock.
+ */
+enum pvt_clock_type {
+	PVT_CLOCK_APB,
+	PVT_CLOCK_REF,
+	PVT_CLOCK_NUM
+};
+
+/*
+ * struct pvt_sensor_info - Baikal SoC PVT sensor informational structure
+ * @channel: Sensor channel ID.
+ * @label: hwmon sensor label.
+ * @mode: PVT mode corresponding to the channel.
+ * @thres_base: upper and lower threshold values of the sensor.
+ * @thres_sts_lo: low threshold status bitfield.
+ * @thres_sts_hi: high threshold status bitfield.
+ * @type: Sensor type.
+ * @value_min: minimal sensor value.
+ * @value_max: maximal sensor value.
+ * @attr_min_alarm: Min alarm attribute ID.
+ * @attr_min_alarm: Max alarm attribute ID.
+ */
+struct pvt_sensor_info {
+	int channel;
+	const char *label;
+	u32 mode;
+	unsigned long thres_base;
+	u32 thres_sts_lo;
+	u32 thres_sts_hi;
+	enum hwmon_sensor_types type;
+	long value_min;
+	long value_max;
+	u32 attr_min_alarm;
+	u32 attr_max_alarm;
+};
+
+#define PVT_SENSOR_INFO(_ch, _label, _type, _mode, _thres)	\
+	{							\
+		.channel = _ch,					\
+		.label = _label,				\
+		.mode = PVT_CTRL_MODE_ ##_mode,			\
+		.thres_base = PVT_ ##_thres,			\
+		.thres_sts_lo = PVT_INTR_ ##_thres## _LO,	\
+		.thres_sts_hi = PVT_INTR_ ##_thres## _HI,	\
+		.type = _type,					\
+		.value_min = PVT_ ##_mode## _MIN,		\
+		.value_max = PVT_ ##_mode## _MAX,		\
+		.attr_min_alarm = _type## _min_alarm,		\
+		.attr_max_alarm = _type## _max_alarm,		\
+	}
+
+/*
+ * struct pvt_cache - PVT sensors data cache
+ * @data: data cache in raw format.
+ * @thres_sts_lo: low threshold status saved on the previous data conversion.
+ * @thres_sts_hi: high threshold status saved on the previous data conversion.
+ * @data_seqlock: cached data seq-lock.
+ * @conversion: data conversion completion.
+ */
+struct pvt_cache {
+	u32 data;
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+	seqlock_t data_seqlock;
+	u32 thres_sts_lo;
+	u32 thres_sts_hi;
+#else
+	struct completion conversion;
+#endif
+};
+
+struct pvt_hwmon;
+
+/*
+ * struct pvt_ops - PVT sensor operations
+ * @read_pvt: read PVT register.
+ * @write_pvt: write PVT register.
+ * @to_pvt: convert temperature/voltage to PVT data.
+ * @from_pvt: convert PVT data to temperature/voltage.
+ */
+struct pvt_ops {
+	int  (*init)(struct pvt_hwmon *);
+	u32  (*read)(struct pvt_hwmon *, u32);
+	int  (*write)(struct pvt_hwmon *, u32, u32);
+	u32  (*to_pvt)(struct pvt_hwmon *, enum pvt_sensor_type, long);
+	long (*from_pvt)(struct pvt_hwmon *, enum pvt_sensor_type, u32);
+};
+
+/*
+ * struct pvt_hwmon - Baikal SoC PVT private data
+ * @dev: device structure of the PVT platform device.
+ * @hwmon: hwmon device structure.
+ * @regs: pointer to the Baikal SoC PVT registers region.
+ * @irq: PVT events IRQ number.
+ * @clks: Array of the PVT clocks descriptor (APB/ref clocks).
+ * @iface_mtx: Generic interface mutex (used to lock the alarm registers
+ *	       when the alarms enabled, or the data conversion interface
+ *	       if alarms are disabled).
+ * @sensor: current PVT sensor the data conversion is being performed for.
+ * @cache: data cache descriptor.
+ * @timeout: conversion timeout cache.
+ * @ops: PVT operations.
+ * @tzd: thermal zone.
+ */
+struct pvt_hwmon {
+	struct device *dev;
+	struct device *hwmon;
+
+	void __iomem *regs;
+	int irq;
+
+	struct clk_bulk_data clks[PVT_CLOCK_NUM];
+
+	struct mutex iface_mtx;
+	enum pvt_sensor_type sensor;
+	struct pvt_cache cache[PVT_SENSORS_NUM];
+	ktime_t timeout;
+
+	struct pvt_ops *ops;
+
+	const struct pvt_sensor_info *info;
+
+	struct thermal_zone_device *tzd;
+};
+
+int baikal_pvt_create(struct platform_device *pdev, struct pvt_ops *ops,
+		      const struct pvt_sensor_info *info,
+		      const struct hwmon_channel_info * const *hwmon_info);
+
+#endif /* __HWMON_BAIKAL_PVT_H__ */
diff --git a/drivers/hwmon/bm1000-pvt-hwmon.c b/drivers/hwmon/bm1000-pvt-hwmon.c
new file mode 100644
index 00000000000000..5b955da5996972
--- /dev/null
+++ b/drivers/hwmon/bm1000-pvt-hwmon.c
@@ -0,0 +1,201 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020-2025 BAIKAL ELECTRONICS, JSC
+ *
+ * Authors:
+ *   Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>
+ *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
+ *
+ * Baikal BE-M1000 SoC Process, Voltage, Temperature sensor driver.
+ */
+
+#include <linux/firmware/baikal/baikal-smc.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/polynomial.h>
+
+#include "baikal-pvt.h"
+
+#define PVT_READ		0
+#define PVT_WRITE		1
+
+/*
+ * For the sake of the code simplification we created the sensors info table
+ * with the sensor names, activation modes, threshold registers base address
+ * and the thresholds bit fields.
+ */
+static const struct pvt_sensor_info pvt_info[] = {
+	PVT_SENSOR_INFO(0, "CPU Core Temperature", hwmon_temp, TEMP, TTHRES),
+	PVT_SENSOR_INFO(0, "CPU Core Voltage", hwmon_in, VOLT, VTHRES),
+	PVT_SENSOR_INFO(1, "CPU Core Low-Vt", hwmon_in, LVT, LTHRES),
+	PVT_SENSOR_INFO(2, "CPU Core High-Vt", hwmon_in, HVT, HTHRES),
+	PVT_SENSOR_INFO(3, "CPU Core Standard-Vt", hwmon_in, SVT, STHRES),
+};
+
+static const struct hwmon_channel_info * const pvt_channel_info[] = {
+	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_TYPE | HWMON_T_LABEL |
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+			   HWMON_T_MIN | HWMON_T_MIN_ALARM |
+			   HWMON_T_MAX | HWMON_T_MAX_ALARM |
+#endif
+			   HWMON_T_OFFSET),
+	HWMON_CHANNEL_INFO(in,
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+			   HWMON_I_MIN | HWMON_I_MIN_ALARM |
+			   HWMON_I_MAX | HWMON_I_MAX_ALARM |
+#endif
+			   HWMON_I_INPUT | HWMON_I_LABEL,
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+			   HWMON_I_MIN | HWMON_I_MIN_ALARM |
+			   HWMON_I_MAX | HWMON_I_MAX_ALARM |
+#endif
+			   HWMON_I_INPUT | HWMON_I_LABEL,
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+			   HWMON_I_MIN | HWMON_I_MIN_ALARM |
+			   HWMON_I_MAX | HWMON_I_MAX_ALARM |
+#endif
+			   HWMON_I_INPUT | HWMON_I_LABEL,
+#if defined(CONFIG_SENSORS_BAIKAL_PVT_ALARMS)
+			   HWMON_I_MIN | HWMON_I_MIN_ALARM |
+			   HWMON_I_MAX | HWMON_I_MAX_ALARM |
+#endif
+			   HWMON_I_INPUT | HWMON_I_LABEL),
+	NULL
+};
+
+static const struct polynomial bm1000_poly_temp_to_N = {
+	.total_divider = 10000,
+	.terms = {
+		{4,   18322, 10000, 10000},
+		{3,    2343, 10000,    10},
+		{2,   87018, 10000,    10},
+		{1,   39269,  1000,     1},
+		{0, 1720400,     1,     1}
+	}
+};
+
+static const struct polynomial bm1000_poly_N_to_temp = {
+	.total_divider = 1,
+	.terms = {
+		{4,  -16743, 1000, 1},
+		{3,   81542, 1000, 1},
+		{2, -182010, 1000, 1},
+		{1,  310200, 1000, 1},
+		{0,  -48380,    1, 1}
+	}
+};
+
+static const struct polynomial bm1000_poly_volt_to_N = {
+	.total_divider = 10,
+	.terms = {
+		{1,  18658, 1000, 1},
+		{0, -11572,    1, 1}
+	}
+};
+
+static const struct polynomial bm1000_poly_N_to_volt = {
+	.total_divider = 10,
+	.terms = {
+		{1,    100000, 18658,     1},
+		{0, 115720000,     1, 18658}
+	}
+};
+
+static const struct baikal_pvt_data {
+	const struct polynomial *temp_to_N;
+	const struct polynomial *N_to_temp;
+	const struct polynomial *volt_to_N;
+	const struct polynomial *N_to_volt;
+} baikal_pvt = {
+	.temp_to_N = &bm1000_poly_temp_to_N,
+	.N_to_temp = &bm1000_poly_N_to_temp,
+	.volt_to_N = &bm1000_poly_volt_to_N,
+	.N_to_volt = &bm1000_poly_N_to_volt
+};
+
+static int baikal_init_pvt(struct pvt_hwmon *pvt)
+{
+	struct platform_device *pdev = to_platform_device(pvt->dev);
+	struct resource *res;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -ENODEV;
+	pvt->regs = (void __iomem *)res->start;
+
+	return 0;
+}
+
+static u32 baikal_read_pvt(struct pvt_hwmon *pvt, u32 reg)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_smc(BAIKAL_SMC_PVT_CMD, PVT_READ, (unsigned long)pvt->regs,
+		      reg, 0, 0, 0, 0, &res);
+
+	return res.a0;
+}
+
+static int baikal_write_pvt(struct pvt_hwmon *pvt, u32 reg, u32 data)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_smc(BAIKAL_SMC_PVT_CMD, PVT_WRITE, (unsigned long)pvt->regs,
+		      reg, data, 0, 0, 0, &res);
+	return res.a0;
+}
+
+static u32 baikal_to_pvt(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			 long val)
+{
+	const struct polynomial *poly = (type == PVT_TEMP) ?
+					baikal_pvt.temp_to_N :
+					baikal_pvt.volt_to_N;
+
+	return polynomial_calc(poly, val);
+}
+
+static long baikal_from_pvt(struct pvt_hwmon *pvt, enum pvt_sensor_type type,
+			    u32 data)
+{
+	const struct polynomial *poly = (type == PVT_TEMP) ?
+					baikal_pvt.N_to_temp :
+					baikal_pvt.N_to_volt;
+
+	return polynomial_calc(poly, data);
+}
+
+static struct pvt_ops baikal_pvt_ops = {
+	.init     = baikal_init_pvt,
+	.read     = baikal_read_pvt,
+	.write    = baikal_write_pvt,
+	.to_pvt   = baikal_to_pvt,
+	.from_pvt = baikal_from_pvt,
+};
+
+static int pvt_probe(struct platform_device *pdev)
+{
+	return baikal_pvt_create(pdev, &baikal_pvt_ops, pvt_info, pvt_channel_info);
+}
+
+static const struct of_device_id pvt_of_match[] = {
+	{ .compatible = "baikal,bm1000-pvt" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, pvt_of_match);
+
+static struct platform_driver pvt_driver = {
+	.probe  = pvt_probe,
+	.driver = {
+		.name = "bm1000-pvt",
+		.of_match_table = pvt_of_match
+	}
+};
+module_platform_driver(pvt_driver);
+
+MODULE_AUTHOR("Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru>");
+MODULE_DESCRIPTION("Baikal BE-M1000 SoC PVT driver");
+MODULE_LICENSE("GPL v2");
-- 
2.42.2



  parent reply	other threads:[~2026-02-27 10:32 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-27 10:32 [d-kernel] [PATCH 00/35] Kernel 6.18 with support for the Baikal-M SoC Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 01/35] Baikal Electronics SoC family Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 02/35] clk: Add clock drivers for Baikal BE-M1000 Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 03/35] clk: baikal-m: old firmware: use "cmu-id" if there is no "reg" in devicetree Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 04/35] usb: add support for Baikal USB PHY Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 05/35] usb: dwc3: of-simple: added compatible string for Baikal-M SoC Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 06/35] uart: add support for UART Baikal BE-M1000 Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 07/35] serial: 8250_dw: verify clock rate in dw8250_set_termios Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 08/35] cpufreq-dt: don't load on Baikal-M SoC Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 09/35] net: stmmac: support of Baikal-BE1000 SoCs GMAC Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 10/35] net: fwnode_get_phy_id: consider all compatible strings Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 11/35] drm/panfrost: forcibly set dma-coherent on Baikal-M Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 12/35] drm/panfrost: disable devfreq " Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 13/35] ata: ahci: add support for Baikal BE-M1000 Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 14/35] drm: add Baikal-M SoC video display unit driver Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 15/35] drm: baikal-vdu: driver compatibility with SDK earlier than 5.9 Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 16/35] drm: baikal-vdu: disable backlight driver loading Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 17/35] sound: add support for Baikal BE-M1000 I2S Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 18/35] sound: dwc-i2s: request all IRQs specified in device tree Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 19/35] sound: dwc-i2s: paper over RX overrun warnings on Baikal-M Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 20/35] sound: baikal-i2s: " Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 21/35] drm/bridge: dw-hdmi: support ahb audio hw revision 0x2a Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 22/35] dt-bindings: dw-hdmi: added ahb-audio-regshift Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 23/35] drm/bridge: dw-hdmi: force ahb audio register offset for Baikal-M Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 24/35] dw-hdmi: add flag SNDRV_PCM_INFO_BATCH for audio via hdmi on Baikal-M Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 25/35] bmc: add board management controller driver Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 26/35] pm: disable all sleep states on Baikal-M based boards Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 27/35] sound: hda: add driver for HDA controller on Baikal-M Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 28/35] sound: hda: enable jack detection in polling mode " Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 29/35] input: new driver - serdev-serio Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 30/35] input: serio: add an alias to the sersev-serio driver Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 31/35] input: added TF307 serio PS/2 emulator driver Daniil Gnusarev
2026-02-27 10:32 ` Daniil Gnusarev [this message]
2026-02-27 10:32 ` [d-kernel] [PATCH 33/35] hwmon: baikal-pvt: support work on machines with old firmware Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 34/35] dw-pcie: refuse to load on Baikal-M with recent firmware Daniil Gnusarev
2026-02-27 10:32 ` [d-kernel] [PATCH 35/35] config-aarch64: enable more configs for Baikal-M support Daniil Gnusarev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260227103236.785736-33-gnusarevda@basealt.ru \
    --to=gnusarevda@basealt.ru \
    --cc=devel-kernel@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux kernel packages development

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel-kernel/0 devel-kernel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel-kernel devel-kernel/ http://lore.altlinux.org/devel-kernel \
		devel-kernel@altlinux.org devel-kernel@altlinux.ru devel-kernel@altlinux.com
	public-inbox-index devel-kernel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git