1 #include <linux/err.h>
2 #include <linux/module.h>
3 #include <linux/reboot.h>
4 #include <linux/jiffies.h>
5 #include <linux/hwmon.h>
6 #include <linux/hwmon-sysfs.h>
7 
8 #include <loongson.h>
9 #include <boot_param.h>
10 #include <loongson_hwmon.h>
11 
12 /*
13  * Loongson-3 series cpu has two sensors inside,
14  * each of them from 0 to 255,
15  * if more than 127, that is dangerous.
16  * here only provide sensor1 data, because it always hot than sensor0
17  */
18 int loongson3_cpu_temp(int cpu)
19 {
20 	u32 reg, prid_rev;
21 
22 	reg = LOONGSON_CHIPTEMP(cpu);
23 	prid_rev = read_c0_prid() & PRID_REV_MASK;
24 	switch (prid_rev) {
25 	case PRID_REV_LOONGSON3A_R1:
26 		reg = (reg >> 8) & 0xff;
27 		break;
28 	case PRID_REV_LOONGSON3B_R1:
29 	case PRID_REV_LOONGSON3B_R2:
30 	case PRID_REV_LOONGSON3A_R2_0:
31 	case PRID_REV_LOONGSON3A_R2_1:
32 		reg = ((reg >> 8) & 0xff) - 100;
33 		break;
34 	case PRID_REV_LOONGSON3A_R3_0:
35 	case PRID_REV_LOONGSON3A_R3_1:
36 		reg = (reg & 0xffff)*731/0x4000 - 273;
37 		break;
38 	}
39 	return (int)reg * 1000;
40 }
41 
42 static int nr_packages;
43 static struct device *cpu_hwmon_dev;
44 
45 static ssize_t get_hwmon_name(struct device *dev,
46 			struct device_attribute *attr, char *buf);
47 static SENSOR_DEVICE_ATTR(name, S_IRUGO, get_hwmon_name, NULL, 0);
48 
49 static struct attribute *cpu_hwmon_attributes[] = {
50 	&sensor_dev_attr_name.dev_attr.attr,
51 	NULL
52 };
53 
54 /* Hwmon device attribute group */
55 static struct attribute_group cpu_hwmon_attribute_group = {
56 	.attrs = cpu_hwmon_attributes,
57 };
58 
59 /* Hwmon device get name */
60 static ssize_t get_hwmon_name(struct device *dev,
61 			struct device_attribute *attr, char *buf)
62 {
63 	return sprintf(buf, "cpu-hwmon\n");
64 }
65 
66 static ssize_t get_cpu_temp(struct device *dev,
67 			struct device_attribute *attr, char *buf);
68 static ssize_t cpu_temp_label(struct device *dev,
69 			struct device_attribute *attr, char *buf);
70 
71 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, get_cpu_temp, NULL, 1);
72 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, cpu_temp_label, NULL, 1);
73 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, get_cpu_temp, NULL, 2);
74 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, cpu_temp_label, NULL, 2);
75 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, get_cpu_temp, NULL, 3);
76 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, cpu_temp_label, NULL, 3);
77 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, get_cpu_temp, NULL, 4);
78 static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, cpu_temp_label, NULL, 4);
79 
80 static const struct attribute *hwmon_cputemp[4][3] = {
81 	{
82 		&sensor_dev_attr_temp1_input.dev_attr.attr,
83 		&sensor_dev_attr_temp1_label.dev_attr.attr,
84 		NULL
85 	},
86 	{
87 		&sensor_dev_attr_temp2_input.dev_attr.attr,
88 		&sensor_dev_attr_temp2_label.dev_attr.attr,
89 		NULL
90 	},
91 	{
92 		&sensor_dev_attr_temp3_input.dev_attr.attr,
93 		&sensor_dev_attr_temp3_label.dev_attr.attr,
94 		NULL
95 	},
96 	{
97 		&sensor_dev_attr_temp4_input.dev_attr.attr,
98 		&sensor_dev_attr_temp4_label.dev_attr.attr,
99 		NULL
100 	}
101 };
102 
103 static ssize_t cpu_temp_label(struct device *dev,
104 			struct device_attribute *attr, char *buf)
105 {
106 	int id = (to_sensor_dev_attr(attr))->index - 1;
107 	return sprintf(buf, "CPU %d Temperature\n", id);
108 }
109 
110 static ssize_t get_cpu_temp(struct device *dev,
111 			struct device_attribute *attr, char *buf)
112 {
113 	int id = (to_sensor_dev_attr(attr))->index - 1;
114 	int value = loongson3_cpu_temp(id);
115 	return sprintf(buf, "%d\n", value);
116 }
117 
118 static int create_sysfs_cputemp_files(struct kobject *kobj)
119 {
120 	int i, ret = 0;
121 
122 	for (i=0; i<nr_packages; i++)
123 		ret = sysfs_create_files(kobj, hwmon_cputemp[i]);
124 
125 	return ret;
126 }
127 
128 static void remove_sysfs_cputemp_files(struct kobject *kobj)
129 {
130 	int i;
131 
132 	for (i=0; i<nr_packages; i++)
133 		sysfs_remove_files(kobj, hwmon_cputemp[i]);
134 }
135 
136 #define CPU_THERMAL_THRESHOLD 90000
137 static struct delayed_work thermal_work;
138 
139 static void do_thermal_timer(struct work_struct *work)
140 {
141 	int i, value, temp_max = 0;
142 
143 	for (i=0; i<nr_packages; i++) {
144 		value = loongson3_cpu_temp(i);
145 		if (value > temp_max)
146 			temp_max = value;
147 	}
148 
149 	if (temp_max <= CPU_THERMAL_THRESHOLD)
150 		schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000));
151 	else
152 		orderly_poweroff(true);
153 }
154 
155 static int __init loongson_hwmon_init(void)
156 {
157 	int ret;
158 
159 	pr_info("Loongson Hwmon Enter...\n");
160 
161 	cpu_hwmon_dev = hwmon_device_register(NULL);
162 	if (IS_ERR(cpu_hwmon_dev)) {
163 		ret = -ENOMEM;
164 		pr_err("hwmon_device_register fail!\n");
165 		goto fail_hwmon_device_register;
166 	}
167 
168 	nr_packages = loongson_sysconf.nr_cpus /
169 		loongson_sysconf.cores_per_package;
170 
171 	ret = sysfs_create_group(&cpu_hwmon_dev->kobj,
172 				&cpu_hwmon_attribute_group);
173 	if (ret) {
174 		pr_err("fail to create loongson hwmon!\n");
175 		goto fail_sysfs_create_group_hwmon;
176 	}
177 
178 	ret = create_sysfs_cputemp_files(&cpu_hwmon_dev->kobj);
179 	if (ret) {
180 		pr_err("fail to create cpu temperature interface!\n");
181 		goto fail_create_sysfs_cputemp_files;
182 	}
183 
184 	INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer);
185 	schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000));
186 
187 	return ret;
188 
189 fail_create_sysfs_cputemp_files:
190 	sysfs_remove_group(&cpu_hwmon_dev->kobj,
191 				&cpu_hwmon_attribute_group);
192 
193 fail_sysfs_create_group_hwmon:
194 	hwmon_device_unregister(cpu_hwmon_dev);
195 
196 fail_hwmon_device_register:
197 	return ret;
198 }
199 
200 static void __exit loongson_hwmon_exit(void)
201 {
202 	cancel_delayed_work_sync(&thermal_work);
203 	remove_sysfs_cputemp_files(&cpu_hwmon_dev->kobj);
204 	sysfs_remove_group(&cpu_hwmon_dev->kobj,
205 				&cpu_hwmon_attribute_group);
206 	hwmon_device_unregister(cpu_hwmon_dev);
207 }
208 
209 module_init(loongson_hwmon_init);
210 module_exit(loongson_hwmon_exit);
211 
212 MODULE_AUTHOR("Yu Xiang <xiangy@lemote.com>");
213 MODULE_AUTHOR("Huacai Chen <chenhc@lemote.com>");
214 MODULE_DESCRIPTION("Loongson CPU Hwmon driver");
215 MODULE_LICENSE("GPL");
216