18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: ISC 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014-2015 Qualcomm Atheros, Inc. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/device.h> 78c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 88c2ecf20Sopenharmony_ci#include <linux/thermal.h> 98c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 108c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 118c2ecf20Sopenharmony_ci#include "core.h" 128c2ecf20Sopenharmony_ci#include "debug.h" 138c2ecf20Sopenharmony_ci#include "wmi-ops.h" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_cistatic int 168c2ecf20Sopenharmony_ciath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev, 178c2ecf20Sopenharmony_ci unsigned long *state) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci *state = ATH10K_THERMAL_THROTTLE_MAX; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci return 0; 228c2ecf20Sopenharmony_ci} 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic int 258c2ecf20Sopenharmony_ciath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev, 268c2ecf20Sopenharmony_ci unsigned long *state) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci struct ath10k *ar = cdev->devdata; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci mutex_lock(&ar->conf_mutex); 318c2ecf20Sopenharmony_ci *state = ar->thermal.throttle_state; 328c2ecf20Sopenharmony_ci mutex_unlock(&ar->conf_mutex); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci return 0; 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic int 388c2ecf20Sopenharmony_ciath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev, 398c2ecf20Sopenharmony_ci unsigned long throttle_state) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci struct ath10k *ar = cdev->devdata; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) { 448c2ecf20Sopenharmony_ci ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n", 458c2ecf20Sopenharmony_ci throttle_state, ATH10K_THERMAL_THROTTLE_MAX); 468c2ecf20Sopenharmony_ci return -EINVAL; 478c2ecf20Sopenharmony_ci } 488c2ecf20Sopenharmony_ci mutex_lock(&ar->conf_mutex); 498c2ecf20Sopenharmony_ci ar->thermal.throttle_state = throttle_state; 508c2ecf20Sopenharmony_ci ath10k_thermal_set_throttling(ar); 518c2ecf20Sopenharmony_ci mutex_unlock(&ar->conf_mutex); 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic const struct thermal_cooling_device_ops ath10k_thermal_ops = { 568c2ecf20Sopenharmony_ci .get_max_state = ath10k_thermal_get_max_throttle_state, 578c2ecf20Sopenharmony_ci .get_cur_state = ath10k_thermal_get_cur_throttle_state, 588c2ecf20Sopenharmony_ci .set_cur_state = ath10k_thermal_set_cur_throttle_state, 598c2ecf20Sopenharmony_ci}; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic ssize_t ath10k_thermal_show_temp(struct device *dev, 628c2ecf20Sopenharmony_ci struct device_attribute *attr, 638c2ecf20Sopenharmony_ci char *buf) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci struct ath10k *ar = dev_get_drvdata(dev); 668c2ecf20Sopenharmony_ci int ret, temperature; 678c2ecf20Sopenharmony_ci unsigned long time_left; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci mutex_lock(&ar->conf_mutex); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* Can't get temperature when the card is off */ 728c2ecf20Sopenharmony_ci if (ar->state != ATH10K_STATE_ON) { 738c2ecf20Sopenharmony_ci ret = -ENETDOWN; 748c2ecf20Sopenharmony_ci goto out; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci reinit_completion(&ar->thermal.wmi_sync); 788c2ecf20Sopenharmony_ci ret = ath10k_wmi_pdev_get_temperature(ar); 798c2ecf20Sopenharmony_ci if (ret) { 808c2ecf20Sopenharmony_ci ath10k_warn(ar, "failed to read temperature %d\n", ret); 818c2ecf20Sopenharmony_ci goto out; 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) { 858c2ecf20Sopenharmony_ci ret = -ESHUTDOWN; 868c2ecf20Sopenharmony_ci goto out; 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync, 908c2ecf20Sopenharmony_ci ATH10K_THERMAL_SYNC_TIMEOUT_HZ); 918c2ecf20Sopenharmony_ci if (!time_left) { 928c2ecf20Sopenharmony_ci ath10k_warn(ar, "failed to synchronize thermal read\n"); 938c2ecf20Sopenharmony_ci ret = -ETIMEDOUT; 948c2ecf20Sopenharmony_ci goto out; 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci spin_lock_bh(&ar->data_lock); 988c2ecf20Sopenharmony_ci temperature = ar->thermal.temperature; 998c2ecf20Sopenharmony_ci spin_unlock_bh(&ar->data_lock); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci /* display in millidegree celcius */ 1028c2ecf20Sopenharmony_ci ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000); 1038c2ecf20Sopenharmony_ciout: 1048c2ecf20Sopenharmony_ci mutex_unlock(&ar->conf_mutex); 1058c2ecf20Sopenharmony_ci return ret; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_civoid ath10k_thermal_event_temperature(struct ath10k *ar, int temperature) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci spin_lock_bh(&ar->data_lock); 1118c2ecf20Sopenharmony_ci ar->thermal.temperature = temperature; 1128c2ecf20Sopenharmony_ci spin_unlock_bh(&ar->data_lock); 1138c2ecf20Sopenharmony_ci complete(&ar->thermal.wmi_sync); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR(temp1_input, 0444, ath10k_thermal_show_temp, 1178c2ecf20Sopenharmony_ci NULL, 0); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic struct attribute *ath10k_hwmon_attrs[] = { 1208c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 1218c2ecf20Sopenharmony_ci NULL, 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(ath10k_hwmon); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_civoid ath10k_thermal_set_throttling(struct ath10k *ar) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci u32 period, duration, enabled; 1288c2ecf20Sopenharmony_ci int ret; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci lockdep_assert_held(&ar->conf_mutex); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map)) 1338c2ecf20Sopenharmony_ci return; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (!ar->wmi.ops->gen_pdev_set_quiet_mode) 1368c2ecf20Sopenharmony_ci return; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci if (ar->state != ATH10K_STATE_ON) 1398c2ecf20Sopenharmony_ci return; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci period = ar->thermal.quiet_period; 1428c2ecf20Sopenharmony_ci duration = (period * ar->thermal.throttle_state) / 100; 1438c2ecf20Sopenharmony_ci enabled = duration ? 1 : 0; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration, 1468c2ecf20Sopenharmony_ci ATH10K_QUIET_START_OFFSET, 1478c2ecf20Sopenharmony_ci enabled); 1488c2ecf20Sopenharmony_ci if (ret) { 1498c2ecf20Sopenharmony_ci ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n", 1508c2ecf20Sopenharmony_ci period, duration, enabled, ret); 1518c2ecf20Sopenharmony_ci } 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ciint ath10k_thermal_register(struct ath10k *ar) 1558c2ecf20Sopenharmony_ci{ 1568c2ecf20Sopenharmony_ci struct thermal_cooling_device *cdev; 1578c2ecf20Sopenharmony_ci struct device *hwmon_dev; 1588c2ecf20Sopenharmony_ci int ret; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map)) 1618c2ecf20Sopenharmony_ci return 0; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci cdev = thermal_cooling_device_register("ath10k_thermal", ar, 1648c2ecf20Sopenharmony_ci &ath10k_thermal_ops); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci if (IS_ERR(cdev)) { 1678c2ecf20Sopenharmony_ci ath10k_err(ar, "failed to setup thermal device result: %ld\n", 1688c2ecf20Sopenharmony_ci PTR_ERR(cdev)); 1698c2ecf20Sopenharmony_ci return -EINVAL; 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj, 1738c2ecf20Sopenharmony_ci "cooling_device"); 1748c2ecf20Sopenharmony_ci if (ret) { 1758c2ecf20Sopenharmony_ci ath10k_err(ar, "failed to create cooling device symlink\n"); 1768c2ecf20Sopenharmony_ci goto err_cooling_destroy; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci ar->thermal.cdev = cdev; 1808c2ecf20Sopenharmony_ci ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* Do not register hwmon device when temperature reading is not 1838c2ecf20Sopenharmony_ci * supported by firmware 1848c2ecf20Sopenharmony_ci */ 1858c2ecf20Sopenharmony_ci if (!(ar->wmi.ops->gen_pdev_get_temperature)) 1868c2ecf20Sopenharmony_ci return 0; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci /* Avoid linking error on devm_hwmon_device_register_with_groups, I 1898c2ecf20Sopenharmony_ci * guess linux/hwmon.h is missing proper stubs. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_ci if (!IS_REACHABLE(CONFIG_HWMON)) 1928c2ecf20Sopenharmony_ci return 0; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev, 1958c2ecf20Sopenharmony_ci "ath10k_hwmon", ar, 1968c2ecf20Sopenharmony_ci ath10k_hwmon_groups); 1978c2ecf20Sopenharmony_ci if (IS_ERR(hwmon_dev)) { 1988c2ecf20Sopenharmony_ci ath10k_err(ar, "failed to register hwmon device: %ld\n", 1998c2ecf20Sopenharmony_ci PTR_ERR(hwmon_dev)); 2008c2ecf20Sopenharmony_ci ret = -EINVAL; 2018c2ecf20Sopenharmony_ci goto err_remove_link; 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci return 0; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cierr_remove_link: 2068c2ecf20Sopenharmony_ci sysfs_remove_link(&ar->dev->kobj, "cooling_device"); 2078c2ecf20Sopenharmony_cierr_cooling_destroy: 2088c2ecf20Sopenharmony_ci thermal_cooling_device_unregister(cdev); 2098c2ecf20Sopenharmony_ci return ret; 2108c2ecf20Sopenharmony_ci} 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_civoid ath10k_thermal_unregister(struct ath10k *ar) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map)) 2158c2ecf20Sopenharmony_ci return; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci sysfs_remove_link(&ar->dev->kobj, "cooling_device"); 2188c2ecf20Sopenharmony_ci thermal_cooling_device_unregister(ar->thermal.cdev); 2198c2ecf20Sopenharmony_ci} 220