Merge branch 'next' into for-linus
[linux-2.6-microblaze.git] / drivers / platform / mips / cpu_hwmon.c
1 // SPDX-License-Identifier: GPL-2.0-only
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>
12 #include <loongson_regs.h>
13
14 static int csr_temp_enable = 0;
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 {
24         u32 reg, prid_rev;
25
26         if (csr_temp_enable) {
27                 reg = (csr_readl(LOONGSON_CSR_CPUTEMP) & 0xff);
28                 goto out;
29         }
30
31         reg = LOONGSON_CHIPTEMP(cpu);
32         prid_rev = read_c0_prid() & PRID_REV_MASK;
33
34         switch (prid_rev) {
35         case PRID_REV_LOONGSON3A_R1:
36                 reg = (reg >> 8) & 0xff;
37                 break;
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;
43                 break;
44         case PRID_REV_LOONGSON3A_R3_0:
45         case PRID_REV_LOONGSON3A_R3_1:
46         default:
47                 reg = (reg & 0xffff)*731/0x4000 - 273;
48                 break;
49         }
50
51 out:
52         return (int)reg * 1000;
53 }
54
55 static int nr_packages;
56 static struct device *cpu_hwmon_dev;
57
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);
61
62 static struct attribute *cpu_hwmon_attributes[] = {
63         &sensor_dev_attr_name.dev_attr.attr,
64         NULL
65 };
66
67 /* Hwmon device attribute group */
68 static struct attribute_group cpu_hwmon_attribute_group = {
69         .attrs = cpu_hwmon_attributes,
70 };
71
72 /* Hwmon device get name */
73 static ssize_t get_hwmon_name(struct device *dev,
74                         struct device_attribute *attr, char *buf)
75 {
76         return sprintf(buf, "cpu-hwmon\n");
77 }
78
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);
83
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);
92
93 static const struct attribute *hwmon_cputemp[4][3] = {
94         {
95                 &sensor_dev_attr_temp1_input.dev_attr.attr,
96                 &sensor_dev_attr_temp1_label.dev_attr.attr,
97                 NULL
98         },
99         {
100                 &sensor_dev_attr_temp2_input.dev_attr.attr,
101                 &sensor_dev_attr_temp2_label.dev_attr.attr,
102                 NULL
103         },
104         {
105                 &sensor_dev_attr_temp3_input.dev_attr.attr,
106                 &sensor_dev_attr_temp3_label.dev_attr.attr,
107                 NULL
108         },
109         {
110                 &sensor_dev_attr_temp4_input.dev_attr.attr,
111                 &sensor_dev_attr_temp4_label.dev_attr.attr,
112                 NULL
113         }
114 };
115
116 static ssize_t cpu_temp_label(struct device *dev,
117                         struct device_attribute *attr, char *buf)
118 {
119         int id = (to_sensor_dev_attr(attr))->index - 1;
120         return sprintf(buf, "CPU %d Temperature\n", id);
121 }
122
123 static ssize_t get_cpu_temp(struct device *dev,
124                         struct device_attribute *attr, char *buf)
125 {
126         int id = (to_sensor_dev_attr(attr))->index - 1;
127         int value = loongson3_cpu_temp(id);
128         return sprintf(buf, "%d\n", value);
129 }
130
131 static int create_sysfs_cputemp_files(struct kobject *kobj)
132 {
133         int i, ret = 0;
134
135         for (i=0; i<nr_packages; i++)
136                 ret = sysfs_create_files(kobj, hwmon_cputemp[i]);
137
138         return ret;
139 }
140
141 static void remove_sysfs_cputemp_files(struct kobject *kobj)
142 {
143         int i;
144
145         for (i=0; i<nr_packages; i++)
146                 sysfs_remove_files(kobj, hwmon_cputemp[i]);
147 }
148
149 #define CPU_THERMAL_THRESHOLD 90000
150 static struct delayed_work thermal_work;
151
152 static void do_thermal_timer(struct work_struct *work)
153 {
154         int i, value, temp_max = 0;
155
156         for (i=0; i<nr_packages; i++) {
157                 value = loongson3_cpu_temp(i);
158                 if (value > temp_max)
159                         temp_max = value;
160         }
161
162         if (temp_max <= CPU_THERMAL_THRESHOLD)
163                 schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000));
164         else
165                 orderly_poweroff(true);
166 }
167
168 static int __init loongson_hwmon_init(void)
169 {
170         int ret;
171
172         pr_info("Loongson Hwmon Enter...\n");
173
174         if (cpu_has_csr())
175                 csr_temp_enable = csr_readl(LOONGSON_CSR_FEATURES) & LOONGSON_CSRF_TEMP;
176
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;
182         }
183
184         nr_packages = loongson_sysconf.nr_cpus /
185                 loongson_sysconf.cores_per_package;
186
187         ret = sysfs_create_group(&cpu_hwmon_dev->kobj,
188                                 &cpu_hwmon_attribute_group);
189         if (ret) {
190                 pr_err("fail to create loongson hwmon!\n");
191                 goto fail_sysfs_create_group_hwmon;
192         }
193
194         ret = create_sysfs_cputemp_files(&cpu_hwmon_dev->kobj);
195         if (ret) {
196                 pr_err("fail to create cpu temperature interface!\n");
197                 goto fail_create_sysfs_cputemp_files;
198         }
199
200         INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer);
201         schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000));
202
203         return ret;
204
205 fail_create_sysfs_cputemp_files:
206         sysfs_remove_group(&cpu_hwmon_dev->kobj,
207                                 &cpu_hwmon_attribute_group);
208
209 fail_sysfs_create_group_hwmon:
210         hwmon_device_unregister(cpu_hwmon_dev);
211
212 fail_hwmon_device_register:
213         return ret;
214 }
215
216 static void __exit loongson_hwmon_exit(void)
217 {
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);
223 }
224
225 module_init(loongson_hwmon_init);
226 module_exit(loongson_hwmon_exit);
227
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");