1 // SPDX-License-Identifier: GPL-2.0+
3 * Author: zhanghongchen <zhanghongchen@loongson.cn>
4 * Yinbo Zhu <zhuyinbo@loongson.cn>
5 * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
8 #include <linux/interrupt.h>
10 #include <linux/minmax.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
15 #include <linux/thermal.h>
16 #include <linux/units.h>
17 #include "thermal_hwmon.h"
19 #define LOONGSON2_MAX_SENSOR_SEL_NUM 3
21 #define LOONGSON2_THSENS_CTRL_HI_REG 0x0
22 #define LOONGSON2_THSENS_CTRL_LOW_REG 0x8
23 #define LOONGSON2_THSENS_STATUS_REG 0x10
24 #define LOONGSON2_THSENS_OUT_REG 0x14
26 #define LOONGSON2_THSENS_INT_LO BIT(0)
27 #define LOONGSON2_THSENS_INT_HIGH BIT(1)
28 #define LOONGSON2_THSENS_OUT_MASK 0xFF
30 struct loongson2_thermal_chip_data {
31 unsigned int thermal_sensor_sel;
34 struct loongson2_thermal_data {
36 const struct loongson2_thermal_chip_data *chip_data;
39 static int loongson2_thermal_set(struct loongson2_thermal_data *data,
40 int low, int high, bool enable)
43 int reg_off = data->chip_data->thermal_sensor_sel * 2;
45 low = clamp(-40, low, high);
46 high = clamp(125, low, high);
52 reg_ctrl |= enable ? 0x100 : 0;
53 writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_LOW_REG + reg_off);
56 reg_ctrl |= enable ? 0x100 : 0;
57 writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_HI_REG + reg_off);
62 static int loongson2_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
65 struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
67 reg_val = readl(data->regs + LOONGSON2_THSENS_OUT_REG);
68 *temp = ((reg_val & LOONGSON2_THSENS_OUT_MASK) - HECTO) * KILO;
73 static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev)
75 struct thermal_zone_device *tzd = dev;
76 struct loongson2_thermal_data *data = thermal_zone_device_priv(tzd);
78 writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
79 LOONGSON2_THSENS_STATUS_REG);
81 thermal_zone_device_update(tzd, THERMAL_EVENT_UNSPECIFIED);
86 static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
88 struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
90 return loongson2_thermal_set(data, low/MILLI, high/MILLI, true);
93 static const struct thermal_zone_device_ops loongson2_of_thermal_ops = {
94 .get_temp = loongson2_thermal_get_temp,
95 .set_trips = loongson2_thermal_set_trips,
98 static int loongson2_thermal_probe(struct platform_device *pdev)
100 struct device *dev = &pdev->dev;
101 struct loongson2_thermal_data *data;
102 struct thermal_zone_device *tzd;
105 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
109 data->chip_data = device_get_match_data(dev);
111 data->regs = devm_platform_ioremap_resource(pdev, 0);
112 if (IS_ERR(data->regs))
113 return PTR_ERR(data->regs);
115 irq = platform_get_irq(pdev, 0);
119 writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
120 LOONGSON2_THSENS_STATUS_REG);
122 loongson2_thermal_set(data, 0, 0, false);
124 for (i = 0; i <= LOONGSON2_MAX_SENSOR_SEL_NUM; i++) {
125 tzd = devm_thermal_of_zone_register(dev, i, data,
126 &loongson2_of_thermal_ops);
131 if (PTR_ERR(tzd) != -ENODEV)
134 return dev_err_probe(dev, PTR_ERR(tzd), "failed to register");
137 ret = devm_request_threaded_irq(dev, irq, NULL, loongson2_thermal_irq_thread,
138 IRQF_ONESHOT, "loongson2_thermal", tzd);
140 return dev_err_probe(dev, ret, "failed to request alarm irq\n");
142 devm_thermal_add_hwmon_sysfs(dev, tzd);
147 static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = {
148 .thermal_sensor_sel = 0,
151 static const struct of_device_id of_loongson2_thermal_match[] = {
153 .compatible = "loongson,ls2k1000-thermal",
154 .data = &loongson2_thermal_ls2k1000_data,
158 MODULE_DEVICE_TABLE(of, of_loongson2_thermal_match);
160 static struct platform_driver loongson2_thermal_driver = {
162 .name = "loongson2_thermal",
163 .of_match_table = of_loongson2_thermal_match,
165 .probe = loongson2_thermal_probe,
167 module_platform_driver(loongson2_thermal_driver);
169 MODULE_DESCRIPTION("Loongson2 thermal driver");
170 MODULE_LICENSE("GPL");