18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Co., Ltd. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci#include <linux/err.h> 68c2ecf20Sopenharmony_ci#include <linux/module.h> 78c2ecf20Sopenharmony_ci#include <linux/reboot.h> 88c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 98c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 108c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <loongson.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_cistatic int nr_packages; 158c2ecf20Sopenharmony_cistatic struct device *cpu_hwmon_dev; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistatic int loongson3_cpu_temp(int cpu) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci u32 reg; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci reg = iocsr_read32(LOONGARCH_IOCSR_CPUTEMP) & 0xff; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci return (int)((s8)reg) * 1000; 248c2ecf20Sopenharmony_ci} 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic ssize_t cpu_temp_label(struct device *dev, 278c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci int id = (to_sensor_dev_attr(attr))->index - 1; 308c2ecf20Sopenharmony_ci return sprintf(buf, "CPU %d Temperature\n", id); 318c2ecf20Sopenharmony_ci} 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic ssize_t get_cpu_temp(struct device *dev, 348c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci int id = (to_sensor_dev_attr(attr))->index - 1; 378c2ecf20Sopenharmony_ci int value = loongson3_cpu_temp(id); 388c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", value); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_input, 0444, get_cpu_temp, NULL, 1); 428c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_label, 0444, cpu_temp_label, NULL, 1); 438c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_input, 0444, get_cpu_temp, NULL, 2); 448c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp2_label, 0444, cpu_temp_label, NULL, 2); 458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp3_input, 0444, get_cpu_temp, NULL, 3); 468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp3_label, 0444, cpu_temp_label, NULL, 3); 478c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp4_input, 0444, get_cpu_temp, NULL, 4); 488c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp4_label, 0444, cpu_temp_label, NULL, 4); 498c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp5_input, 0444, get_cpu_temp, NULL, 4); 508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp5_label, 0444, cpu_temp_label, NULL, 4); 518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp6_input, 0444, get_cpu_temp, NULL, 4); 528c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp6_label, 0444, cpu_temp_label, NULL, 4); 538c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp7_input, 0444, get_cpu_temp, NULL, 4); 548c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp7_label, 0444, cpu_temp_label, NULL, 4); 558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp8_input, 0444, get_cpu_temp, NULL, 4); 568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp8_label, 0444, cpu_temp_label, NULL, 4); 578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp9_input, 0444, get_cpu_temp, NULL, 4); 588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp9_label, 0444, cpu_temp_label, NULL, 4); 598c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp10_input, 0444, get_cpu_temp, NULL, 4); 608c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp10_label, 0444, cpu_temp_label, NULL, 4); 618c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp11_input, 0444, get_cpu_temp, NULL, 4); 628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp11_label, 0444, cpu_temp_label, NULL, 4); 638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp12_input, 0444, get_cpu_temp, NULL, 4); 648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp12_label, 0444, cpu_temp_label, NULL, 4); 658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp13_input, 0444, get_cpu_temp, NULL, 4); 668c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp13_label, 0444, cpu_temp_label, NULL, 4); 678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp14_input, 0444, get_cpu_temp, NULL, 4); 688c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp14_label, 0444, cpu_temp_label, NULL, 4); 698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp15_input, 0444, get_cpu_temp, NULL, 4); 708c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp15_label, 0444, cpu_temp_label, NULL, 4); 718c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp16_input, 0444, get_cpu_temp, NULL, 4); 728c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp16_label, 0444, cpu_temp_label, NULL, 4); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic struct attribute *cpu_hwmon_attributes[] = { 758c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 768c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_label.dev_attr.attr, 778c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_input.dev_attr.attr, 788c2ecf20Sopenharmony_ci &sensor_dev_attr_temp2_label.dev_attr.attr, 798c2ecf20Sopenharmony_ci &sensor_dev_attr_temp3_input.dev_attr.attr, 808c2ecf20Sopenharmony_ci &sensor_dev_attr_temp3_label.dev_attr.attr, 818c2ecf20Sopenharmony_ci &sensor_dev_attr_temp4_input.dev_attr.attr, 828c2ecf20Sopenharmony_ci &sensor_dev_attr_temp4_label.dev_attr.attr, 838c2ecf20Sopenharmony_ci &sensor_dev_attr_temp5_input.dev_attr.attr, 848c2ecf20Sopenharmony_ci &sensor_dev_attr_temp5_label.dev_attr.attr, 858c2ecf20Sopenharmony_ci &sensor_dev_attr_temp6_input.dev_attr.attr, 868c2ecf20Sopenharmony_ci &sensor_dev_attr_temp6_label.dev_attr.attr, 878c2ecf20Sopenharmony_ci &sensor_dev_attr_temp7_input.dev_attr.attr, 888c2ecf20Sopenharmony_ci &sensor_dev_attr_temp7_label.dev_attr.attr, 898c2ecf20Sopenharmony_ci &sensor_dev_attr_temp8_input.dev_attr.attr, 908c2ecf20Sopenharmony_ci &sensor_dev_attr_temp8_label.dev_attr.attr, 918c2ecf20Sopenharmony_ci &sensor_dev_attr_temp9_input.dev_attr.attr, 928c2ecf20Sopenharmony_ci &sensor_dev_attr_temp9_label.dev_attr.attr, 938c2ecf20Sopenharmony_ci &sensor_dev_attr_temp10_input.dev_attr.attr, 948c2ecf20Sopenharmony_ci &sensor_dev_attr_temp10_label.dev_attr.attr, 958c2ecf20Sopenharmony_ci &sensor_dev_attr_temp11_input.dev_attr.attr, 968c2ecf20Sopenharmony_ci &sensor_dev_attr_temp11_label.dev_attr.attr, 978c2ecf20Sopenharmony_ci &sensor_dev_attr_temp12_input.dev_attr.attr, 988c2ecf20Sopenharmony_ci &sensor_dev_attr_temp12_label.dev_attr.attr, 998c2ecf20Sopenharmony_ci &sensor_dev_attr_temp13_input.dev_attr.attr, 1008c2ecf20Sopenharmony_ci &sensor_dev_attr_temp13_label.dev_attr.attr, 1018c2ecf20Sopenharmony_ci &sensor_dev_attr_temp14_input.dev_attr.attr, 1028c2ecf20Sopenharmony_ci &sensor_dev_attr_temp14_label.dev_attr.attr, 1038c2ecf20Sopenharmony_ci &sensor_dev_attr_temp15_input.dev_attr.attr, 1048c2ecf20Sopenharmony_ci &sensor_dev_attr_temp15_label.dev_attr.attr, 1058c2ecf20Sopenharmony_ci &sensor_dev_attr_temp16_input.dev_attr.attr, 1068c2ecf20Sopenharmony_ci &sensor_dev_attr_temp16_label.dev_attr.attr, 1078c2ecf20Sopenharmony_ci NULL 1088c2ecf20Sopenharmony_ci}; 1098c2ecf20Sopenharmony_cistatic umode_t cpu_hwmon_is_visible(struct kobject *kobj, 1108c2ecf20Sopenharmony_ci struct attribute *attr, int i) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci int id = i / 2; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (id < nr_packages) 1158c2ecf20Sopenharmony_ci return attr->mode; 1168c2ecf20Sopenharmony_ci return 0; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic struct attribute_group cpu_hwmon_group = { 1208c2ecf20Sopenharmony_ci .attrs = cpu_hwmon_attributes, 1218c2ecf20Sopenharmony_ci .is_visible = cpu_hwmon_is_visible, 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic const struct attribute_group *cpu_hwmon_groups[] = { 1258c2ecf20Sopenharmony_ci &cpu_hwmon_group, 1268c2ecf20Sopenharmony_ci NULL 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic int cpu_initial_threshold = 72000; 1308c2ecf20Sopenharmony_cistatic int cpu_thermal_threshold = 96000; 1318c2ecf20Sopenharmony_cimodule_param(cpu_thermal_threshold, int, 0644); 1328c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cpu_thermal_threshold, "cpu thermal threshold (96000 (default))"); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic struct delayed_work thermal_work; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic void do_thermal_timer(struct work_struct *work) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci int i, value, temp_max = 0; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci for (i=0; i<nr_packages; i++) { 1418c2ecf20Sopenharmony_ci value = loongson3_cpu_temp(i); 1428c2ecf20Sopenharmony_ci if (value > temp_max) 1438c2ecf20Sopenharmony_ci temp_max = value; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci if (temp_max <= cpu_thermal_threshold) 1478c2ecf20Sopenharmony_ci schedule_delayed_work(&thermal_work, msecs_to_jiffies(5000)); 1488c2ecf20Sopenharmony_ci else 1498c2ecf20Sopenharmony_ci orderly_poweroff(true); 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic int __init loongson_hwmon_init(void) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci int i, value, temp_max = 0; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci pr_info("Loongson Hwmon Enter...\n"); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci nr_packages = loongson_sysconf.nr_cpus / 1598c2ecf20Sopenharmony_ci loongson_sysconf.cores_per_package; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci cpu_hwmon_dev = hwmon_device_register_with_groups(NULL, "cpu_hwmon", 1628c2ecf20Sopenharmony_ci NULL, cpu_hwmon_groups); 1638c2ecf20Sopenharmony_ci if (IS_ERR(cpu_hwmon_dev)) { 1648c2ecf20Sopenharmony_ci pr_err("Hwmon register fail with %ld!\n", PTR_ERR(cpu_hwmon_dev)); 1658c2ecf20Sopenharmony_ci return PTR_ERR(cpu_hwmon_dev); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci for (i = 0; i < nr_packages; i++) { 1698c2ecf20Sopenharmony_ci value = loongson3_cpu_temp(i); 1708c2ecf20Sopenharmony_ci if (value > temp_max) 1718c2ecf20Sopenharmony_ci temp_max = value; 1728c2ecf20Sopenharmony_ci } 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci pr_info("Initial CPU temperature is %d (highest).\n", temp_max); 1758c2ecf20Sopenharmony_ci if (temp_max > cpu_initial_threshold) 1768c2ecf20Sopenharmony_ci cpu_thermal_threshold += temp_max - cpu_initial_threshold; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci INIT_DEFERRABLE_WORK(&thermal_work, do_thermal_timer); 1798c2ecf20Sopenharmony_ci schedule_delayed_work(&thermal_work, msecs_to_jiffies(20000)); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci return 0; 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic void __exit loongson_hwmon_exit(void) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&thermal_work); 1878c2ecf20Sopenharmony_ci hwmon_device_unregister(cpu_hwmon_dev); 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cimodule_init(loongson_hwmon_init); 1918c2ecf20Sopenharmony_cimodule_exit(loongson_hwmon_exit); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yu Xiang <xiangy@lemote.com>"); 1948c2ecf20Sopenharmony_ciMODULE_AUTHOR("Huacai Chen <chenhc@lemote.com>"); 1958c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Loongson CPU Hwmon driver"); 1968c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 197