Skip to content

Commit e7e3a7c

Browse files
YinboZhudlezcano
authored andcommitted
thermal/drivers/loongson-2: Add thermal management support
This patch adds the support for Loongson-2 thermal sensor controller, which can support maximum four sensor selectors that corresponding to four sets of thermal control registers and one set of sampling register. The sensor selector can selector a speific thermal sensor as temperature input. The sampling register is used to obtain the temperature in real time, the control register GATE field is used to set the threshold of high or low temperature, when the input temperature is higher than the high temperature threshold or lower than the low temperature threshold, an interrupt will occur. Signed-off-by: zhanghongchen <[email protected]> Signed-off-by: Yinbo Zhu <[email protected]> Signed-off-by: Daniel Lezcano <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e98153a commit e7e3a7c

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

MAINTAINERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12285,6 +12285,13 @@ S: Maintained
1228512285
F: Documentation/devicetree/bindings/pinctrl/loongson,ls2k-pinctrl.yaml
1228612286
F: drivers/pinctrl/pinctrl-loongson2.c
1228712287

12288+
LOONGSON-2 SOC SERIES THERMAL DRIVER
12289+
M: zhanghongchen <[email protected]>
12290+
M: Yinbo Zhu <[email protected]>
12291+
12292+
S: Maintained
12293+
F: drivers/thermal/loongson2_thermal.c
12294+
1228812295
LSILOGIC MPT FUSION DRIVERS (FC/SAS/SPI)
1228912296
M: Sathya Prakash <[email protected]>
1229012297
M: Sreekanth Reddy <[email protected]>

drivers/thermal/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,16 @@ config KHADAS_MCU_FAN_THERMAL
510510
If you say yes here you get support for the FAN controlled
511511
by the Microcontroller found on the Khadas VIM boards.
512512

513+
config LOONGSON2_THERMAL
514+
tristate "Loongson-2 SoC series thermal driver"
515+
depends on LOONGARCH || COMPILE_TEST
516+
depends on OF
517+
help
518+
Support for Thermal driver found on Loongson-2 SoC series platforms.
519+
The thermal driver realizes get_temp and set_trips function, which
520+
are used to obtain the temperature of the current node and set the
521+
temperature range to trigger the interrupt. When the input temperature
522+
is higher than the high temperature threshold or lower than the low
523+
temperature threshold, the interrupt will occur.
524+
513525
endif

drivers/thermal/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ obj-$(CONFIG_UNIPHIER_THERMAL) += uniphier_thermal.o
6363
obj-$(CONFIG_AMLOGIC_THERMAL) += amlogic_thermal.o
6464
obj-$(CONFIG_SPRD_THERMAL) += sprd_thermal.o
6565
obj-$(CONFIG_KHADAS_MCU_FAN_THERMAL) += khadas_mcu_fan.o
66+
obj-$(CONFIG_LOONGSON2_THERMAL) += loongson2_thermal.o

