0001
0002 #include <linux/err.h>
0003 #include <linux/module.h>
0004 #include <linux/reboot.h>
0005 #include <linux/jiffies.h>
0006 #include <linux/hwmon.h>
0007 #include <linux/hwmon-sysfs.h>
0008
0009 #include <loongson.h>
0010 #include <boot_param.h>
0011 #include <loongson_hwmon.h>
0012 #include <loongson_regs.h>
0013
0014 static int csr_temp_enable;
0015
0016
0017
0018
0019
0020
0021
0022 int loongson3_cpu_temp(int cpu)
0023 {
0024 u32 reg, prid_rev;
0025
0026 if (csr_temp_enable) {
0027 reg = (csr_readl(LOONGSON_CSR_CPUTEMP) & 0xff);
0028 goto out;
0029 }
0030
0031 reg = LOONGSON_CHIPTEMP(cpu);
0032 prid_rev = read_c0_prid() & PRID_REV_MASK;
0033
0034 switch (prid_rev) {
0035 case PRID_REV_LOONGSON3A_R1:
0036 reg = (reg >> 8) & 0xff;
0037 break;
0038 case PRID_REV_LOONGSON3B_R1:
0039 case PRID_REV_LOONGSON3B_R2:
0040 case PRID_REV_LOONGSON3A_R2_0:
0041 case PRID_REV_LOONGSON3A_R2_1:
0042 reg = ((reg >> 8) & 0xff) - 100;
0043 break;
0044 case PRID_REV_LOONGSON3A_R3_0:
0045 case PRID_REV_LOONGSON3A_R3_1:
0046 default:
0047 reg = (reg & 0xffff) * 731 / 0x4000 - 273;
0048 break;
0049 }
0050
0051 out:
0052 return (int)reg * 1000;
0053 }
0054
0055 static int nr_packages;
0056 static struct device *cpu_hwmon_dev;
0057
0058 static ssize_t cpu_temp_label(struct device *dev,
0059 struct device_attribute *attr, char *buf)
0060 {
0061 int id = (to_sensor_dev_attr(attr))->index - 1;
0062
0063 return sprintf(buf, "CPU %d Temperature\n", id);
0064 }
0065
0066 static ssize_t get_cpu_temp(struct device *dev,
0067 struct device_attribute *attr, char *buf)
0068 {
0069 int id = (to_sensor_dev_attr(attr))->index - 1;
0070 int value = loongson3_cpu_temp(id);
0071
0072 return sprintf(buf, "%d\n", value);
0073 }
0074
0075 static SENSOR_DEVICE_ATTR(temp1_input, 0444, get_cpu_temp, NULL, 1);
0076 static SENSOR_DEVICE_ATTR(temp1_label, 0444, cpu_temp_label, NULL, 1);
0077 static SENSOR_DEVICE_ATTR(temp2_input, 0444, get_cpu_temp, NULL, 2);
0078 static SENSOR_DEVICE_ATTR(temp2_label, 0444, cpu_temp_label, NULL, 2);
0079 static SENSOR_DEVICE_ATTR(temp3_input, 0444, get_cpu_temp, NULL, 3);
0080 static SENSOR_DEVICE_ATTR(temp3_label, 0444, cpu_temp_label, NULL, 3);
0081 static SENSOR_DEVICE_ATTR(temp4_input, 0444, get_cpu_temp, NULL, 4);
0082 static SENSOR_DEVICE_ATTR(temp4_label, 0444, cpu_temp_label, NULL, 4);
0083
0084 static struct attribute *cpu_hwmon_attributes[] = {
0085 &sensor_dev_attr_temp1_input.dev_attr.attr,
0086 &sensor_dev_attr_temp1_label.dev_attr.attr,
0087 &sensor_dev_attr_temp2_input.dev_attr.attr,
0088 &sensor_dev_attr_temp2_label.dev_attr.attr,
0089 &sensor_dev_attr_temp3_input.dev_attr.attr,
0090 &sensor_dev_attr_temp3_label.dev_attr.attr,
0091 &sensor_dev_attr_temp4_input.dev_attr.attr,
0092 &sensor_dev_attr_temp4_label.dev_attr.attr,
0093 NULL
0094 };
0095
0096 static umode_t cpu_hwmon_is_visible(struct kobject *kobj,
0097 struct attribute *attr, int i)
0098 {
0099 int id = i / 2;
0100
0101 if (id < nr_packages)
0102 return attr->mode;
0103 return 0;
0104 }
0105
0106 static struct attribute_group cpu_hwmon_group = {
0107 .attrs = cpu_hwmon_attributes,
0108 .is_visible = cpu_hwmon_is_visible,
0109 };
0110
0111 static const struct attribute_group *cpu_hwmon_groups[] = {
0112 &cpu_hwmon_group,
0113 NULL
0114 };
0115
0116 #define CPU_THERMAL_THRESHOLD 90000
0117 static struct delayed_work thermal_work;
0118
0119 static void do_thermal_timer(struct work_struct *work)
0120 {
0121 int i, value;
0122
0123 for (i = 0; i < nr_packages; i++) {
0124 value = loongson3_cpu_temp(i);
0125 if (value > CPU_THERMAL_THRESHOLD) {
0126 pr_emerg("Power off due to high temp: %d\n", value);
0127 orderly_poweroff(true);
0128 }
0129 }
0130
0131 schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000));
0132 }
0133
0134 static int __init loongson_hwmon_init(void)
0135 {
0136 pr_info("Loongson Hwmon Enter...\n");
0137
0138 if (cpu_has_csr())
0139 csr_temp_enable = csr_readl(LOONGSON_CSR_FEATURES) &
0140 LOONGSON_CSRF_TEMP;
0141
0142 nr_packages = loongson_sysconf.nr_cpus /
0143 loongson_sysconf.cores_per_package;
0144
0145 cpu_hwmon_dev = hwmon_device_register_with_groups(NULL, "cpu_hwmon",
0146 NULL, cpu_hwmon_groups);
0147 if (IS_ERR(cpu_hwmon_dev)) {
0148 pr_err("hwmon_device_register fail!\n");
0149 return PTR_ERR(cpu_hwmon_dev);
0150 }
0151
0152 INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer);
0153 schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000));
0154
0155 return 0;
0156 }
0157
0158 static void __exit loongson_hwmon_exit(void)
0159 {
0160 cancel_delayed_work_sync(&thermal_work);
0161 hwmon_device_unregister(cpu_hwmon_dev);
0162 }
0163
0164 module_init(loongson_hwmon_init);
0165 module_exit(loongson_hwmon_exit);
0166
0167 MODULE_AUTHOR("Yu Xiang <xiangy@lemote.com>");
0168 MODULE_AUTHOR("Huacai Chen <chenhc@lemote.com>");
0169 MODULE_DESCRIPTION("Loongson CPU Hwmon driver");
0170 MODULE_LICENSE("GPL");