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/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"
18 #define LOONGSON2_MAX_SENSOR_SEL_NUM 3
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
25 #define LOONGSON2_THSENS_INT_LO BIT(0)
26 #define LOONGSON2_THSENS_INT_HIGH BIT(1)
27 #define LOONGSON2_THSENS_OUT_MASK 0xFF
29 struct loongson2_thermal_chip_data {
30 unsigned int thermal_sensor_sel;
33 struct loongson2_thermal_data {
35 const struct loongson2_thermal_chip_data *chip_data;
38 static int loongson2_thermal_set(struct loongson2_thermal_data *data,
39 int low, int high, bool enable)
42 int reg_off = data->chip_data->thermal_sensor_sel * 2;
44 low = clamp(-40, low, high);
45 high = clamp(125, low, high);
51 reg_ctrl |= enable ? 0x100 : 0;
52 writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_LOW_REG + reg_off);
55 reg_ctrl |= enable ? 0x100 : 0;
56 writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_HI_REG + reg_off);
61 static int loongson2_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
64 struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
66 reg_val = readl(data->regs + LOONGSON2_THSENS_OUT_REG);
67 *temp = ((reg_val & LOONGSON2_THSENS_OUT_MASK) - HECTO) * KILO;
72 static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev)
74 struct thermal_zone_device *tzd = dev;
75 struct loongson2_thermal_data *data = thermal_zone_device_priv(tzd);
77 writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
78 LOONGSON2_THSENS_STATUS_REG);
80 thermal_zone_device_update(tzd, THERMAL_EVENT_UNSPECIFIED);
85 static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
87 struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
89 return loongson2_thermal_set(data, low/MILLI, high/MILLI, true);
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,
97 static int loongson2_thermal_probe(struct platform_device *pdev)
99 struct device *dev = &pdev->dev;
100 struct loongson2_thermal_data *data;
101 struct thermal_zone_device *tzd;
104 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
108 data->chip_data = device_get_match_data(dev);
110 data->regs = devm_platform_ioremap_resource(pdev, 0);
111 if (IS_ERR(data->regs))
112 return PTR_ERR(data->regs);
114 irq = platform_get_irq(pdev, 0);
118 writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs +
119 LOONGSON2_THSENS_STATUS_REG);
121 loongson2_thermal_set(data, 0, 0, false);
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);
130 if (PTR_ERR(tzd) != ENODEV)
133 return dev_err_probe(dev, PTR_ERR(tzd), "failed to register");
136 ret = devm_request_threaded_irq(dev, irq, NULL, loongson2_thermal_irq_thread,
137 IRQF_ONESHOT, "loongson2_thermal", tzd);
139 return dev_err_probe(dev, ret, "failed to request alarm irq\n");
141 devm_thermal_add_hwmon_sysfs(dev, tzd);
146 static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = {
147 .thermal_sensor_sel = 0,
150 static const struct of_device_id of_loongson2_thermal_match[] = {
152 .compatible = "loongson,ls2k1000-thermal",
153 .data = &loongson2_thermal_ls2k1000_data,
157 MODULE_DEVICE_TABLE(of, of_loongson2_thermal_match);
159 static struct platform_driver loongson2_thermal_driver = {
161 .name = "loongson2_thermal",
162 .of_match_table = of_loongson2_thermal_match,
164 .probe = loongson2_thermal_probe,
166 module_platform_driver(loongson2_thermal_driver);
168 MODULE_DESCRIPTION("Loongson2 thermal driver");
169 MODULE_LICENSE("GPL");