1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2014-2015 Qualcomm Atheros, Inc.
4 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
7 #include <linux/device.h>
8 #include <linux/sysfs.h>
9 #include <linux/thermal.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
17 ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
20 *state = ATH10K_THERMAL_THROTTLE_MAX;
26 ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
29 struct ath10k *ar = cdev->devdata;
31 mutex_lock(&ar->conf_mutex);
32 *state = ar->thermal.throttle_state;
33 mutex_unlock(&ar->conf_mutex);
39 ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
40 unsigned long throttle_state)
42 struct ath10k *ar = cdev->devdata;
44 if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) {
45 ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n",
46 throttle_state, ATH10K_THERMAL_THROTTLE_MAX);
49 mutex_lock(&ar->conf_mutex);
50 ar->thermal.throttle_state = throttle_state;
51 ath10k_thermal_set_throttling(ar);
52 mutex_unlock(&ar->conf_mutex);
56 static const struct thermal_cooling_device_ops ath10k_thermal_ops = {
57 .get_max_state = ath10k_thermal_get_max_throttle_state,
58 .get_cur_state = ath10k_thermal_get_cur_throttle_state,
59 .set_cur_state = ath10k_thermal_set_cur_throttle_state,
62 static ssize_t ath10k_thermal_show_temp(struct device *dev,
63 struct device_attribute *attr,
66 struct ath10k *ar = dev_get_drvdata(dev);
68 unsigned long time_left;
70 mutex_lock(&ar->conf_mutex);
72 /* Can't get temperature when the card is off */
73 if (ar->state != ATH10K_STATE_ON) {
78 reinit_completion(&ar->thermal.wmi_sync);
79 ret = ath10k_wmi_pdev_get_temperature(ar);
81 ath10k_warn(ar, "failed to read temperature %d\n", ret);
85 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
90 time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync,
91 ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
93 ath10k_warn(ar, "failed to synchronize thermal read\n");
98 spin_lock_bh(&ar->data_lock);
99 temperature = ar->thermal.temperature;
100 spin_unlock_bh(&ar->data_lock);
102 /* display in millidegree celsius */
103 ret = sysfs_emit(buf, "%d\n", temperature * 1000);
105 mutex_unlock(&ar->conf_mutex);
109 void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
111 spin_lock_bh(&ar->data_lock);
112 ar->thermal.temperature = temperature;
113 spin_unlock_bh(&ar->data_lock);
114 complete(&ar->thermal.wmi_sync);
117 static SENSOR_DEVICE_ATTR(temp1_input, 0444, ath10k_thermal_show_temp,
120 static struct attribute *ath10k_hwmon_attrs[] = {
121 &sensor_dev_attr_temp1_input.dev_attr.attr,
124 ATTRIBUTE_GROUPS(ath10k_hwmon);
126 void ath10k_thermal_set_throttling(struct ath10k *ar)
128 u32 period, duration, enabled;
131 lockdep_assert_held(&ar->conf_mutex);
133 if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))
136 if (!ar->wmi.ops->gen_pdev_set_quiet_mode)
139 if (ar->state != ATH10K_STATE_ON)
142 period = ar->thermal.quiet_period;
143 duration = (period * ar->thermal.throttle_state) / 100;
144 enabled = duration ? 1 : 0;
146 ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
147 ATH10K_QUIET_START_OFFSET,
150 ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
151 period, duration, enabled, ret);
155 int ath10k_thermal_register(struct ath10k *ar)
157 struct thermal_cooling_device *cdev;
158 struct device *hwmon_dev;
161 if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))
164 cdev = thermal_cooling_device_register("ath10k_thermal", ar,
165 &ath10k_thermal_ops);
168 ath10k_err(ar, "failed to setup thermal device result: %ld\n",
173 ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
176 ath10k_err(ar, "failed to create cooling device symlink\n");
177 goto err_cooling_destroy;
180 ar->thermal.cdev = cdev;
181 ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT;
183 /* Do not register hwmon device when temperature reading is not
184 * supported by firmware
186 if (!(ar->wmi.ops->gen_pdev_get_temperature))
189 /* Avoid linking error on devm_hwmon_device_register_with_groups, I
190 * guess linux/hwmon.h is missing proper stubs.
192 if (!IS_REACHABLE(CONFIG_HWMON))
195 hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
197 ath10k_hwmon_groups);
198 if (IS_ERR(hwmon_dev)) {
199 ath10k_err(ar, "failed to register hwmon device: %ld\n",
202 goto err_remove_link;
207 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
209 thermal_cooling_device_unregister(cdev);
213 void ath10k_thermal_unregister(struct ath10k *ar)
215 if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))
218 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
219 thermal_cooling_device_unregister(ar->thermal.cdev);