sunxi: backport h616 thermal sensor support
[openwrt/staging/stintel.git] / target / linux / sunxi / patches-6.1 / 013-v6.9-thermal-drivers-sun8i-Add-SRAM-register-access-code.patch
1 From f8b54d1120b81ed57bed96cc8e814ba08886d1e5 Mon Sep 17 00:00:00 2001
2 From: Andre Przywara <andre.przywara@arm.com>
3 Date: Mon, 19 Feb 2024 15:36:37 +0000
4 Subject: [PATCH] thermal/drivers/sun8i: Add SRAM register access code
5
6 The Allwinner H616 SoC needs to clear a bit in one register in the SRAM
7 controller, to report reasonable temperature values. On reset, bit 16 in
8 register 0x3000000 is set, which leads to the driver reporting
9 temperatures around 200C. Clearing this bit brings the values down to the
10 expected range. The BSP code does a one-time write in U-Boot, with a
11 comment just mentioning the effect on the THS, but offering no further
12 explanation.
13
14 To not rely on firmware to set things up for us, add code that queries
15 the SRAM controller device via a DT phandle link, then clear just this
16 single bit.
17
18 Signed-off-by: Andre Przywara <andre.przywara@arm.com>
19 Acked-by: Vasily Khoruzhick <anarsoul@gmail.com>
20 Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
21 Link: https://lore.kernel.org/r/20240219153639.179814-6-andre.przywara@arm.com
22 ---
23 drivers/thermal/sun8i_thermal.c | 51 +++++++++++++++++++++++++++++++++
24 1 file changed, 51 insertions(+)
25
26 --- a/drivers/thermal/sun8i_thermal.c
27 +++ b/drivers/thermal/sun8i_thermal.c
28 @@ -15,6 +15,7 @@
29 #include <linux/module.h>
30 #include <linux/nvmem-consumer.h>
31 #include <linux/of_device.h>
32 +#include <linux/of_platform.h>
33 #include <linux/platform_device.h>
34 #include <linux/regmap.h>
35 #include <linux/reset.h>
36 @@ -68,6 +69,7 @@ struct tsensor {
37 struct ths_thermal_chip {
38 bool has_mod_clk;
39 bool has_bus_clk_reset;
40 + bool needs_sram;
41 int sensor_num;
42 int offset;
43 int scale;
44 @@ -85,12 +87,16 @@ struct ths_device {
45 const struct ths_thermal_chip *chip;
46 struct device *dev;
47 struct regmap *regmap;
48 + struct regmap_field *sram_regmap_field;
49 struct reset_control *reset;
50 struct clk *bus_clk;
51 struct clk *mod_clk;
52 struct tsensor sensor[MAX_SENSOR_NUM];
53 };
54
55 +/* The H616 needs to have a bit 16 in the SRAM control register cleared. */
56 +static const struct reg_field sun8i_ths_sram_reg_field = REG_FIELD(0x0, 16, 16);
57 +
58 /* Temp Unit: millidegree Celsius */
59 static int sun8i_ths_calc_temp(struct ths_device *tmdev,
60 int id, int reg)
61 @@ -337,6 +343,34 @@ static void sun8i_ths_reset_control_asse
62 reset_control_assert(data);
63 }
64
65 +static struct regmap *sun8i_ths_get_sram_regmap(struct device_node *node)
66 +{
67 + struct device_node *sram_node;
68 + struct platform_device *sram_pdev;
69 + struct regmap *regmap = NULL;
70 +
71 + sram_node = of_parse_phandle(node, "allwinner,sram", 0);
72 + if (!sram_node)
73 + return ERR_PTR(-ENODEV);
74 +
75 + sram_pdev = of_find_device_by_node(sram_node);
76 + if (!sram_pdev) {
77 + /* platform device might not be probed yet */
78 + regmap = ERR_PTR(-EPROBE_DEFER);
79 + goto out_put_node;
80 + }
81 +
82 + /* If no regmap is found then the other device driver is at fault */
83 + regmap = dev_get_regmap(&sram_pdev->dev, NULL);
84 + if (!regmap)
85 + regmap = ERR_PTR(-EINVAL);
86 +
87 + platform_device_put(sram_pdev);
88 +out_put_node:
89 + of_node_put(sram_node);
90 + return regmap;
91 +}
92 +
93 static int sun8i_ths_resource_init(struct ths_device *tmdev)
94 {
95 struct device *dev = tmdev->dev;
96 @@ -381,6 +415,19 @@ static int sun8i_ths_resource_init(struc
97 if (ret)
98 return ret;
99
100 + if (tmdev->chip->needs_sram) {
101 + struct regmap *regmap;
102 +
103 + regmap = sun8i_ths_get_sram_regmap(dev->of_node);
104 + if (IS_ERR(regmap))
105 + return PTR_ERR(regmap);
106 + tmdev->sram_regmap_field = devm_regmap_field_alloc(dev,
107 + regmap,
108 + sun8i_ths_sram_reg_field);
109 + if (IS_ERR(tmdev->sram_regmap_field))
110 + return PTR_ERR(tmdev->sram_regmap_field);
111 + }
112 +
113 ret = sun8i_ths_calibrate(tmdev);
114 if (ret)
115 return ret;
116 @@ -427,6 +474,10 @@ static int sun50i_h6_thermal_init(struct
117 {
118 int val;
119
120 + /* The H616 needs to have a bit in the SRAM control register cleared. */
121 + if (tmdev->sram_regmap_field)
122 + regmap_field_write(tmdev->sram_regmap_field, 0);
123 +
124 /*
125 * The manual recommends an overall sample frequency of 50 KHz (20us,
126 * 480 cycles at 24 MHz), which provides plenty of time for both the