blob: e98ce8ce27e2b145f25c22aed835102e14ef9f6f [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>
20#include "core.h"
21#include "debug.h"
22#include "wmi-ops.h"
23
24static int ath10k_thermal_get_active_vifs(struct ath10k *ar,
25 enum wmi_vdev_type type)
26{
27 struct ath10k_vif *arvif;
28 int count = 0;
29
30 lockdep_assert_held(&ar->conf_mutex);
31
32 list_for_each_entry(arvif, &ar->arvifs, list) {
33 if (!arvif->is_started)
34 continue;
35
36 if (!arvif->is_up)
37 continue;
38
39 if (arvif->vdev_type != type)
40 continue;
41
42 count++;
43 }
44 return count;
45}
46
47static int ath10k_thermal_get_max_dutycycle(struct thermal_cooling_device *cdev,
48 unsigned long *state)
49{
50 *state = ATH10K_QUIET_DUTY_CYCLE_MAX;
51
52 return 0;
53}
54
55static int ath10k_thermal_get_cur_dutycycle(struct thermal_cooling_device *cdev,
56 unsigned long *state)
57{
58 struct ath10k *ar = cdev->devdata;
59
60 mutex_lock(&ar->conf_mutex);
61 *state = ar->thermal.duty_cycle;
62 mutex_unlock(&ar->conf_mutex);
63
64 return 0;
65}
66
67static int ath10k_thermal_set_cur_dutycycle(struct thermal_cooling_device *cdev,
68 unsigned long duty_cycle)
69{
70 struct ath10k *ar = cdev->devdata;
71 u32 period, duration, enabled;
72 int num_bss, ret = 0;
73
74 mutex_lock(&ar->conf_mutex);
75 if (ar->state != ATH10K_STATE_ON) {
76 ret = -ENETDOWN;
77 goto out;
78 }
79
80 if (duty_cycle > ATH10K_QUIET_DUTY_CYCLE_MAX) {
81 ath10k_warn(ar, "duty cycle %ld is exceeding the limit %d\n",
82 duty_cycle, ATH10K_QUIET_DUTY_CYCLE_MAX);
83 ret = -EINVAL;
84 goto out;
85 }
86 /* TODO: Right now, thermal mitigation is handled only for single/multi
87 * vif AP mode. Since quiet param is not validated in STA mode, it needs
88 * to be investigated further to handle multi STA and multi-vif (AP+STA)
89 * mode properly.
90 */
91 num_bss = ath10k_thermal_get_active_vifs(ar, WMI_VDEV_TYPE_AP);
92 if (!num_bss) {
93 ath10k_warn(ar, "no active AP interfaces\n");
94 ret = -ENETDOWN;
95 goto out;
96 }
97 period = max(ATH10K_QUIET_PERIOD_MIN,
98 (ATH10K_QUIET_PERIOD_DEFAULT / num_bss));
99 duration = period * (duty_cycle / 100);
100 enabled = duration ? 1 : 0;
101
102 ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
103 ATH10K_QUIET_START_OFFSET,
104 enabled);
105 if (ret) {
106 ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
107 period, duration, enabled, ret);
108 goto out;
109 }
110 ar->thermal.duty_cycle = duty_cycle;
111out:
112 mutex_unlock(&ar->conf_mutex);
113 return ret;
114}
115
116static struct thermal_cooling_device_ops ath10k_thermal_ops = {
117 .get_max_state = ath10k_thermal_get_max_dutycycle,
118 .get_cur_state = ath10k_thermal_get_cur_dutycycle,
119 .set_cur_state = ath10k_thermal_set_cur_dutycycle,
120};
121
122int ath10k_thermal_register(struct ath10k *ar)
123{
124 struct thermal_cooling_device *cdev;
125 int ret;
126
127 cdev = thermal_cooling_device_register("ath10k_thermal", ar,
128 &ath10k_thermal_ops);
129
130 if (IS_ERR(cdev)) {
131 ath10k_err(ar, "failed to setup thermal device result: %ld\n",
132 PTR_ERR(cdev));
133 return -EINVAL;
134 }
135
136 ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
137 "cooling_device");
138 if (ret) {
139 ath10k_err(ar, "failed to create thermal symlink\n");
140 goto err_cooling_destroy;
141 }
142
143 ar->thermal.cdev = cdev;
144 return 0;
145
146err_cooling_destroy:
147 thermal_cooling_device_unregister(cdev);
148 return ret;
149}
150
151void ath10k_thermal_unregister(struct ath10k *ar)
152{
153 thermal_cooling_device_unregister(ar->thermal.cdev);
154 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
155}