blob: 8640f413f384752488b1b217d08c87f1fc9164f0 [file] [log] [blame]
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +02001/*
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 Manoharanac2953f2014-12-17 12:22:26 +020020#include <linux/hwmon.h>
21#include <linux/hwmon-sysfs.h>
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020022#include "core.h"
23#include "debug.h"
24#include "wmi-ops.h"
25
26static 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
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053049static int
50ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
51 unsigned long *state)
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020052{
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053053 *state = ATH10K_THERMAL_THROTTLE_MAX;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020054
55 return 0;
56}
57
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053058static int
59ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
60 unsigned long *state)
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020061{
62 struct ath10k *ar = cdev->devdata;
63
64 mutex_lock(&ar->conf_mutex);
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053065 *state = ar->thermal.throttle_state;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020066 mutex_unlock(&ar->conf_mutex);
67
68 return 0;
69}
70
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053071static int
72ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
73 unsigned long throttle_state)
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020074{
75 struct ath10k *ar = cdev->devdata;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020076 int num_bss, ret = 0;
77
78 mutex_lock(&ar->conf_mutex);
79 if (ar->state != ATH10K_STATE_ON) {
80 ret = -ENETDOWN;
81 goto out;
82 }
83
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053084 if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) {
85 ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n",
86 throttle_state, ATH10K_THERMAL_THROTTLE_MAX);
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020087 ret = -EINVAL;
88 goto out;
89 }
90 /* TODO: Right now, thermal mitigation is handled only for single/multi
91 * vif AP mode. Since quiet param is not validated in STA mode, it needs
92 * to be investigated further to handle multi STA and multi-vif (AP+STA)
93 * mode properly.
94 */
95 num_bss = ath10k_thermal_get_active_vifs(ar, WMI_VDEV_TYPE_AP);
96 if (!num_bss) {
97 ath10k_warn(ar, "no active AP interfaces\n");
98 ret = -ENETDOWN;
99 goto out;
100 }
Rajkumar Manoharan972f0512015-03-15 20:36:21 +0530101 ar->thermal.throttle_state = throttle_state;
Rajkumar Manoharan8515b5c2015-03-15 20:36:22 +0530102 ath10k_thermal_set_throttling(ar);
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200103out:
104 mutex_unlock(&ar->conf_mutex);
105 return ret;
106}
107
108static struct thermal_cooling_device_ops ath10k_thermal_ops = {
Rajkumar Manoharan972f0512015-03-15 20:36:21 +0530109 .get_max_state = ath10k_thermal_get_max_throttle_state,
110 .get_cur_state = ath10k_thermal_get_cur_throttle_state,
111 .set_cur_state = ath10k_thermal_set_cur_throttle_state,
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200112};
113
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200114static ssize_t ath10k_thermal_show_temp(struct device *dev,
115 struct device_attribute *attr,
116 char *buf)
117{
118 struct ath10k *ar = dev_get_drvdata(dev);
119 int ret, temperature;
120
121 mutex_lock(&ar->conf_mutex);
122
123 /* Can't get temperature when the card is off */
124 if (ar->state != ATH10K_STATE_ON) {
125 ret = -ENETDOWN;
126 goto out;
127 }
128
129 reinit_completion(&ar->thermal.wmi_sync);
130 ret = ath10k_wmi_pdev_get_temperature(ar);
131 if (ret) {
132 ath10k_warn(ar, "failed to read temperature %d\n", ret);
133 goto out;
134 }
135
136 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
137 ret = -ESHUTDOWN;
138 goto out;
139 }
140
141 ret = wait_for_completion_timeout(&ar->thermal.wmi_sync,
142 ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
143 if (ret == 0) {
144 ath10k_warn(ar, "failed to synchronize thermal read\n");
145 ret = -ETIMEDOUT;
146 goto out;
147 }
148
149 spin_lock_bh(&ar->data_lock);
150 temperature = ar->thermal.temperature;
151 spin_unlock_bh(&ar->data_lock);
152
Rajkumar Manoharan6d481612015-01-13 12:44:24 +0530153 /* display in millidegree celcius */
154 ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200155out:
156 mutex_unlock(&ar->conf_mutex);
157 return ret;
158}
159
160void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
161{
162 spin_lock_bh(&ar->data_lock);
163 ar->thermal.temperature = temperature;
164 spin_unlock_bh(&ar->data_lock);
165 complete(&ar->thermal.wmi_sync);
166}
167
168static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ath10k_thermal_show_temp,
169 NULL, 0);
170
171static struct attribute *ath10k_hwmon_attrs[] = {
172 &sensor_dev_attr_temp1_input.dev_attr.attr,
173 NULL,
174};
175ATTRIBUTE_GROUPS(ath10k_hwmon);
176
Rajkumar Manoharan8515b5c2015-03-15 20:36:22 +0530177void ath10k_thermal_set_throttling(struct ath10k *ar)
178{
179 u32 period, duration, enabled;
180 int ret;
181
182 lockdep_assert_held(&ar->conf_mutex);
183
184 period = ar->thermal.quiet_period;
185 duration = (period * ar->thermal.throttle_state) / 100;
186 enabled = duration ? 1 : 0;
187
188 ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
189 ATH10K_QUIET_START_OFFSET,
190 enabled);
191 if (ret) {
192 ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
193 period, duration, enabled, ret);
194 }
195}
196
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200197int ath10k_thermal_register(struct ath10k *ar)
198{
199 struct thermal_cooling_device *cdev;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200200 struct device *hwmon_dev;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200201 int ret;
202
203 cdev = thermal_cooling_device_register("ath10k_thermal", ar,
204 &ath10k_thermal_ops);
205
206 if (IS_ERR(cdev)) {
207 ath10k_err(ar, "failed to setup thermal device result: %ld\n",
208 PTR_ERR(cdev));
209 return -EINVAL;
210 }
211
212 ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
213 "cooling_device");
214 if (ret) {
Rajkumar Manoharan7e47e8e2015-03-12 19:32:00 +0200215 ath10k_err(ar, "failed to create cooling device symlink\n");
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200216 goto err_cooling_destroy;
217 }
218
219 ar->thermal.cdev = cdev;
Rajkumar Manoharan63fb32d2015-03-15 20:36:20 +0530220 ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200221
222 /* Do not register hwmon device when temperature reading is not
223 * supported by firmware
224 */
225 if (ar->wmi.op_version != ATH10K_FW_WMI_OP_VERSION_10_2_4)
226 return 0;
227
Kalle Valo96bba982015-01-07 17:04:10 +0200228 /* Avoid linking error on devm_hwmon_device_register_with_groups, I
229 * guess linux/hwmon.h is missing proper stubs. */
Rajkumar Manoharana4031af2015-01-13 14:21:45 +0530230 if (!config_enabled(CONFIG_HWMON))
Kalle Valo96bba982015-01-07 17:04:10 +0200231 return 0;
232
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200233 hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
234 "ath10k_hwmon", ar,
235 ath10k_hwmon_groups);
236 if (IS_ERR(hwmon_dev)) {
237 ath10k_err(ar, "failed to register hwmon device: %ld\n",
238 PTR_ERR(hwmon_dev));
239 ret = -EINVAL;
240 goto err_remove_link;
241 }
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200242 return 0;
243
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200244err_remove_link:
Rajkumar Manoharan7e47e8e2015-03-12 19:32:00 +0200245 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200246err_cooling_destroy:
247 thermal_cooling_device_unregister(cdev);
248 return ret;
249}
250
251void ath10k_thermal_unregister(struct ath10k *ar)
252{
253 thermal_cooling_device_unregister(ar->thermal.cdev);
254 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
255}