blob: d3fd2ab4d86acd5cd640d2bc9e9663b75d4e6363 [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 }
Rajkumar Manoharan63fb32d2015-03-15 20:36:20 +053099 period = ar->thermal.quiet_period;
Rajkumar Manoharan8bdadac2015-01-13 14:52:14 +0530100 duration = (period * duty_cycle) / 100;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200101 enabled = duration ? 1 : 0;
102
103 ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
104 ATH10K_QUIET_START_OFFSET,
105 enabled);
106 if (ret) {
107 ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
108 period, duration, enabled, ret);
109 goto out;
110 }
111 ar->thermal.duty_cycle = duty_cycle;
112out:
113 mutex_unlock(&ar->conf_mutex);
114 return ret;
115}
116
117static struct thermal_cooling_device_ops ath10k_thermal_ops = {
118 .get_max_state = ath10k_thermal_get_max_dutycycle,
119 .get_cur_state = ath10k_thermal_get_cur_dutycycle,
120 .set_cur_state = ath10k_thermal_set_cur_dutycycle,
121};
122
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200123static ssize_t ath10k_thermal_show_temp(struct device *dev,
124 struct device_attribute *attr,
125 char *buf)
126{
127 struct ath10k *ar = dev_get_drvdata(dev);
128 int ret, temperature;
129
130 mutex_lock(&ar->conf_mutex);
131
132 /* Can't get temperature when the card is off */
133 if (ar->state != ATH10K_STATE_ON) {
134 ret = -ENETDOWN;
135 goto out;
136 }
137
138 reinit_completion(&ar->thermal.wmi_sync);
139 ret = ath10k_wmi_pdev_get_temperature(ar);
140 if (ret) {
141 ath10k_warn(ar, "failed to read temperature %d\n", ret);
142 goto out;
143 }
144
145 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
146 ret = -ESHUTDOWN;
147 goto out;
148 }
149
150 ret = wait_for_completion_timeout(&ar->thermal.wmi_sync,
151 ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
152 if (ret == 0) {
153 ath10k_warn(ar, "failed to synchronize thermal read\n");
154 ret = -ETIMEDOUT;
155 goto out;
156 }
157
158 spin_lock_bh(&ar->data_lock);
159 temperature = ar->thermal.temperature;
160 spin_unlock_bh(&ar->data_lock);
161
Rajkumar Manoharan6d481612015-01-13 12:44:24 +0530162 /* display in millidegree celcius */
163 ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200164out:
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) {
Rajkumar Manoharan7e47e8e2015-03-12 19:32:00 +0200204 ath10k_err(ar, "failed to create cooling device symlink\n");
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200205 goto err_cooling_destroy;
206 }
207
208 ar->thermal.cdev = cdev;
Rajkumar Manoharan63fb32d2015-03-15 20:36:20 +0530209 ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200210
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 Valo96bba982015-01-07 17:04:10 +0200217 /* Avoid linking error on devm_hwmon_device_register_with_groups, I
218 * guess linux/hwmon.h is missing proper stubs. */
Rajkumar Manoharana4031af2015-01-13 14:21:45 +0530219 if (!config_enabled(CONFIG_HWMON))
Kalle Valo96bba982015-01-07 17:04:10 +0200220 return 0;
221
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200222 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 Manoharanfe6f36d2014-12-17 12:22:07 +0200231 return 0;
232
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200233err_remove_link:
Rajkumar Manoharan7e47e8e2015-03-12 19:32:00 +0200234 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200235err_cooling_destroy:
236 thermal_cooling_device_unregister(cdev);
237 return ret;
238}
239
240void 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}