Files
nixos-hardware/spacemit/k3-pico-itx/patches/0012-thermal-spacemit-k3.patch
T

471 lines
12 KiB
Diff

From: liberodark <liberodark@gmail.com>
Subject: [PATCH] thermal: spacemit: k3: tsensor support (v2)
Port of the SpacemiT K3 thermal sensor driver with devicetree zones
(top/vpu/gpu/cluster, critical trip 115C).
v2: review fixes :
- switch both clocks to devm_clk_get_enabled() (enable results were
ignored and the clocks leaked on every probe error path)
- check the sensor descriptor allocation and the init_sensors()
return (required DT properties: on failure the zone loop iterated
over an undefined sensor range)
- check reset_control_deassert() and assert the reset through a devm
action so teardown runs in the correct order (zones unregistered
before clocks/reset), replacing the mis-ordered remove() callback
- drop the commented-out IRQ block and reduce the debug bit-toggle
sequence to the plain register clear it amounted to
Signed-off-by: liberodark <liberodark@gmail.com>
---
--- a/arch/riscv/boot/dts/spacemit/k3.dtsi
+++ b/arch/riscv/boot/dts/spacemit/k3.dtsi
@@ -16,6 +16,64 @@
model = "SpacemiT K3";
compatible = "spacemit,k3";
+ thermal-zones {
+ top-thermal {
+ polling-delay = <1000>;
+ polling-delay-passive = <500>;
+ thermal-sensors = <&tsensor 0>;
+
+ trips {
+ top-crit {
+ temperature = <115000>;
+ hysteresis = <2000>;
+ type = "critical";
+ };
+ };
+ };
+
+ vpu-thermal {
+ polling-delay = <1000>;
+ polling-delay-passive = <500>;
+ thermal-sensors = <&tsensor 2>;
+
+ trips {
+ vpu-crit {
+ temperature = <115000>;
+ hysteresis = <2000>;
+ type = "critical";
+ };
+ };
+ };
+
+ gpu-thermal {
+ polling-delay = <1000>;
+ polling-delay-passive = <500>;
+ thermal-sensors = <&tsensor 3>;
+
+ trips {
+ gpu-crit {
+ temperature = <115000>;
+ hysteresis = <2000>;
+ type = "critical";
+ };
+ };
+ };
+
+ cluster-thermal {
+ polling-delay = <1000>;
+ polling-delay-passive = <500>;
+ thermal-sensors = <&tsensor 4>;
+
+ trips {
+ cluster-crit {
+ temperature = <115000>;
+ hysteresis = <2000>;
+ type = "critical";
+ };
+ };
+ };
+ };
+
reserved-memory {
#address-cells = <2>;
#size-cells = <2>;
@@ -784,6 +842,22 @@
#reset-cells = <1>;
};
+ tsensor: thermal-sensor@d4018000 {
+ compatible = "spacemit,k3-tsensor";
+ reg = <0x0 0xd4018000 0x0 0x100>;
+ interrupt-parent = <&saplic>;
+ interrupts = <61 IRQ_TYPE_LEVEL_HIGH>;
+ interrupt-names = "tsensor";
+ clocks = <&syscon_apbc CLK_APBC_TSEN>,
+ <&syscon_apbc CLK_APBC_TSEN_BUS>;
+ clock-names = "func", "bus";
+ resets = <&syscon_apbc RESET_APBC_TSEN>;
+ sensor_range = <0x0 0x7>;
+ tsensor_map = <1 0 1 1 1 1 1 1>;
+ temperature_offset = <274>;
+ #thermal-sensor-cells = <1>;
+ };
+
uart0: serial@d4017000 {
compatible = "spacemit,k3-uart", "intel,xscale-uart";
reg = <0x0 0xd4017000 0x0 0x100>;
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -306,6 +306,15 @@
threshold values. The direction of the comparison is configurable
(greater / lesser than).
+config SPACEMIT_K3_THERMAL
+ tristate "SpacemiT K3 thermal sensor support"
+ depends on ARCH_SPACEMIT || COMPILE_TEST
+ depends on OF && HAS_IOMEM
+ help
+ Enable this to support the thermal sensor (tsensor) block on
+ the SpacemiT K3 SoC. It exposes on-die temperature sensors as
+ thermal zones and hwmon devices.
+
config K3_THERMAL
tristate "Texas Instruments K3 thermal support"
depends on ARCH_K3 || COMPILE_TEST
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -35,6 +35,7 @@
thermal_sys-$(CONFIG_PCIE_THERMAL) += pcie_cooling.o
obj-$(CONFIG_K3_THERMAL) += k3_bandgap.o k3_j72xx_bandgap.o
+obj-$(CONFIG_SPACEMIT_K3_THERMAL) += spacemit-k3-thermal.o
# platform thermal drivers
obj-y += broadcom/
obj-$(CONFIG_THERMAL_MMIO) += thermal_mmio.o
--- /dev/null
+++ b/drivers/thermal/spacemit-k3-thermal.c
@@ -0,0 +1,268 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/cpufreq.h>
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/io.h>
+#include <linux/of_device.h>
+#include <linux/thermal.h>
+#include <linux/reset.h>
+#include "thermal_hwmon.h"
+#include "thermal_core.h"
+#include "spacemit-k3-thermal.h"
+
+static int init_sensors(struct platform_device *pdev)
+{
+ int ret;
+ unsigned int val;
+ struct k3_thermal_sensor *s = platform_get_drvdata(pdev);
+
+ /* read the sensor range */
+ ret = of_property_read_u32_array(pdev->dev.of_node, "sensor_range", s->sr, 2);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "get sensor range error\n");
+ return ret;
+ }
+
+ if (s->sr[1] >= MAX_SENSOR_NUMBER) {
+ dev_err(&pdev->dev, "un-fitable sensor range\n");
+ return -EINVAL;
+ }
+
+ ret = of_property_read_u32_array(pdev->dev.of_node, "tsensor_map",
+ s->tsen_enable_map, s->sr[1] - s->sr[0] + 1);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to get definition of tsensor_map\n");
+ return -EINVAL;
+ }
+
+ /* first: disable all the interrupts */
+ writel(0xffffffff, s->base + REG_TSEN_LITE_INT_CLR);
+ writel(0xffffffff, s->base + REG_TSEN_LITE_INT_ENB);
+
+ /* select clk div 26M/4 */
+ val = readl(s->base + REG_TSEN_LITE_CFG);
+ val &= ~BITS_D_CK_DIV_SEL;
+ val |= BITS_CK_DIV_SEL_DIV4;
+
+ /* vref calibration */
+ val &= ~BITS_D_REG_VREF_CTRL;
+ val |= ((CALIB_VREF_DEFAULT & 0xff) << BITS_D_REG_VREF_OFFSET);
+
+ writel(val, s->base + REG_TSEN_LITE_CFG);
+
+ return 0;
+}
+
+static void enable_sensors(struct platform_device *pdev)
+{
+ struct k3_thermal_sensor *s = platform_get_drvdata(pdev);
+
+ writel(readl(s->base + REG_TSEN_LITE_CFG) | BIT_TSEN_EN,
+ s->base + REG_TSEN_LITE_CFG);
+}
+
+static int k3_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
+{
+ struct k3_thermal_sensor_desc *desc = (struct k3_thermal_sensor_desc *)tz->devdata;
+
+ mutex_lock(&desc->ks->lock);
+ /* select which sensor */
+ writel(desc->index, desc->base + REG_TSEN_LITE_CFG2);
+ msleep(1);
+ *temp = readl(desc->base + REG_TSEN_LITE_TEMP_DATA);
+ mutex_unlock(&desc->ks->lock);
+
+ *temp &= BITS_TEMP_DATA;
+ *temp /= TEMP_RAW_DATA_DIV;
+
+ *temp -= desc->temp_offset;
+
+ *temp *= 1000;
+
+ return 0;
+}
+
+/**
+ * static int k3_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
+ * {
+ * int index;
+ * unsigned int temp;
+ * int over_thrsh = high;
+ * int under_thrsh = low;
+ * struct k3_thermal_sensor_desc *desc = (struct k3_thermal_sensor_desc *)tz->devdata;
+ *
+ * index = desc - desc;
+ * writel(index, desc->base + REG_TSEN_LITE_CFG2);
+ *
+ * over_thrsh /= 1000;
+ * over_thrsh += desc->temp_offset;
+ * over_thrsh *= TEMP_RAW_DATA_DIV;
+ *
+ * temp = readl(desc->base + REG_TSEN_LITE_TEMP_THRESH);
+ * temp &= ~0xffff0000;
+ * temp |= (over_thrsh << TSEN_HIGH_THRESH_OFFSET);
+ * writel(temp, desc->base + REG_TSEN_LITE_TEMP_THRESH);
+ *
+ * if (low < 0)
+ * under_thrsh = 0;
+ *
+ * under_thrsh /= 1000;
+ * under_thrsh += desc->temp_offset;
+ * under_thrsh *= TEMP_RAW_DATA_DIV;
+ * temp = readl(desc->base + REG_TSEN_LITE_TEMP_THRESH);
+ * temp &= ~0xffff;
+ * temp |= (under_thrsh << TSEN_LOW_THRESH_OFFSET);
+ * writel(temp, desc->base + REG_TSEN_LITE_TEMP_THRESH);
+ *
+ * return 0;
+ * }
+ */
+static const struct thermal_zone_device_ops k3_of_thermal_ops = {
+ .get_temp = k3_thermal_get_temp,
+ /* .set_trips = k3_thermal_set_trips, */
+};
+
+/**
+ * static irqreturn_t k3_thermal_irq_thread(int irq, void *data)
+ * {
+ * struct k3_thermal_sensor_desc *desc = (struct k3_thermal_sensor_desc *)data;
+ *
+ * thermal_zone_device_update(desc->tzd, THERMAL_EVENT_UNSPECIFIED);
+ *
+ * return IRQ_HANDLED;
+ * }
+ */
+
+static void k3_thermal_reset_assert(void *data)
+{
+ reset_control_assert(data);
+}
+
+static int k3_thermal_probe(struct platform_device *pdev)
+{
+ unsigned int value;
+ int ret, i;
+ struct resource *res;
+ struct k3_thermal_sensor *s;
+ struct device *dev = &pdev->dev;
+
+ s = devm_kzalloc(dev, sizeof(*s), GFP_KERNEL);
+ if (!s)
+ return -ENOMEM;
+
+ s->dev = dev;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ s->base = devm_ioremap_resource(dev, res);
+ if (IS_ERR(s->base))
+ return PTR_ERR(s->base);
+
+ ret = of_property_read_u32(dev->of_node, "temperature_offset", &s->temp_offset);
+ if (ret) {
+ dev_err(dev, "Get temperature_offset failed\n");
+ return -EINVAL;
+ }
+
+ s->resets = devm_reset_control_get_optional(&pdev->dev, NULL);
+ if (IS_ERR(s->resets))
+ return PTR_ERR(s->resets);
+
+ ret = reset_control_deassert(s->resets);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, k3_thermal_reset_assert, s->resets);
+ if (ret)
+ return ret;
+
+ /*
+ * devm teardown runs in reverse order: the thermal zones are
+ * unregistered before the clocks are disabled and the reset is
+ * asserted, so a late poll can never hit dead hardware.
+ */
+ s->fclk = devm_clk_get_enabled(dev, "func");
+ if (IS_ERR(s->fclk))
+ return PTR_ERR(s->fclk);
+
+ s->bclk = devm_clk_get_enabled(dev, "bus");
+ if (IS_ERR(s->bclk))
+ return PTR_ERR(s->bclk);
+
+ mutex_init(&s->lock);
+
+ s->sdesc = devm_kcalloc(dev, MAX_SENSOR_NUMBER,
+ sizeof(struct k3_thermal_sensor_desc),
+ GFP_KERNEL);
+ if (!s->sdesc)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, s);
+
+ /* initialize the sensors */
+ ret = init_sensors(pdev);
+ if (ret)
+ return ret;
+
+ /* enable the sensors & using auto mode */
+ enable_sensors(pdev);
+
+ /* clear d_en_autozero[25] and d_out_sel[26] */
+ value = readl(s->base);
+ value &= ~((1 << 25) | (1 << 26));
+ writel(value, s->base);
+
+ /* then register the thermal zone */
+ for (i = s->sr[0]; i <= s->sr[1]; ++i) {
+ if (s->tsen_enable_map[i] == 0)
+ continue;
+
+ s->sdesc[i].base = s->base;
+ s->sdesc[i].index = i;
+ s->sdesc[i].ks = s;
+ s->sdesc[i].temp_offset = s->temp_offset;
+ s->sdesc[i].tzd = devm_thermal_of_zone_register(dev,
+ i, s->sdesc + i, &k3_of_thermal_ops);
+ if (IS_ERR(s->sdesc[i].tzd)) {
+ ret = PTR_ERR(s->sdesc[i].tzd);
+ /*
+ * No thermal zone described in DT for this sensor:
+ * skip it instead of aborting the whole probe.
+ */
+ if (ret == -ENODEV) {
+ s->sdesc[i].tzd = NULL;
+ continue;
+ }
+ dev_err(dev, "failed to register sensor id %d: %d\n",
+ i, ret);
+ return ret;
+ }
+
+ /* register the thermal sensor to hwmon */
+ if (devm_thermal_add_hwmon_sysfs(dev, s->sdesc[i].tzd))
+ dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
+ }
+
+ return 0;
+}
+
+static const struct of_device_id of_k3_thermal_match[] = {
+ {
+ .compatible = "spacemit,k3-tsensor",
+ },
+ { /* end */ }
+};
+
+MODULE_DEVICE_TABLE(of, of_k3_thermal_match);
+
+static struct platform_driver k3_thermal_driver = {
+ .driver = {
+ .name = "k3_thermal",
+ .of_match_table = of_k3_thermal_match,
+ },
+ .probe = k3_thermal_probe,
+};
+
+module_platform_driver(k3_thermal_driver);
--- /dev/null
+++ b/drivers/thermal/spacemit-k3-thermal.h
@@ -0,0 +1,58 @@
+#ifndef __k3_THERMAL_H__
+#define __k3_THERMAL_H__
+
+#define MAX_SENSOR_NUMBER 8
+#define CALIB_VREF_DEFAULT (0x9A)
+#define BITS_D_REG_VREF_CTRL BITS(7, 14)
+#define BITS_D_REG_VREF_OFFSET (7)
+
+#define BITS(_start, _end) ((BIT(_end) - BIT(_start)) + BIT(_end))
+
+#define MAX_SENSOR_NUMBER 8
+#define TEMPERATURE_OFFSET 278
+
+#define REG_TSEN_LITE_CFG 0x00
+#define REG_TSEN_LITE_CFG2 0x04
+#define REG_TSEN_LITE_INT_ENB 0x08
+#define REG_TSEN_LITE_INT_ST 0x0C
+#define REG_TSEN_LITE_INT_CLR 0x10
+#define REG_TSEN_REBOOT_THRESH 0x14
+#define REG_TSEN_LITE_TEMP_DATA 0x18
+#define REG_TSEN_LITE_VREF_EFFUSE 0x1C
+#define REG_TSEN_LITE_TEMP_THRESH 0x20
+#define REG_TSEN_LITE_ECO_INTVL 0x24
+#define REG_TSEN_LITE_LP_FIX_BYPASS 0x28
+
+#define BITS_D_CK_DIV_SEL BITS(2, 3)
+#define BITS_TEMP_DATA BITS(0, 11)
+#define BITS_CK_DIV_SEL_DIV4 (0 << 2)
+#define BIT_TSEN_EN BIT(0)
+
+#define TSEN_HIGH_THRESH_OFFSET (0)
+#define TSEN_LOW_THRESH_OFFSET (16)
+
+#define TEMP_RAW_DATA_DIV (8)
+
+struct k3_thermal_sensor_desc {
+ void __iomem *base;
+ int temp_offset;
+ int index;
+ struct k3_thermal_sensor *ks;
+ struct thermal_zone_device *tzd;
+};
+
+struct k3_thermal_sensor {
+ int irq;
+ int temp_offset;
+ void __iomem *base;
+ struct clk *fclk, *bclk;
+ struct reset_control *resets;
+ struct device *dev;
+ struct mutex lock;
+ /* sensor range */
+ unsigned int sr[2];
+ struct k3_thermal_sensor_desc *sdesc;
+ unsigned int tsen_enable_map[MAX_SENSOR_NUMBER];
+};
+
+#endif