Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 1 | /* |
Kalle Valo | 8b1083d | 2017-12-22 18:31:13 +0200 | [diff] [blame] | 2 | * Copyright (c) 2014-2015 Qualcomm Atheros, Inc. |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 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 | |
Rajkumar Manoharan | 972f051 | 2015-03-15 20:36:21 +0530 | [diff] [blame] | 26 | static int |
| 27 | ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev, |
| 28 | unsigned long *state) |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 29 | { |
Rajkumar Manoharan | 972f051 | 2015-03-15 20:36:21 +0530 | [diff] [blame] | 30 | *state = ATH10K_THERMAL_THROTTLE_MAX; |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Rajkumar Manoharan | 972f051 | 2015-03-15 20:36:21 +0530 | [diff] [blame] | 35 | static int |
| 36 | ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev, |
| 37 | unsigned long *state) |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 38 | { |
| 39 | struct ath10k *ar = cdev->devdata; |
| 40 | |
| 41 | mutex_lock(&ar->conf_mutex); |
Rajkumar Manoharan | 972f051 | 2015-03-15 20:36:21 +0530 | [diff] [blame] | 42 | *state = ar->thermal.throttle_state; |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 43 | mutex_unlock(&ar->conf_mutex); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Rajkumar Manoharan | 972f051 | 2015-03-15 20:36:21 +0530 | [diff] [blame] | 48 | static int |
| 49 | ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev, |
| 50 | unsigned long throttle_state) |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 51 | { |
| 52 | struct ath10k *ar = cdev->devdata; |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 53 | |
Rajkumar Manoharan | 28bf0c4 | 2015-03-15 20:36:24 +0530 | [diff] [blame] | 54 | if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) { |
| 55 | ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n", |
| 56 | throttle_state, ATH10K_THERMAL_THROTTLE_MAX); |
| 57 | return -EINVAL; |
| 58 | } |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 59 | mutex_lock(&ar->conf_mutex); |
Rajkumar Manoharan | 28bf0c4 | 2015-03-15 20:36:24 +0530 | [diff] [blame] | 60 | ar->thermal.throttle_state = throttle_state; |
Rajkumar Manoharan | 8515b5c | 2015-03-15 20:36:22 +0530 | [diff] [blame] | 61 | ath10k_thermal_set_throttling(ar); |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 62 | mutex_unlock(&ar->conf_mutex); |
Rajkumar Manoharan | 9936fa5 | 2015-03-15 20:36:25 +0530 | [diff] [blame] | 63 | return 0; |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Bhumika Goyal | 1cdb6c9 | 2017-06-27 17:31:52 +0300 | [diff] [blame] | 66 | static const struct thermal_cooling_device_ops ath10k_thermal_ops = { |
Rajkumar Manoharan | 972f051 | 2015-03-15 20:36:21 +0530 | [diff] [blame] | 67 | .get_max_state = ath10k_thermal_get_max_throttle_state, |
| 68 | .get_cur_state = ath10k_thermal_get_cur_throttle_state, |
| 69 | .set_cur_state = ath10k_thermal_set_cur_throttle_state, |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 72 | static ssize_t ath10k_thermal_show_temp(struct device *dev, |
| 73 | struct device_attribute *attr, |
| 74 | char *buf) |
| 75 | { |
| 76 | struct ath10k *ar = dev_get_drvdata(dev); |
| 77 | int ret, temperature; |
Nicholas Mc Guire | f439539 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 78 | unsigned long time_left; |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 79 | |
| 80 | mutex_lock(&ar->conf_mutex); |
| 81 | |
| 82 | /* Can't get temperature when the card is off */ |
| 83 | if (ar->state != ATH10K_STATE_ON) { |
| 84 | ret = -ENETDOWN; |
| 85 | goto out; |
| 86 | } |
| 87 | |
| 88 | reinit_completion(&ar->thermal.wmi_sync); |
| 89 | ret = ath10k_wmi_pdev_get_temperature(ar); |
| 90 | if (ret) { |
| 91 | ath10k_warn(ar, "failed to read temperature %d\n", ret); |
| 92 | goto out; |
| 93 | } |
| 94 | |
| 95 | if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) { |
| 96 | ret = -ESHUTDOWN; |
| 97 | goto out; |
| 98 | } |
| 99 | |
Nicholas Mc Guire | f439539 | 2015-03-30 15:39:20 +0300 | [diff] [blame] | 100 | time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync, |
| 101 | ATH10K_THERMAL_SYNC_TIMEOUT_HZ); |
| 102 | if (!time_left) { |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 103 | ath10k_warn(ar, "failed to synchronize thermal read\n"); |
| 104 | ret = -ETIMEDOUT; |
| 105 | goto out; |
| 106 | } |
| 107 | |
| 108 | spin_lock_bh(&ar->data_lock); |
| 109 | temperature = ar->thermal.temperature; |
| 110 | spin_unlock_bh(&ar->data_lock); |
| 111 | |
Rajkumar Manoharan | 6d48161 | 2015-01-13 12:44:24 +0530 | [diff] [blame] | 112 | /* display in millidegree celcius */ |
| 113 | ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000); |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 114 | out: |
| 115 | mutex_unlock(&ar->conf_mutex); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature) |
| 120 | { |
| 121 | spin_lock_bh(&ar->data_lock); |
| 122 | ar->thermal.temperature = temperature; |
| 123 | spin_unlock_bh(&ar->data_lock); |
| 124 | complete(&ar->thermal.wmi_sync); |
| 125 | } |
| 126 | |
Marcin Rokicki | 53c8d48 | 2017-02-20 14:40:27 +0100 | [diff] [blame] | 127 | static SENSOR_DEVICE_ATTR(temp1_input, 0444, ath10k_thermal_show_temp, |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 128 | NULL, 0); |
| 129 | |
| 130 | static struct attribute *ath10k_hwmon_attrs[] = { |
| 131 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 132 | NULL, |
| 133 | }; |
| 134 | ATTRIBUTE_GROUPS(ath10k_hwmon); |
| 135 | |
Rajkumar Manoharan | 8515b5c | 2015-03-15 20:36:22 +0530 | [diff] [blame] | 136 | void ath10k_thermal_set_throttling(struct ath10k *ar) |
| 137 | { |
| 138 | u32 period, duration, enabled; |
| 139 | int ret; |
| 140 | |
| 141 | lockdep_assert_held(&ar->conf_mutex); |
| 142 | |
Rakesh Pillai | 5388457 | 2018-12-14 12:17:46 +0200 | [diff] [blame] | 143 | if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map)) |
| 144 | return; |
| 145 | |
Michal Kazior | d600a6d | 2015-03-24 12:40:17 +0000 | [diff] [blame] | 146 | if (!ar->wmi.ops->gen_pdev_set_quiet_mode) |
| 147 | return; |
| 148 | |
Rajkumar Manoharan | 9936fa5 | 2015-03-15 20:36:25 +0530 | [diff] [blame] | 149 | if (ar->state != ATH10K_STATE_ON) |
| 150 | return; |
| 151 | |
Rajkumar Manoharan | 8515b5c | 2015-03-15 20:36:22 +0530 | [diff] [blame] | 152 | period = ar->thermal.quiet_period; |
| 153 | duration = (period * ar->thermal.throttle_state) / 100; |
| 154 | enabled = duration ? 1 : 0; |
| 155 | |
| 156 | ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration, |
| 157 | ATH10K_QUIET_START_OFFSET, |
| 158 | enabled); |
| 159 | if (ret) { |
| 160 | ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n", |
| 161 | period, duration, enabled, ret); |
| 162 | } |
| 163 | } |
| 164 | |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 165 | int ath10k_thermal_register(struct ath10k *ar) |
| 166 | { |
| 167 | struct thermal_cooling_device *cdev; |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 168 | struct device *hwmon_dev; |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 169 | int ret; |
| 170 | |
Rakesh Pillai | 5388457 | 2018-12-14 12:17:46 +0200 | [diff] [blame] | 171 | if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map)) |
| 172 | return 0; |
| 173 | |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 174 | cdev = thermal_cooling_device_register("ath10k_thermal", ar, |
| 175 | &ath10k_thermal_ops); |
| 176 | |
| 177 | if (IS_ERR(cdev)) { |
| 178 | ath10k_err(ar, "failed to setup thermal device result: %ld\n", |
| 179 | PTR_ERR(cdev)); |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj, |
| 184 | "cooling_device"); |
| 185 | if (ret) { |
Rajkumar Manoharan | 7e47e8e | 2015-03-12 19:32:00 +0200 | [diff] [blame] | 186 | ath10k_err(ar, "failed to create cooling device symlink\n"); |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 187 | goto err_cooling_destroy; |
| 188 | } |
| 189 | |
| 190 | ar->thermal.cdev = cdev; |
Rajkumar Manoharan | 63fb32d | 2015-03-15 20:36:20 +0530 | [diff] [blame] | 191 | ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT; |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 192 | |
| 193 | /* Do not register hwmon device when temperature reading is not |
| 194 | * supported by firmware |
| 195 | */ |
Tamizh chelvam | 6dd4634 | 2015-10-31 11:20:32 +0200 | [diff] [blame] | 196 | if (!(ar->wmi.ops->gen_pdev_get_temperature)) |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 197 | return 0; |
| 198 | |
Kalle Valo | 96bba98 | 2015-01-07 17:04:10 +0200 | [diff] [blame] | 199 | /* Avoid linking error on devm_hwmon_device_register_with_groups, I |
Marcin Rokicki | d6dfe25c | 2017-02-20 14:39:57 +0100 | [diff] [blame] | 200 | * guess linux/hwmon.h is missing proper stubs. |
| 201 | */ |
Masahiro Yamada | 749bc03 | 2016-08-24 01:27:26 +0900 | [diff] [blame] | 202 | if (!IS_REACHABLE(CONFIG_HWMON)) |
Kalle Valo | 96bba98 | 2015-01-07 17:04:10 +0200 | [diff] [blame] | 203 | return 0; |
| 204 | |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 205 | hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev, |
| 206 | "ath10k_hwmon", ar, |
| 207 | ath10k_hwmon_groups); |
| 208 | if (IS_ERR(hwmon_dev)) { |
| 209 | ath10k_err(ar, "failed to register hwmon device: %ld\n", |
| 210 | PTR_ERR(hwmon_dev)); |
| 211 | ret = -EINVAL; |
| 212 | goto err_remove_link; |
| 213 | } |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 214 | return 0; |
| 215 | |
Rajkumar Manoharan | ac2953f | 2014-12-17 12:22:26 +0200 | [diff] [blame] | 216 | err_remove_link: |
Rajkumar Manoharan | 7e47e8e | 2015-03-12 19:32:00 +0200 | [diff] [blame] | 217 | sysfs_remove_link(&ar->dev->kobj, "cooling_device"); |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 218 | err_cooling_destroy: |
| 219 | thermal_cooling_device_unregister(cdev); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | void ath10k_thermal_unregister(struct ath10k *ar) |
| 224 | { |
Rakesh Pillai | 5388457 | 2018-12-14 12:17:46 +0200 | [diff] [blame] | 225 | if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map)) |
| 226 | return; |
| 227 | |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 228 | sysfs_remove_link(&ar->dev->kobj, "cooling_device"); |
Mohammed Shafi Shajakhan | 83cfce8 | 2015-10-08 16:03:03 +0530 | [diff] [blame] | 229 | thermal_cooling_device_unregister(ar->thermal.cdev); |
Rajkumar Manoharan | fe6f36d | 2014-12-17 12:22:07 +0200 | [diff] [blame] | 230 | } |