Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Qualcomm Atheros, Inc. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/sysfs.h> |
| 19 | #include <linux/thermal.h> |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 20 | #include <linux/hwmon.h> |
| 21 | #include <linux/hwmon-sysfs.h> |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 22 | #include "core.h" |
| 23 | #include "debug.h" |
| 24 | #include "wmi-ops.h" |
| 25 | |
| 26 | static int ath10k_thermal_get_active_vifs(struct ath10k *ar, |
| 27 | enum wmi_vdev_type type) |
| 28 | { |
| 29 | struct ath10k_vif *arvif; |
| 30 | int count = 0; |
| 31 | |
| 32 | lockdep_assert_held(&ar->conf_mutex); |
| 33 | |
| 34 | list_for_each_entry(arvif, &ar->arvifs, list) { |
| 35 | if (!arvif->is_started) |
| 36 | continue; |
| 37 | |
| 38 | if (!arvif->is_up) |
| 39 | continue; |
| 40 | |
| 41 | if (arvif->vdev_type != type) |
| 42 | continue; |
| 43 | |
| 44 | count++; |
| 45 | } |
| 46 | return count; |
| 47 | } |
| 48 | |
| 49 | static int ath10k_thermal_get_max_dutycycle(struct thermal_cooling_device *cdev, |
| 50 | unsigned long *state) |
| 51 | { |
| 52 | *state = ATH10K_QUIET_DUTY_CYCLE_MAX; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int ath10k_thermal_get_cur_dutycycle(struct thermal_cooling_device *cdev, |
| 58 | unsigned long *state) |
| 59 | { |
| 60 | struct ath10k *ar = cdev->devdata; |
| 61 | |
| 62 | mutex_lock(&ar->conf_mutex); |
| 63 | *state = ar->thermal.duty_cycle; |
| 64 | mutex_unlock(&ar->conf_mutex); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int ath10k_thermal_set_cur_dutycycle(struct thermal_cooling_device *cdev, |
| 70 | unsigned long duty_cycle) |
| 71 | { |
| 72 | struct ath10k *ar = cdev->devdata; |
| 73 | u32 period, duration, enabled; |
| 74 | int num_bss, ret = 0; |
| 75 | |
| 76 | mutex_lock(&ar->conf_mutex); |
| 77 | if (ar->state != ATH10K_STATE_ON) { |
| 78 | ret = -ENETDOWN; |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 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); |
| 85 | ret = -EINVAL; |
| 86 | goto out; |
| 87 | } |
| 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) |
| 91 | * mode properly. |
| 92 | */ |
| 93 | num_bss = ath10k_thermal_get_active_vifs(ar, WMI_VDEV_TYPE_AP); |
| 94 | if (!num_bss) { |
| 95 | ath10k_warn(ar, "no active AP interfaces\n"); |
| 96 | ret = -ENETDOWN; |
| 97 | goto out; |
| 98 | } |
| 99 | period = max(ATH10K_QUIET_PERIOD_MIN, |
| 100 | (ATH10K_QUIET_PERIOD_DEFAULT / num_bss)); |
Rajkumar Manoharan | 8bdadac | 2015-01-13 14:52:14 +0530 | [diff] [blame] | 101 | duration = (period * duty_cycle) / 100; |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 102 | enabled = duration ? 1 : 0; |
| 103 | |
| 104 | ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration, |
| 105 | ATH10K_QUIET_START_OFFSET, |
| 106 | enabled); |
| 107 | if (ret) { |
| 108 | ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n", |
| 109 | period, duration, enabled, ret); |
| 110 | goto out; |
| 111 | } |
| 112 | ar->thermal.duty_cycle = duty_cycle; |
| 113 | out: |
| 114 | mutex_unlock(&ar->conf_mutex); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 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, |
| 122 | }; |
| 123 | |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 124 | static ssize_t ath10k_thermal_show_temp(struct device *dev, |
| 125 | struct device_attribute *attr, |
| 126 | char *buf) |
| 127 | { |
| 128 | struct ath10k *ar = dev_get_drvdata(dev); |
| 129 | int ret, temperature; |
| 130 | |
| 131 | mutex_lock(&ar->conf_mutex); |
| 132 | |
| 133 | /* Can't get temperature when the card is off */ |
| 134 | if (ar->state != ATH10K_STATE_ON) { |
| 135 | ret = -ENETDOWN; |
| 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | reinit_completion(&ar->thermal.wmi_sync); |
| 140 | ret = ath10k_wmi_pdev_get_temperature(ar); |
| 141 | if (ret) { |
| 142 | ath10k_warn(ar, "failed to read temperature %d\n", ret); |
| 143 | goto out; |
| 144 | } |
| 145 | |
| 146 | if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) { |
| 147 | ret = -ESHUTDOWN; |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | ret = wait_for_completion_timeout(&ar->thermal.wmi_sync, |
| 152 | ATH10K_THERMAL_SYNC_TIMEOUT_HZ); |
| 153 | if (ret == 0) { |
| 154 | ath10k_warn(ar, "failed to synchronize thermal read\n"); |
| 155 | ret = -ETIMEDOUT; |
| 156 | goto out; |
| 157 | } |
| 158 | |
| 159 | spin_lock_bh(&ar->data_lock); |
| 160 | temperature = ar->thermal.temperature; |
| 161 | spin_unlock_bh(&ar->data_lock); |
| 162 | |
Rajkumar Manoharan | 6d48161 | 2015-01-13 12:44:24 +0530 | [diff] [blame] | 163 | /* display in millidegree celcius */ |
| 164 | ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000); |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 165 | out: |
| 166 | mutex_unlock(&ar->conf_mutex); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature) |
| 171 | { |
| 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); |
| 176 | } |
| 177 | |
| 178 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ath10k_thermal_show_temp, |
| 179 | NULL, 0); |
| 180 | |
| 181 | static struct attribute *ath10k_hwmon_attrs[] = { |
| 182 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 183 | NULL, |
| 184 | }; |
| 185 | ATTRIBUTE_GROUPS(ath10k_hwmon); |
| 186 | |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 187 | int ath10k_thermal_register(struct ath10k *ar) |
| 188 | { |
| 189 | struct thermal_cooling_device *cdev; |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 190 | struct device *hwmon_dev; |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 191 | int ret; |
| 192 | |
| 193 | cdev = thermal_cooling_device_register("ath10k_thermal", ar, |
| 194 | &ath10k_thermal_ops); |
| 195 | |
| 196 | if (IS_ERR(cdev)) { |
| 197 | ath10k_err(ar, "failed to setup thermal device result: %ld\n", |
| 198 | PTR_ERR(cdev)); |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
| 202 | ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj, |
| 203 | "cooling_device"); |
| 204 | if (ret) { |
Rajkumar Manoharan | 7e47e8e | 2015-03-12 19:32:00 +0200 | [diff] [blame^] | 205 | ath10k_err(ar, "failed to create cooling device symlink\n"); |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 206 | goto err_cooling_destroy; |
| 207 | } |
| 208 | |
| 209 | ar->thermal.cdev = cdev; |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 210 | |
| 211 | /* Do not register hwmon device when temperature reading is not |
| 212 | * supported by firmware |
| 213 | */ |
| 214 | if (ar->wmi.op_version != ATH10K_FW_WMI_OP_VERSION_10_2_4) |
| 215 | return 0; |
| 216 | |
Kalle Valo | 96bba98 | 2015-01-07 17:04:10 +0200 | [diff] [blame] | 217 | /* Avoid linking error on devm_hwmon_device_register_with_groups, I |
| 218 | * guess linux/hwmon.h is missing proper stubs. */ |
Rajkumar Manoharan | a4031af | 2015-01-13 14:21:45 +0530 | [diff] [blame] | 219 | if (!config_enabled(CONFIG_HWMON)) |
Kalle Valo | 96bba98 | 2015-01-07 17:04:10 +0200 | [diff] [blame] | 220 | return 0; |
| 221 | |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 222 | hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev, |
| 223 | "ath10k_hwmon", ar, |
| 224 | ath10k_hwmon_groups); |
| 225 | if (IS_ERR(hwmon_dev)) { |
| 226 | ath10k_err(ar, "failed to register hwmon device: %ld\n", |
| 227 | PTR_ERR(hwmon_dev)); |
| 228 | ret = -EINVAL; |
| 229 | goto err_remove_link; |
| 230 | } |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 231 | return 0; |
| 232 | |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 233 | err_remove_link: |
Rajkumar Manoharan | 7e47e8e | 2015-03-12 19:32:00 +0200 | [diff] [blame^] | 234 | sysfs_remove_link(&ar->dev->kobj, "cooling_device"); |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 235 | err_cooling_destroy: |
| 236 | thermal_cooling_device_unregister(cdev); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | void ath10k_thermal_unregister(struct ath10k *ar) |
| 241 | { |
| 242 | thermal_cooling_device_unregister(ar->thermal.cdev); |
| 243 | sysfs_remove_link(&ar->dev->kobj, "cooling_device"); |
| 244 | } |