1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/module.h>
4 #include <linux/reboot.h>
5 #include <linux/jiffies.h>
6 #include <linux/hwmon.h>
7 #include <linux/hwmon-sysfs.h>
10 #include <boot_param.h>
11 #include <loongson_hwmon.h>
12 #include <loongson_regs.h>
14 static int csr_temp_enable
= 0;
17 * Loongson-3 series cpu has two sensors inside,
18 * each of them from 0 to 255,
19 * if more than 127, that is dangerous.
20 * here only provide sensor1 data, because it always hot than sensor0
22 int loongson3_cpu_temp(int cpu
)
26 if (csr_temp_enable
) {
27 reg
= (csr_readl(LOONGSON_CSR_CPUTEMP
) & 0xff);
31 reg
= LOONGSON_CHIPTEMP(cpu
);
32 prid_rev
= read_c0_prid() & PRID_REV_MASK
;
35 case PRID_REV_LOONGSON3A_R1
:
36 reg
= (reg
>> 8) & 0xff;
38 case PRID_REV_LOONGSON3B_R1
:
39 case PRID_REV_LOONGSON3B_R2
:
40 case PRID_REV_LOONGSON3A_R2_0
:
41 case PRID_REV_LOONGSON3A_R2_1
:
42 reg
= ((reg
>> 8) & 0xff) - 100;
44 case PRID_REV_LOONGSON3A_R3_0
:
45 case PRID_REV_LOONGSON3A_R3_1
:
47 reg
= (reg
& 0xffff)*731/0x4000 - 273;
52 return (int)reg
* 1000;
55 static int nr_packages
;
56 static struct device
*cpu_hwmon_dev
;
58 static ssize_t
get_hwmon_name(struct device
*dev
,
59 struct device_attribute
*attr
, char *buf
);
60 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, get_hwmon_name
, NULL
, 0);
62 static struct attribute
*cpu_hwmon_attributes
[] = {
63 &sensor_dev_attr_name
.dev_attr
.attr
,
67 /* Hwmon device attribute group */
68 static struct attribute_group cpu_hwmon_attribute_group
= {
69 .attrs
= cpu_hwmon_attributes
,
72 /* Hwmon device get name */
73 static ssize_t
get_hwmon_name(struct device
*dev
,
74 struct device_attribute
*attr
, char *buf
)
76 return sprintf(buf
, "cpu-hwmon\n");
79 static ssize_t
get_cpu_temp(struct device
*dev
,
80 struct device_attribute
*attr
, char *buf
);
81 static ssize_t
cpu_temp_label(struct device
*dev
,
82 struct device_attribute
*attr
, char *buf
);
84 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, get_cpu_temp
, NULL
, 1);
85 static SENSOR_DEVICE_ATTR(temp1_label
, S_IRUGO
, cpu_temp_label
, NULL
, 1);
86 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, get_cpu_temp
, NULL
, 2);
87 static SENSOR_DEVICE_ATTR(temp2_label
, S_IRUGO
, cpu_temp_label
, NULL
, 2);
88 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
, get_cpu_temp
, NULL
, 3);
89 static SENSOR_DEVICE_ATTR(temp3_label
, S_IRUGO
, cpu_temp_label
, NULL
, 3);
90 static SENSOR_DEVICE_ATTR(temp4_input
, S_IRUGO
, get_cpu_temp
, NULL
, 4);
91 static SENSOR_DEVICE_ATTR(temp4_label
, S_IRUGO
, cpu_temp_label
, NULL
, 4);
93 static const struct attribute
*hwmon_cputemp
[4][3] = {
95 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
96 &sensor_dev_attr_temp1_label
.dev_attr
.attr
,
100 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
101 &sensor_dev_attr_temp2_label
.dev_attr
.attr
,
105 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
106 &sensor_dev_attr_temp3_label
.dev_attr
.attr
,
110 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
111 &sensor_dev_attr_temp4_label
.dev_attr
.attr
,
116 static ssize_t
cpu_temp_label(struct device
*dev
,
117 struct device_attribute
*attr
, char *buf
)
119 int id
= (to_sensor_dev_attr(attr
))->index
- 1;
120 return sprintf(buf
, "CPU %d Temperature\n", id
);
123 static ssize_t
get_cpu_temp(struct device
*dev
,
124 struct device_attribute
*attr
, char *buf
)
126 int id
= (to_sensor_dev_attr(attr
))->index
- 1;
127 int value
= loongson3_cpu_temp(id
);
128 return sprintf(buf
, "%d\n", value
);
131 static int create_sysfs_cputemp_files(struct kobject
*kobj
)
135 for (i
=0; i
<nr_packages
; i
++)
136 ret
= sysfs_create_files(kobj
, hwmon_cputemp
[i
]);
141 static void remove_sysfs_cputemp_files(struct kobject
*kobj
)
145 for (i
=0; i
<nr_packages
; i
++)
146 sysfs_remove_files(kobj
, hwmon_cputemp
[i
]);
149 #define CPU_THERMAL_THRESHOLD 90000
150 static struct delayed_work thermal_work
;
152 static void do_thermal_timer(struct work_struct
*work
)
154 int i
, value
, temp_max
= 0;
156 for (i
=0; i
<nr_packages
; i
++) {
157 value
= loongson3_cpu_temp(i
);
158 if (value
> temp_max
)
162 if (temp_max
<= CPU_THERMAL_THRESHOLD
)
163 schedule_delayed_work(&thermal_work
, msecs_to_jiffies(5000));
165 orderly_poweroff(true);
168 static int __init
loongson_hwmon_init(void)
172 pr_info("Loongson Hwmon Enter...\n");
175 csr_temp_enable
= csr_readl(LOONGSON_CSR_FEATURES
) & LOONGSON_CSRF_TEMP
;
177 cpu_hwmon_dev
= hwmon_device_register(NULL
);
178 if (IS_ERR(cpu_hwmon_dev
)) {
179 ret
= PTR_ERR(cpu_hwmon_dev
);
180 pr_err("hwmon_device_register fail!\n");
181 goto fail_hwmon_device_register
;
184 nr_packages
= loongson_sysconf
.nr_cpus
/
185 loongson_sysconf
.cores_per_package
;
187 ret
= sysfs_create_group(&cpu_hwmon_dev
->kobj
,
188 &cpu_hwmon_attribute_group
);
190 pr_err("fail to create loongson hwmon!\n");
191 goto fail_sysfs_create_group_hwmon
;
194 ret
= create_sysfs_cputemp_files(&cpu_hwmon_dev
->kobj
);
196 pr_err("fail to create cpu temperature interface!\n");
197 goto fail_create_sysfs_cputemp_files
;
200 INIT_DEFERRABLE_WORK(&thermal_work
, do_thermal_timer
);
201 schedule_delayed_work(&thermal_work
, msecs_to_jiffies(20000));
205 fail_create_sysfs_cputemp_files
:
206 sysfs_remove_group(&cpu_hwmon_dev
->kobj
,
207 &cpu_hwmon_attribute_group
);
209 fail_sysfs_create_group_hwmon
:
210 hwmon_device_unregister(cpu_hwmon_dev
);
212 fail_hwmon_device_register
:
216 static void __exit
loongson_hwmon_exit(void)
218 cancel_delayed_work_sync(&thermal_work
);
219 remove_sysfs_cputemp_files(&cpu_hwmon_dev
->kobj
);
220 sysfs_remove_group(&cpu_hwmon_dev
->kobj
,
221 &cpu_hwmon_attribute_group
);
222 hwmon_device_unregister(cpu_hwmon_dev
);
225 module_init(loongson_hwmon_init
);
226 module_exit(loongson_hwmon_exit
);
228 MODULE_AUTHOR("Yu Xiang <xiangy@lemote.com>");
229 MODULE_AUTHOR("Huacai Chen <chenhc@lemote.com>");
230 MODULE_DESCRIPTION("Loongson CPU Hwmon driver");
231 MODULE_LICENSE("GPL");