drivers/thermal/loongson2_thermal.c

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Author: zhanghongchen <[email protected]>
4+
* Yinbo Zhu <[email protected]>
5+
* Copyright (C) 2022-2023 Loongson Technology Corporation Limited
6+
*/
7+
8+
#include <linux/interrupt.h>
9+
#include <linux/io.h>
10+
#include <linux/minmax.h>
11+
#include <linux/module.h>
12+
#include <linux/of_device.h>
13+
#include <linux/platform_device.h>
14+
#include <linux/thermal.h>
15+
#include <linux/units.h>
16+
#include "thermal_hwmon.h"
17+
18+
#define LOONGSON2_MAX_SENSOR_SEL_NUM 3
19+
20+
#define LOONGSON2_THSENS_CTRL_HI_REG 0x0
21+
#define LOONGSON2_THSENS_CTRL_LOW_REG 0x8
22+
#define LOONGSON2_THSENS_STATUS_REG 0x10
23+
#define LOONGSON2_THSENS_OUT_REG 0x14
24+
25+
#define LOONGSON2_THSENS_INT_LO BIT(0)
26+
#define LOONGSON2_THSENS_INT_HIGH BIT(1)
27+
#define LOONGSON2_THSENS_OUT_MASK 0xFF
28+
29+
struct loongson2_thermal_chip_data {
30+
unsigned int thermal_sensor_sel;
31+
};
32+
33+
struct loongson2_thermal_data {
34+
void __iomem *regs;
35+
const struct loongson2_thermal_chip_data *chip_data;
36+
};
37+
38+
static int loongson2_thermal_set(struct loongson2_thermal_data *data,
39+
int low, int high, bool enable)
40+
{
41+
u64 reg_ctrl = 0;
42+
int reg_off = data->chip_data->thermal_sensor_sel * 2;
43+
44+
low = clamp(-40, low, high);
45+
high = clamp(125, low, high);
46+
47+
low += HECTO;
48+
high += HECTO;
49+
50+
reg_ctrl = low;
51+
reg_ctrl |= enable ? 0x100 : 0;
52+
writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_LOW_REG + reg_off);
53+
54+
reg_ctrl = high;
55+
reg_ctrl |= enable ? 0x100 : 0;
56+
writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_HI_REG + reg_off);
57+
58+
return 0;
59+
}
60+
61+
static int loongson2_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
62+
{
63+
u32 reg_val;
64+
struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
65+
66+
reg_val = readl(data->regs + LOONGSON2_THSENS_OUT_REG);
67+
*temp = ((reg_val & LOONGSON2_THSENS_OUT_MASK) - HECTO) * KILO;
68+
69+
return 0;
70+
}
71+
72+
static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev)
73+
{
74+
struct thermal_zone_device *tzd = dev;
75+
struct loongson2_thermal_data *data = thermal_zone_device_priv(tzd);
76+
77+
writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
78+
LOONGSON2_THSENS_STATUS_REG);
79+
80+
thermal_zone_device_update(tzd, THERMAL_EVENT_UNSPECIFIED);
81+
82+
return IRQ_HANDLED;
83+
}
84+
85+
static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
86+
{
87+
struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
88+
89+
return loongson2_thermal_set(data, low/MILLI, high/MILLI, true);
90+
}
91+
92+
static const struct thermal_zone_device_ops loongson2_of_thermal_ops = {
93+
.get_temp = loongson2_thermal_get_temp,
94+
.set_trips = loongson2_thermal_set_trips,
95+
};
96+
97+
static int loongson2_thermal_probe(struct platform_device *pdev)
98+
{
99+
struct device *dev = &pdev->dev;
100+
struct loongson2_thermal_data *data;
101+
struct thermal_zone_device *tzd;
102+
int ret, irq, i;
103+
104+
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
105+
if (!data)
106+
return -ENOMEM;
107+
108+
data->chip_data = device_get_match_data(dev);
109+
110+
data->regs = devm_platform_ioremap_resource(pdev, 0);
111+
if (IS_ERR(data->regs))
112+
return PTR_ERR(data->regs);
113+
114+
irq = platform_get_irq(pdev, 0);
115+
if (irq < 0)
116+
return irq;
117+
118+
writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
119+
LOONGSON2_THSENS_STATUS_REG);
120+
121+
loongson2_thermal_set(data, 0, 0, false);
122+
123+
for (i = 0; i <= LOONGSON2_MAX_SENSOR_SEL_NUM; i++) {
124+
tzd = devm_thermal_of_zone_register(dev, i, data,
125+
&loongson2_of_thermal_ops);
126+
127+
if (!IS_ERR(tzd))
128+
break;
129+
130+
if (PTR_ERR(tzd) != ENODEV)
131+
continue;
132+
133+
return dev_err_probe(dev, PTR_ERR(tzd), "failed to register");
134+
}
135+
136+
ret = devm_request_threaded_irq(dev, irq, NULL, loongson2_thermal_irq_thread,
137+
IRQF_ONESHOT, "loongson2_thermal", tzd);
138+
if (ret < 0)
139+
return dev_err_probe(dev, ret, "failed to request alarm irq\n");
140+
141+
devm_thermal_add_hwmon_sysfs(dev, tzd);
142+
143+
return 0;
144+
}
145+
146+
static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = {
147+
.thermal_sensor_sel = 0,
148+
};
149+
150+
static const struct of_device_id of_loongson2_thermal_match[] = {
151+
{
152+
.compatible = "loongson,ls2k1000-thermal",
153+
.data = &loongson2_thermal_ls2k1000_data,
154+
},
155+
{ /* end */ }
156+
};
157+
MODULE_DEVICE_TABLE(of, of_loongson2_thermal_match);
158+
159+
static struct platform_driver loongson2_thermal_driver = {
160+
.driver = {
161+
.name = "loongson2_thermal",
162+
.of_match_table = of_loongson2_thermal_match,
163+
},
164+
.probe = loongson2_thermal_probe,
165+
};
166+
module_platform_driver(loongson2_thermal_driver);
167+
168+
MODULE_DESCRIPTION("Loongson2 thermal driver");
169+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)