Path: blob/main/sys/contrib/dev/athk/ath10k/thermal.c
106883 views
// SPDX-License-Identifier: ISC1/*2* Copyright (c) 2014-2015 Qualcomm Atheros, Inc.3* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.4*/56#include <linux/device.h>7#include <linux/sysfs.h>8#include <linux/thermal.h>9#include <linux/hwmon.h>10#include <linux/hwmon-sysfs.h>11#include "core.h"12#include "debug.h"13#include "wmi-ops.h"1415static int16ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,17unsigned long *state)18{19*state = ATH10K_THERMAL_THROTTLE_MAX;2021return 0;22}2324static int25ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,26unsigned long *state)27{28struct ath10k *ar = cdev->devdata;2930mutex_lock(&ar->conf_mutex);31*state = ar->thermal.throttle_state;32mutex_unlock(&ar->conf_mutex);3334return 0;35}3637static int38ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,39unsigned long throttle_state)40{41struct ath10k *ar = cdev->devdata;4243if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) {44ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n",45throttle_state, ATH10K_THERMAL_THROTTLE_MAX);46return -EINVAL;47}48mutex_lock(&ar->conf_mutex);49ar->thermal.throttle_state = throttle_state;50ath10k_thermal_set_throttling(ar);51mutex_unlock(&ar->conf_mutex);52return 0;53}5455static const struct thermal_cooling_device_ops ath10k_thermal_ops = {56.get_max_state = ath10k_thermal_get_max_throttle_state,57.get_cur_state = ath10k_thermal_get_cur_throttle_state,58.set_cur_state = ath10k_thermal_set_cur_throttle_state,59};6061static ssize_t ath10k_thermal_show_temp(struct device *dev,62struct device_attribute *attr,63char *buf)64{65struct ath10k *ar = dev_get_drvdata(dev);66int ret, temperature;67unsigned long time_left;6869mutex_lock(&ar->conf_mutex);7071/* Can't get temperature when the card is off */72if (ar->state != ATH10K_STATE_ON) {73ret = -ENETDOWN;74goto out;75}7677reinit_completion(&ar->thermal.wmi_sync);78ret = ath10k_wmi_pdev_get_temperature(ar);79if (ret) {80ath10k_warn(ar, "failed to read temperature %d\n", ret);81goto out;82}8384if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {85ret = -ESHUTDOWN;86goto out;87}8889time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync,90ATH10K_THERMAL_SYNC_TIMEOUT_HZ);91if (!time_left) {92ath10k_warn(ar, "failed to synchronize thermal read\n");93ret = -ETIMEDOUT;94goto out;95}9697spin_lock_bh(&ar->data_lock);98temperature = ar->thermal.temperature;99spin_unlock_bh(&ar->data_lock);100101/* display in millidegree celsius */102ret = sysfs_emit(buf, "%d\n", temperature * 1000);103out:104mutex_unlock(&ar->conf_mutex);105return ret;106}107108void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)109{110spin_lock_bh(&ar->data_lock);111ar->thermal.temperature = temperature;112spin_unlock_bh(&ar->data_lock);113complete(&ar->thermal.wmi_sync);114}115116static SENSOR_DEVICE_ATTR(temp1_input, 0444, ath10k_thermal_show_temp,117NULL, 0);118119static struct attribute *ath10k_hwmon_attrs[] = {120&sensor_dev_attr_temp1_input.dev_attr.attr,121NULL,122};123ATTRIBUTE_GROUPS(ath10k_hwmon);124125void ath10k_thermal_set_throttling(struct ath10k *ar)126{127u32 period, duration, enabled;128int ret;129130lockdep_assert_held(&ar->conf_mutex);131132if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))133return;134135if (!ar->wmi.ops->gen_pdev_set_quiet_mode)136return;137138if (ar->state != ATH10K_STATE_ON)139return;140141period = ar->thermal.quiet_period;142duration = (period * ar->thermal.throttle_state) / 100;143enabled = duration ? 1 : 0;144145ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,146ATH10K_QUIET_START_OFFSET,147enabled);148if (ret) {149ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",150period, duration, enabled, ret);151}152}153154int ath10k_thermal_register(struct ath10k *ar)155{156struct thermal_cooling_device *cdev;157struct device *hwmon_dev;158int ret;159160if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))161return 0;162163cdev = thermal_cooling_device_register("ath10k_thermal", ar,164&ath10k_thermal_ops);165166if (IS_ERR(cdev)) {167ath10k_err(ar, "failed to setup thermal device result: %ld\n",168PTR_ERR(cdev));169return -EINVAL;170}171172ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,173"cooling_device");174if (ret) {175ath10k_err(ar, "failed to create cooling device symlink\n");176goto err_cooling_destroy;177}178179ar->thermal.cdev = cdev;180ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT;181182/* Do not register hwmon device when temperature reading is not183* supported by firmware184*/185if (!(ar->wmi.ops->gen_pdev_get_temperature))186return 0;187188/* Avoid linking error on devm_hwmon_device_register_with_groups, I189* guess linux/hwmon.h is missing proper stubs.190*/191if (!IS_REACHABLE(CONFIG_HWMON))192return 0;193194hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,195"ath10k_hwmon", ar,196ath10k_hwmon_groups);197if (IS_ERR(hwmon_dev)) {198ath10k_err(ar, "failed to register hwmon device: %ld\n",199PTR_ERR(hwmon_dev));200ret = -EINVAL;201goto err_remove_link;202}203return 0;204205err_remove_link:206sysfs_remove_link(&ar->dev->kobj, "cooling_device");207err_cooling_destroy:208thermal_cooling_device_unregister(cdev);209return ret;210}211212void ath10k_thermal_unregister(struct ath10k *ar)213{214if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))215return;216217sysfs_remove_link(&ar->dev->kobj, "cooling_device");218thermal_cooling_device_unregister(ar->thermal.cdev);219}220221222