Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
64f09aa9 HC |
2 | #include <linux/err.h> |
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> | |
8 | ||
9 | #include <loongson.h> | |
10 | #include <boot_param.h> | |
11 | #include <loongson_hwmon.h> | |
7507445b HC |
12 | #include <loongson_regs.h> |
13 | ||
f17d3f21 | 14 | static int csr_temp_enable; |
64f09aa9 HC |
15 | |
16 | /* | |
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 | |
21 | */ | |
22 | int loongson3_cpu_temp(int cpu) | |
23 | { | |
0a00024d | 24 | u32 reg, prid_rev; |
64f09aa9 | 25 | |
7507445b HC |
26 | if (csr_temp_enable) { |
27 | reg = (csr_readl(LOONGSON_CSR_CPUTEMP) & 0xff); | |
28 | goto out; | |
29 | } | |
30 | ||
64f09aa9 | 31 | reg = LOONGSON_CHIPTEMP(cpu); |
0a00024d | 32 | prid_rev = read_c0_prid() & PRID_REV_MASK; |
7507445b | 33 | |
0a00024d HC |
34 | switch (prid_rev) { |
35 | case PRID_REV_LOONGSON3A_R1: | |
64f09aa9 | 36 | reg = (reg >> 8) & 0xff; |
0a00024d | 37 | break; |
0a00024d HC |
38 | case PRID_REV_LOONGSON3B_R1: |
39 | case PRID_REV_LOONGSON3B_R2: | |
f3ade253 HC |
40 | case PRID_REV_LOONGSON3A_R2_0: |
41 | case PRID_REV_LOONGSON3A_R2_1: | |
64f09aa9 | 42 | reg = ((reg >> 8) & 0xff) - 100; |
0a00024d | 43 | break; |
7cff3f16 HC |
44 | case PRID_REV_LOONGSON3A_R3_0: |
45 | case PRID_REV_LOONGSON3A_R3_1: | |
7507445b | 46 | default: |
f17d3f21 | 47 | reg = (reg & 0xffff) * 731 / 0x4000 - 273; |
0a00024d HC |
48 | break; |
49 | } | |
7507445b HC |
50 | |
51 | out: | |
64f09aa9 HC |
52 | return (int)reg * 1000; |
53 | } | |
54 | ||
99b0b5a3 | 55 | static int nr_packages; |
64f09aa9 HC |
56 | static struct device *cpu_hwmon_dev; |
57 | ||
99b0b5a3 | 58 | static ssize_t cpu_temp_label(struct device *dev, |
64f09aa9 HC |
59 | struct device_attribute *attr, char *buf) |
60 | { | |
99b0b5a3 | 61 | int id = (to_sensor_dev_attr(attr))->index - 1; |
f17d3f21 | 62 | |
99b0b5a3 | 63 | return sprintf(buf, "CPU %d Temperature\n", id); |
64f09aa9 HC |
64 | } |
65 | ||
99b0b5a3 | 66 | static ssize_t get_cpu_temp(struct device *dev, |
64f09aa9 HC |
67 | struct device_attribute *attr, char *buf) |
68 | { | |
99b0b5a3 HC |
69 | int id = (to_sensor_dev_attr(attr))->index - 1; |
70 | int value = loongson3_cpu_temp(id); | |
f17d3f21 | 71 | |
64f09aa9 HC |
72 | return sprintf(buf, "%d\n", value); |
73 | } | |
74 | ||
abae018a GR |
75 | static SENSOR_DEVICE_ATTR(temp1_input, 0444, get_cpu_temp, NULL, 1); |
76 | static SENSOR_DEVICE_ATTR(temp1_label, 0444, cpu_temp_label, NULL, 1); | |
77 | static SENSOR_DEVICE_ATTR(temp2_input, 0444, get_cpu_temp, NULL, 2); | |
78 | static SENSOR_DEVICE_ATTR(temp2_label, 0444, cpu_temp_label, NULL, 2); | |
79 | static SENSOR_DEVICE_ATTR(temp3_input, 0444, get_cpu_temp, NULL, 3); | |
80 | static SENSOR_DEVICE_ATTR(temp3_label, 0444, cpu_temp_label, NULL, 3); | |
81 | static SENSOR_DEVICE_ATTR(temp4_input, 0444, get_cpu_temp, NULL, 4); | |
82 | static SENSOR_DEVICE_ATTR(temp4_label, 0444, cpu_temp_label, NULL, 4); | |
64f09aa9 | 83 | |
abae018a GR |
84 | static struct attribute *cpu_hwmon_attributes[] = { |
85 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
86 | &sensor_dev_attr_temp1_label.dev_attr.attr, | |
87 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
88 | &sensor_dev_attr_temp2_label.dev_attr.attr, | |
89 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
90 | &sensor_dev_attr_temp3_label.dev_attr.attr, | |
91 | &sensor_dev_attr_temp4_input.dev_attr.attr, | |
92 | &sensor_dev_attr_temp4_label.dev_attr.attr, | |
93 | NULL | |
94 | }; | |
64f09aa9 | 95 | |
abae018a GR |
96 | static umode_t cpu_hwmon_is_visible(struct kobject *kobj, |
97 | struct attribute *attr, int i) | |
64f09aa9 | 98 | { |
abae018a | 99 | int id = i / 2; |
64f09aa9 | 100 | |
abae018a GR |
101 | if (id < nr_packages) |
102 | return attr->mode; | |
103 | return 0; | |
64f09aa9 HC |
104 | } |
105 | ||
abae018a GR |
106 | static struct attribute_group cpu_hwmon_group = { |
107 | .attrs = cpu_hwmon_attributes, | |
108 | .is_visible = cpu_hwmon_is_visible, | |
109 | }; | |
110 | ||
111 | static const struct attribute_group *cpu_hwmon_groups[] = { | |
112 | &cpu_hwmon_group, | |
113 | NULL | |
114 | }; | |
115 | ||
64f09aa9 HC |
116 | #define CPU_THERMAL_THRESHOLD 90000 |
117 | static struct delayed_work thermal_work; | |
118 | ||
119 | static void do_thermal_timer(struct work_struct *work) | |
120 | { | |
17cbb070 | 121 | int i, value; |
99b0b5a3 | 122 | |
f17d3f21 | 123 | for (i = 0; i < nr_packages; i++) { |
99b0b5a3 | 124 | value = loongson3_cpu_temp(i); |
17cbb070 TY |
125 | if (value > CPU_THERMAL_THRESHOLD) { |
126 | pr_emerg("Power off due to high temp: %d\n", value); | |
127 | orderly_poweroff(true); | |
128 | } | |
99b0b5a3 HC |
129 | } |
130 | ||
17cbb070 | 131 | schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000)); |
64f09aa9 HC |
132 | } |
133 | ||
134 | static int __init loongson_hwmon_init(void) | |
135 | { | |
64f09aa9 HC |
136 | pr_info("Loongson Hwmon Enter...\n"); |
137 | ||
7507445b | 138 | if (cpu_has_csr()) |
f17d3f21 TY |
139 | csr_temp_enable = csr_readl(LOONGSON_CSR_FEATURES) & |
140 | LOONGSON_CSRF_TEMP; | |
7507445b | 141 | |
99b0b5a3 HC |
142 | nr_packages = loongson_sysconf.nr_cpus / |
143 | loongson_sysconf.cores_per_package; | |
144 | ||
abae018a GR |
145 | cpu_hwmon_dev = hwmon_device_register_with_groups(NULL, "cpu_hwmon", |
146 | NULL, cpu_hwmon_groups); | |
147 | if (IS_ERR(cpu_hwmon_dev)) { | |
148 | pr_err("hwmon_device_register fail!\n"); | |
149 | return PTR_ERR(cpu_hwmon_dev); | |
64f09aa9 HC |
150 | } |
151 | ||
152 | INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer); | |
153 | schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000)); | |
154 | ||
abae018a | 155 | return 0; |
64f09aa9 HC |
156 | } |
157 | ||
158 | static void __exit loongson_hwmon_exit(void) | |
159 | { | |
160 | cancel_delayed_work_sync(&thermal_work); | |
64f09aa9 HC |
161 | hwmon_device_unregister(cpu_hwmon_dev); |
162 | } | |
163 | ||
164 | module_init(loongson_hwmon_init); | |
165 | module_exit(loongson_hwmon_exit); | |
166 | ||
167 | MODULE_AUTHOR("Yu Xiang <xiangy@lemote.com>"); | |
168 | MODULE_AUTHOR("Huacai Chen <chenhc@lemote.com>"); | |
169 | MODULE_DESCRIPTION("Loongson CPU Hwmon driver"); | |
170 | MODULE_LICENSE("GPL"); |