2 * Copyright (c) 2014 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/device.h>
18 #include <linux/sysfs.h>
19 #include <linux/thermal.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
26 static int ath10k_thermal_get_active_vifs(struct ath10k *ar,
27 enum wmi_vdev_type type)
29 struct ath10k_vif *arvif;
32 lockdep_assert_held(&ar->conf_mutex);
34 list_for_each_entry(arvif, &ar->arvifs, list) {
35 if (!arvif->is_started)
41 if (arvif->vdev_type != type)
49 static int ath10k_thermal_get_max_dutycycle(struct thermal_cooling_device *cdev,
52 *state = ATH10K_QUIET_DUTY_CYCLE_MAX;
57 static int ath10k_thermal_get_cur_dutycycle(struct thermal_cooling_device *cdev,
60 struct ath10k *ar = cdev->devdata;
62 mutex_lock(&ar->conf_mutex);
63 *state = ar->thermal.duty_cycle;
64 mutex_unlock(&ar->conf_mutex);
69 static int ath10k_thermal_set_cur_dutycycle(struct thermal_cooling_device *cdev,
70 unsigned long duty_cycle)
72 struct ath10k *ar = cdev->devdata;
73 u32 period, duration, enabled;
76 mutex_lock(&ar->conf_mutex);
77 if (ar->state != ATH10K_STATE_ON) {
82 if (duty_cycle > ATH10K_QUIET_DUTY_CYCLE_MAX) {
83 ath10k_warn(ar, "duty cycle %ld is exceeding the limit %d\n",
84 duty_cycle, ATH10K_QUIET_DUTY_CYCLE_MAX);
88 /* TODO: Right now, thermal mitigation is handled only for single/multi
89 * vif AP mode. Since quiet param is not validated in STA mode, it needs
90 * to be investigated further to handle multi STA and multi-vif (AP+STA)
93 num_bss = ath10k_thermal_get_active_vifs(ar, WMI_VDEV_TYPE_AP);
95 ath10k_warn(ar, "no active AP interfaces\n");
99 period = max(ATH10K_QUIET_PERIOD_MIN,
100 (ATH10K_QUIET_PERIOD_DEFAULT / num_bss));
101 duration = (period * duty_cycle) / 100;
102 enabled = duration ? 1 : 0;
104 ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
105 ATH10K_QUIET_START_OFFSET,
108 ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
109 period, duration, enabled, ret);
112 ar->thermal.duty_cycle = duty_cycle;
114 mutex_unlock(&ar->conf_mutex);
118 static struct thermal_cooling_device_ops ath10k_thermal_ops = {
119 .get_max_state = ath10k_thermal_get_max_dutycycle,
120 .get_cur_state = ath10k_thermal_get_cur_dutycycle,
121 .set_cur_state = ath10k_thermal_set_cur_dutycycle,
124 static ssize_t ath10k_thermal_show_temp(struct device *dev,
125 struct device_attribute *attr,
128 struct ath10k *ar = dev_get_drvdata(dev);
129 int ret, temperature;
131 mutex_lock(&ar->conf_mutex);
133 /* Can't get temperature when the card is off */
134 if (ar->state != ATH10K_STATE_ON) {
139 reinit_completion(&ar->thermal.wmi_sync);
140 ret = ath10k_wmi_pdev_get_temperature(ar);
142 ath10k_warn(ar, "failed to read temperature %d\n", ret);
146 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
151 ret = wait_for_completion_timeout(&ar->thermal.wmi_sync,
152 ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
154 ath10k_warn(ar, "failed to synchronize thermal read\n");
159 spin_lock_bh(&ar->data_lock);
160 temperature = ar->thermal.temperature;
161 spin_unlock_bh(&ar->data_lock);
163 /* display in millidegree celcius */
164 ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
166 mutex_unlock(&ar->conf_mutex);
170 void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
172 spin_lock_bh(&ar->data_lock);
173 ar->thermal.temperature = temperature;
174 spin_unlock_bh(&ar->data_lock);
175 complete(&ar->thermal.wmi_sync);
178 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ath10k_thermal_show_temp,
181 static struct attribute *ath10k_hwmon_attrs[] = {
182 &sensor_dev_attr_temp1_input.dev_attr.attr,
185 ATTRIBUTE_GROUPS(ath10k_hwmon);
187 int ath10k_thermal_register(struct ath10k *ar)
189 struct thermal_cooling_device *cdev;
190 struct device *hwmon_dev;
193 cdev = thermal_cooling_device_register("ath10k_thermal", ar,
194 &ath10k_thermal_ops);
197 ath10k_err(ar, "failed to setup thermal device result: %ld\n",
202 ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
205 ath10k_err(ar, "failed to create thermal symlink\n");
206 goto err_cooling_destroy;
209 ar->thermal.cdev = cdev;
211 /* Do not register hwmon device when temperature reading is not
212 * supported by firmware
214 if (ar->wmi.op_version != ATH10K_FW_WMI_OP_VERSION_10_2_4)
217 /* Avoid linking error on devm_hwmon_device_register_with_groups, I
218 * guess linux/hwmon.h is missing proper stubs. */
219 if (!config_enabled(CONFIG_HWMON))
222 hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
224 ath10k_hwmon_groups);
225 if (IS_ERR(hwmon_dev)) {
226 ath10k_err(ar, "failed to register hwmon device: %ld\n",
229 goto err_remove_link;
234 sysfs_remove_link(&ar->dev->kobj, "thermal_sensor");
236 thermal_cooling_device_unregister(cdev);
240 void ath10k_thermal_unregister(struct ath10k *ar)
242 thermal_cooling_device_unregister(ar->thermal.cdev);
243 sysfs_remove_link(&ar->dev->kobj, "cooling_device");