18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci#include <linux/err.h> 38c2ecf20Sopenharmony_ci#include <linux/module.h> 48c2ecf20Sopenharmony_ci#include <linux/reboot.h> 58c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 68c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 78c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <loongson.h> 108c2ecf20Sopenharmony_ci#include <boot_param.h> 118c2ecf20Sopenharmony_ci#include <loongson_hwmon.h> 128c2ecf20Sopenharmony_ci#include <loongson_regs.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_cistatic int csr_temp_enable; 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/* 178c2ecf20Sopenharmony_ci * Loongson-3 series cpu has two sensors inside, 188c2ecf20Sopenharmony_ci * each of them from 0 to 255, 198c2ecf20Sopenharmony_ci * if more than 127, that is dangerous. 208c2ecf20Sopenharmony_ci * here only provide sensor1 data, because it always hot than sensor0 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_ciint loongson3_cpu_temp(int cpu) 238c2ecf20Sopenharmony_ci{ 248c2ecf20Sopenharmony_ci u32 reg, prid_rev; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci if (csr_temp_enable) { 278c2ecf20Sopenharmony_ci reg = (csr_readl(LOONGSON_CSR_CPUTEMP) & 0xff); 288c2ecf20Sopenharmony_ci goto out; 298c2ecf20Sopenharmony_ci } 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci reg = LOONGSON_CHIPTEMP(cpu); 328c2ecf20Sopenharmony_ci prid_rev = read_c0_prid() & PRID_REV_MASK; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci switch (prid_rev) { 358c2ecf20Sopenharmony_ci case PRID_REV_LOONGSON3A_R1: 368c2ecf20Sopenharmony_ci reg = (reg >> 8) & 0xff; 378c2ecf20Sopenharmony_ci break; 388c2ecf20Sopenharmony_ci case PRID_REV_LOONGSON3B_R1: 398c2ecf20Sopenharmony_ci case PRID_REV_LOONGSON3B_R2: 408c2ecf20Sopenharmony_ci case PRID_REV_LOONGSON3A_R2_0: 418c2ecf20Sopenharmony_ci case PRID_REV_LOONGSON3A_R2_1: 428c2ecf20Sopenharmony_ci reg = ((reg >> 8) & 0xff) - 100; 438c2ecf20Sopenharmony_ci break; 448c2ecf20Sopenharmony_ci case PRID_REV_LOONGSON3A_R3_0: 458c2ecf20Sopenharmony_ci case PRID_REV_LOONGSON3A_R3_1: 468c2ecf20Sopenharmony_ci default: 478c2ecf20Sopenharmony_ci reg = (reg & 0xffff) * 731 / 0x4000 - 273; 488c2ecf20Sopenharmony_ci break; 498c2ecf20Sopenharmony_ci } 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ciout: 528c2ecf20Sopenharmony_ci return (int)reg * 1000; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic int nr_packages; 568c2ecf20Sopenharmony_cistatic struct device *cpu_hwmon_dev; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic ssize_t cpu_temp_label(struct device *dev, 598c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci int id = (to_sensor_dev_attr(attr))->index - 1; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci return sprintf(buf, "CPU %d Temperature\n", id); 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic ssize_t get_cpu_temp(struct device *dev, 678c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci int id = (to_sensor_dev_attr(attr))->index - 1; 708c2ecf20Sopenharmony_ci int value = loongson3_cpu_temp(id); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", value); 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_input, 0444, get_cpu_temp, NULL, 1); 768c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_label, 0444, cpu_temp_label, NULL, 1); 778c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_input, 0444, get_cpu_temp, NULL, 2); 788c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_label, 0444, cpu_temp_label, NULL, 2); 798c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp3_input, 0444, get_cpu_temp, NULL, 3); 808c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp3_label, 0444, cpu_temp_label, NULL, 3); 818c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp4_input, 0444, get_cpu_temp, NULL, 4); 828c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp4_label, 0444, cpu_temp_label, NULL, 4); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic struct attribute *cpu_hwmon_attributes[] = { 858c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 868c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_label.dev_attr.attr, 878c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_input.dev_attr.attr, 888c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_label.dev_attr.attr, 898c2ecf20Sopenharmony_ci &sensor_dev_attr_temp3_input.dev_attr.attr, 908c2ecf20Sopenharmony_ci &sensor_dev_attr_temp3_label.dev_attr.attr, 918c2ecf20Sopenharmony_ci &sensor_dev_attr_temp4_input.dev_attr.attr, 928c2ecf20Sopenharmony_ci &sensor_dev_attr_temp4_label.dev_attr.attr, 938c2ecf20Sopenharmony_ci NULL 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic umode_t cpu_hwmon_is_visible(struct kobject *kobj, 978c2ecf20Sopenharmony_ci struct attribute *attr, int i) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci int id = i / 2; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci if (id < nr_packages) 1028c2ecf20Sopenharmony_ci return attr->mode; 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic struct attribute_group cpu_hwmon_group = { 1078c2ecf20Sopenharmony_ci .attrs = cpu_hwmon_attributes, 1088c2ecf20Sopenharmony_ci .is_visible = cpu_hwmon_is_visible, 1098c2ecf20Sopenharmony_ci}; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic const struct attribute_group *cpu_hwmon_groups[] = { 1128c2ecf20Sopenharmony_ci &cpu_hwmon_group, 1138c2ecf20Sopenharmony_ci NULL 1148c2ecf20Sopenharmony_ci}; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci#define CPU_THERMAL_THRESHOLD 90000 1178c2ecf20Sopenharmony_cistatic struct delayed_work thermal_work; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic void do_thermal_timer(struct work_struct *work) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci int i, value; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci for (i = 0; i < nr_packages; i++) { 1248c2ecf20Sopenharmony_ci value = loongson3_cpu_temp(i); 1258c2ecf20Sopenharmony_ci if (value > CPU_THERMAL_THRESHOLD) { 1268c2ecf20Sopenharmony_ci pr_emerg("Power off due to high temp: %d\n", value); 1278c2ecf20Sopenharmony_ci orderly_poweroff(true); 1288c2ecf20Sopenharmony_ci } 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000)); 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic int __init loongson_hwmon_init(void) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci pr_info("Loongson Hwmon Enter...\n"); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci if (cpu_has_csr()) 1398c2ecf20Sopenharmony_ci csr_temp_enable = csr_readl(LOONGSON_CSR_FEATURES) & 1408c2ecf20Sopenharmony_ci LOONGSON_CSRF_TEMP; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci nr_packages = loongson_sysconf.nr_cpus / 1438c2ecf20Sopenharmony_ci loongson_sysconf.cores_per_package; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci cpu_hwmon_dev = hwmon_device_register_with_groups(NULL, "cpu_hwmon", 1468c2ecf20Sopenharmony_ci NULL, cpu_hwmon_groups); 1478c2ecf20Sopenharmony_ci if (IS_ERR(cpu_hwmon_dev)) { 1488c2ecf20Sopenharmony_ci pr_err("hwmon_device_register fail!\n"); 1498c2ecf20Sopenharmony_ci return PTR_ERR(cpu_hwmon_dev); 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer); 1538c2ecf20Sopenharmony_ci schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000)); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci return 0; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic void __exit loongson_hwmon_exit(void) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&thermal_work); 1618c2ecf20Sopenharmony_ci hwmon_device_unregister(cpu_hwmon_dev); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cimodule_init(loongson_hwmon_init); 1658c2ecf20Sopenharmony_cimodule_exit(loongson_hwmon_exit); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yu Xiang <xiangy@lemote.com>"); 1688c2ecf20Sopenharmony_ciMODULE_AUTHOR("Huacai Chen <chenhc@lemote.com>"); 1698c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Loongson CPU Hwmon driver"); 1708c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 171