blob: b14ae8d135f637bf442db547ad979aff653fa4e8 [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
49static 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
57static 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
69static 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));
101 duration = period * (duty_cycle / 100);
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;
113out:
114 mutex_unlock(&ar->conf_mutex);
115 return ret;
116}
117
118static 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 Manoharanac2953f2014-12-17 12:22:26 +0200124static 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
163 ret = snprintf(buf, PAGE_SIZE, "%d", temperature);
164out:
165 mutex_unlock(&ar->conf_mutex);
166 return ret;
167}
168
169void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
170{
171 spin_lock_bh(&ar->data_lock);
172 ar->thermal.temperature = temperature;
173 spin_unlock_bh(&ar->data_lock);
174 complete(&ar->thermal.wmi_sync);
175}
176
177static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ath10k_thermal_show_temp,
178 NULL, 0);
179
180static struct attribute *ath10k_hwmon_attrs[] = {
181 &sensor_dev_attr_temp1_input.dev_attr.attr,
182 NULL,
183};
184ATTRIBUTE_GROUPS(ath10k_hwmon);
185
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200186int ath10k_thermal_register(struct ath10k *ar)
187{
188 struct thermal_cooling_device *cdev;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200189 struct device *hwmon_dev;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200190 int ret;
191
192 cdev = thermal_cooling_device_register("ath10k_thermal", ar,
193 &ath10k_thermal_ops);
194
195 if (IS_ERR(cdev)) {
196 ath10k_err(ar, "failed to setup thermal device result: %ld\n",
197 PTR_ERR(cdev));
198 return -EINVAL;
199 }
200
201 ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
202 "cooling_device");
203 if (ret) {
204 ath10k_err(ar, "failed to create thermal symlink\n");
205 goto err_cooling_destroy;
206 }
207
208 ar->thermal.cdev = cdev;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200209
210 /* Do not register hwmon device when temperature reading is not
211 * supported by firmware
212 */
213 if (ar->wmi.op_version != ATH10K_FW_WMI_OP_VERSION_10_2_4)
214 return 0;
215
Kalle Valo96bba982015-01-07 17:04:10 +0200216 /* Avoid linking error on devm_hwmon_device_register_with_groups, I
217 * guess linux/hwmon.h is missing proper stubs. */
218 if (!config_enabled(HWMON))
219 return 0;
220
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200221 hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
222 "ath10k_hwmon", ar,
223 ath10k_hwmon_groups);
224 if (IS_ERR(hwmon_dev)) {
225 ath10k_err(ar, "failed to register hwmon device: %ld\n",
226 PTR_ERR(hwmon_dev));
227 ret = -EINVAL;
228 goto err_remove_link;
229 }
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200230 return 0;
231
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200232err_remove_link:
233 sysfs_remove_link(&ar->dev->kobj, "thermal_sensor");
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200234err_cooling_destroy:
235 thermal_cooling_device_unregister(cdev);
236 return ret;
237}
238
239void ath10k_thermal_unregister(struct ath10k *ar)
240{
241 thermal_cooling_device_unregister(ar->thermal.cdev);
242 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
243}