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>
9 #include <boot_param.h>
10 #include <loongson_hwmon.h>
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
18 int loongson3_cpu_temp(int cpu
)
22 reg
= LOONGSON_CHIPTEMP(cpu
);
23 prid_rev
= read_c0_prid() & PRID_REV_MASK
;
25 case PRID_REV_LOONGSON3A_R1
:
26 reg
= (reg
>> 8) & 0xff;
28 case PRID_REV_LOONGSON3A_R2
:
29 case PRID_REV_LOONGSON3B_R1
:
30 case PRID_REV_LOONGSON3B_R2
:
31 reg
= ((reg
>> 8) & 0xff) - 100;
33 case PRID_REV_LOONGSON3A_R3
:
34 reg
= (reg
& 0xffff)*731/0x4000 - 273;
37 return (int)reg
* 1000;
40 static int nr_packages
;
41 static struct device
*cpu_hwmon_dev
;
43 static ssize_t
get_hwmon_name(struct device
*dev
,
44 struct device_attribute
*attr
, char *buf
);
45 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, get_hwmon_name
, NULL
, 0);
47 static struct attribute
*cpu_hwmon_attributes
[] = {
48 &sensor_dev_attr_name
.dev_attr
.attr
,
52 /* Hwmon device attribute group */
53 static struct attribute_group cpu_hwmon_attribute_group
= {
54 .attrs
= cpu_hwmon_attributes
,
57 /* Hwmon device get name */
58 static ssize_t
get_hwmon_name(struct device
*dev
,
59 struct device_attribute
*attr
, char *buf
)
61 return sprintf(buf
, "cpu-hwmon\n");
64 static ssize_t
get_cpu_temp(struct device
*dev
,
65 struct device_attribute
*attr
, char *buf
);
66 static ssize_t
cpu_temp_label(struct device
*dev
,
67 struct device_attribute
*attr
, char *buf
);
69 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, get_cpu_temp
, NULL
, 1);
70 static SENSOR_DEVICE_ATTR(temp1_label
, S_IRUGO
, cpu_temp_label
, NULL
, 1);
71 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, get_cpu_temp
, NULL
, 2);
72 static SENSOR_DEVICE_ATTR(temp2_label
, S_IRUGO
, cpu_temp_label
, NULL
, 2);
73 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
, get_cpu_temp
, NULL
, 3);
74 static SENSOR_DEVICE_ATTR(temp3_label
, S_IRUGO
, cpu_temp_label
, NULL
, 3);
75 static SENSOR_DEVICE_ATTR(temp4_input
, S_IRUGO
, get_cpu_temp
, NULL
, 4);
76 static SENSOR_DEVICE_ATTR(temp4_label
, S_IRUGO
, cpu_temp_label
, NULL
, 4);
78 static const struct attribute
*hwmon_cputemp
[4][3] = {
80 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
81 &sensor_dev_attr_temp1_label
.dev_attr
.attr
,
85 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
86 &sensor_dev_attr_temp2_label
.dev_attr
.attr
,
90 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
91 &sensor_dev_attr_temp3_label
.dev_attr
.attr
,
95 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
96 &sensor_dev_attr_temp4_label
.dev_attr
.attr
,
101 static ssize_t
cpu_temp_label(struct device
*dev
,
102 struct device_attribute
*attr
, char *buf
)
104 int id
= (to_sensor_dev_attr(attr
))->index
- 1;
105 return sprintf(buf
, "CPU %d Temperature\n", id
);
108 static ssize_t
get_cpu_temp(struct device
*dev
,
109 struct device_attribute
*attr
, char *buf
)
111 int id
= (to_sensor_dev_attr(attr
))->index
- 1;
112 int value
= loongson3_cpu_temp(id
);
113 return sprintf(buf
, "%d\n", value
);
116 static int create_sysfs_cputemp_files(struct kobject
*kobj
)
120 for (i
=0; i
<nr_packages
; i
++)
121 ret
= sysfs_create_files(kobj
, hwmon_cputemp
[i
]);
126 static void remove_sysfs_cputemp_files(struct kobject
*kobj
)
130 for (i
=0; i
<nr_packages
; i
++)
131 sysfs_remove_files(kobj
, hwmon_cputemp
[i
]);
134 #define CPU_THERMAL_THRESHOLD 90000
135 static struct delayed_work thermal_work
;
137 static void do_thermal_timer(struct work_struct
*work
)
139 int i
, value
, temp_max
= 0;
141 for (i
=0; i
<nr_packages
; i
++) {
142 value
= loongson3_cpu_temp(i
);
143 if (value
> temp_max
)
147 if (temp_max
<= CPU_THERMAL_THRESHOLD
)
148 schedule_delayed_work(&thermal_work
, msecs_to_jiffies(5000));
150 orderly_poweroff(true);
153 static int __init
loongson_hwmon_init(void)
157 pr_info("Loongson Hwmon Enter...\n");
159 cpu_hwmon_dev
= hwmon_device_register(NULL
);
160 if (IS_ERR(cpu_hwmon_dev
)) {
162 pr_err("hwmon_device_register fail!\n");
163 goto fail_hwmon_device_register
;
166 nr_packages
= loongson_sysconf
.nr_cpus
/
167 loongson_sysconf
.cores_per_package
;
169 ret
= sysfs_create_group(&cpu_hwmon_dev
->kobj
,
170 &cpu_hwmon_attribute_group
);
172 pr_err("fail to create loongson hwmon!\n");
173 goto fail_sysfs_create_group_hwmon
;
176 ret
= create_sysfs_cputemp_files(&cpu_hwmon_dev
->kobj
);
178 pr_err("fail to create cpu temperature interface!\n");
179 goto fail_create_sysfs_cputemp_files
;
182 INIT_DEFERRABLE_WORK(&thermal_work
, do_thermal_timer
);
183 schedule_delayed_work(&thermal_work
, msecs_to_jiffies(20000));
187 fail_create_sysfs_cputemp_files
:
188 sysfs_remove_group(&cpu_hwmon_dev
->kobj
,
189 &cpu_hwmon_attribute_group
);
191 fail_sysfs_create_group_hwmon
:
192 hwmon_device_unregister(cpu_hwmon_dev
);
194 fail_hwmon_device_register
:
198 static void __exit
loongson_hwmon_exit(void)
200 cancel_delayed_work_sync(&thermal_work
);
201 remove_sysfs_cputemp_files(&cpu_hwmon_dev
->kobj
);
202 sysfs_remove_group(&cpu_hwmon_dev
->kobj
,
203 &cpu_hwmon_attribute_group
);
204 hwmon_device_unregister(cpu_hwmon_dev
);
207 module_init(loongson_hwmon_init
);
208 module_exit(loongson_hwmon_exit
);
210 MODULE_AUTHOR("Yu Xiang <xiangy@lemote.com>");
211 MODULE_AUTHOR("Huacai Chen <chenhc@lemote.com>");
212 MODULE_DESCRIPTION("Loongson CPU Hwmon driver");
213 MODULE_LICENSE("GPL");