Loading...
1// SPDX-License-Identifier: BSD-3-Clause-Clear
2/*
3 * Copyright (c) 2020 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
5 */
6
7#include <linux/device.h>
8#include <linux/sysfs.h>
9#include <linux/thermal.h>
10#include <linux/hwmon.h>
11#include <linux/hwmon-sysfs.h>
12#include "core.h"
13#include "debug.h"
14
15static int
16ath11k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
17 unsigned long *state)
18{
19 *state = ATH11K_THERMAL_THROTTLE_MAX;
20
21 return 0;
22}
23
24static int
25ath11k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
26 unsigned long *state)
27{
28 struct ath11k *ar = cdev->devdata;
29
30 mutex_lock(&ar->conf_mutex);
31 *state = ar->thermal.throttle_state;
32 mutex_unlock(&ar->conf_mutex);
33
34 return 0;
35}
36
37static int
38ath11k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
39 unsigned long throttle_state)
40{
41 struct ath11k *ar = cdev->devdata;
42 int ret;
43
44 if (throttle_state > ATH11K_THERMAL_THROTTLE_MAX) {
45 ath11k_warn(ar->ab, "throttle state %ld is exceeding the limit %d\n",
46 throttle_state, ATH11K_THERMAL_THROTTLE_MAX);
47 return -EINVAL;
48 }
49 mutex_lock(&ar->conf_mutex);
50 ret = ath11k_thermal_set_throttling(ar, throttle_state);
51 if (ret == 0)
52 ar->thermal.throttle_state = throttle_state;
53 mutex_unlock(&ar->conf_mutex);
54 return ret;
55}
56
57static const struct thermal_cooling_device_ops ath11k_thermal_ops = {
58 .get_max_state = ath11k_thermal_get_max_throttle_state,
59 .get_cur_state = ath11k_thermal_get_cur_throttle_state,
60 .set_cur_state = ath11k_thermal_set_cur_throttle_state,
61};
62
63static ssize_t ath11k_thermal_show_temp(struct device *dev,
64 struct device_attribute *attr,
65 char *buf)
66{
67 struct ath11k *ar = dev_get_drvdata(dev);
68 int ret, temperature;
69 unsigned long time_left;
70
71 mutex_lock(&ar->conf_mutex);
72
73 /* Can't get temperature when the card is off */
74 if (ar->state != ATH11K_STATE_ON) {
75 ret = -ENETDOWN;
76 goto out;
77 }
78
79 reinit_completion(&ar->thermal.wmi_sync);
80 ret = ath11k_wmi_send_pdev_temperature_cmd(ar);
81 if (ret) {
82 ath11k_warn(ar->ab, "failed to read temperature %d\n", ret);
83 goto out;
84 }
85
86 if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags)) {
87 ret = -ESHUTDOWN;
88 goto out;
89 }
90
91 time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync,
92 ATH11K_THERMAL_SYNC_TIMEOUT_HZ);
93 if (!time_left) {
94 ath11k_warn(ar->ab, "failed to synchronize thermal read\n");
95 ret = -ETIMEDOUT;
96 goto out;
97 }
98
99 spin_lock_bh(&ar->data_lock);
100 temperature = ar->thermal.temperature;
101 spin_unlock_bh(&ar->data_lock);
102
103 /* display in millidegree Celsius */
104 ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
105out:
106 mutex_unlock(&ar->conf_mutex);
107 return ret;
108}
109
110void ath11k_thermal_event_temperature(struct ath11k *ar, int temperature)
111{
112 spin_lock_bh(&ar->data_lock);
113 ar->thermal.temperature = temperature;
114 spin_unlock_bh(&ar->data_lock);
115 complete(&ar->thermal.wmi_sync);
116}
117
118static SENSOR_DEVICE_ATTR(temp1_input, 0444, ath11k_thermal_show_temp,
119 NULL, 0);
120
121static struct attribute *ath11k_hwmon_attrs[] = {
122 &sensor_dev_attr_temp1_input.dev_attr.attr,
123 NULL,
124};
125ATTRIBUTE_GROUPS(ath11k_hwmon);
126
127int ath11k_thermal_set_throttling(struct ath11k *ar, u32 throttle_state)
128{
129 struct ath11k_base *ab = ar->ab;
130 struct thermal_mitigation_params param;
131 int ret = 0;
132
133 lockdep_assert_held(&ar->conf_mutex);
134
135 if (ar->state != ATH11K_STATE_ON)
136 return 0;
137
138 memset(¶m, 0, sizeof(param));
139 param.pdev_id = ar->pdev->pdev_id;
140 param.enable = throttle_state ? 1 : 0;
141 param.dc = ATH11K_THERMAL_DEFAULT_DUTY_CYCLE;
142 param.dc_per_event = 0xFFFFFFFF;
143
144 param.levelconf[0].tmplwm = ATH11K_THERMAL_TEMP_LOW_MARK;
145 param.levelconf[0].tmphwm = ATH11K_THERMAL_TEMP_HIGH_MARK;
146 param.levelconf[0].dcoffpercent = throttle_state;
147 param.levelconf[0].priority = 0; /* disable all data tx queues */
148
149 ret = ath11k_wmi_send_thermal_mitigation_param_cmd(ar, ¶m);
150 if (ret) {
151 ath11k_warn(ab, "failed to send thermal mitigation duty cycle %u ret %d\n",
152 throttle_state, ret);
153 }
154
155 return ret;
156}
157
158int ath11k_thermal_register(struct ath11k_base *ab)
159{
160 struct thermal_cooling_device *cdev;
161 struct device *hwmon_dev;
162 struct ath11k *ar;
163 struct ath11k_pdev *pdev;
164 int i, ret;
165
166 for (i = 0; i < ab->num_radios; i++) {
167 pdev = &ab->pdevs[i];
168 ar = pdev->ar;
169 if (!ar)
170 continue;
171
172 cdev = thermal_cooling_device_register("ath11k_thermal", ar,
173 &ath11k_thermal_ops);
174
175 if (IS_ERR(cdev)) {
176 ath11k_err(ab, "failed to setup thermal device result: %ld\n",
177 PTR_ERR(cdev));
178 ret = -EINVAL;
179 goto err_thermal_destroy;
180 }
181
182 ar->thermal.cdev = cdev;
183
184 ret = sysfs_create_link(&ar->hw->wiphy->dev.kobj, &cdev->device.kobj,
185 "cooling_device");
186 if (ret) {
187 ath11k_err(ab, "failed to create cooling device symlink\n");
188 goto err_thermal_destroy;
189 }
190
191 if (!IS_REACHABLE(CONFIG_HWMON))
192 return 0;
193
194 hwmon_dev = devm_hwmon_device_register_with_groups(&ar->hw->wiphy->dev,
195 "ath11k_hwmon", ar,
196 ath11k_hwmon_groups);
197 if (IS_ERR(hwmon_dev)) {
198 ath11k_err(ar->ab, "failed to register hwmon device: %ld\n",
199 PTR_ERR(hwmon_dev));
200 ret = -EINVAL;
201 goto err_thermal_destroy;
202 }
203 }
204
205 return 0;
206
207err_thermal_destroy:
208 ath11k_thermal_unregister(ab);
209 return ret;
210}
211
212void ath11k_thermal_unregister(struct ath11k_base *ab)
213{
214 struct ath11k *ar;
215 struct ath11k_pdev *pdev;
216 int i;
217
218 for (i = 0; i < ab->num_radios; i++) {
219 pdev = &ab->pdevs[i];
220 ar = pdev->ar;
221 if (!ar)
222 continue;
223
224 sysfs_remove_link(&ar->hw->wiphy->dev.kobj, "cooling_device");
225 thermal_cooling_device_unregister(ar->thermal.cdev);
226 }
227}
1// SPDX-License-Identifier: BSD-3-Clause-Clear
2/*
3 * Copyright (c) 2020 The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/device.h>
7#include <linux/sysfs.h>
8#include <linux/thermal.h>
9#include <linux/hwmon.h>
10#include <linux/hwmon-sysfs.h>
11#include "core.h"
12#include "debug.h"
13
14static int
15ath11k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
16 unsigned long *state)
17{
18 *state = ATH11K_THERMAL_THROTTLE_MAX;
19
20 return 0;
21}
22
23static int
24ath11k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
25 unsigned long *state)
26{
27 struct ath11k *ar = cdev->devdata;
28
29 mutex_lock(&ar->conf_mutex);
30 *state = ar->thermal.throttle_state;
31 mutex_unlock(&ar->conf_mutex);
32
33 return 0;
34}
35
36static int
37ath11k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
38 unsigned long throttle_state)
39{
40 struct ath11k *ar = cdev->devdata;
41 int ret;
42
43 if (throttle_state > ATH11K_THERMAL_THROTTLE_MAX) {
44 ath11k_warn(ar->ab, "throttle state %ld is exceeding the limit %d\n",
45 throttle_state, ATH11K_THERMAL_THROTTLE_MAX);
46 return -EINVAL;
47 }
48 mutex_lock(&ar->conf_mutex);
49 ret = ath11k_thermal_set_throttling(ar, throttle_state);
50 if (ret == 0)
51 ar->thermal.throttle_state = throttle_state;
52 mutex_unlock(&ar->conf_mutex);
53 return ret;
54}
55
56static const struct thermal_cooling_device_ops ath11k_thermal_ops = {
57 .get_max_state = ath11k_thermal_get_max_throttle_state,
58 .get_cur_state = ath11k_thermal_get_cur_throttle_state,
59 .set_cur_state = ath11k_thermal_set_cur_throttle_state,
60};
61
62static ssize_t ath11k_thermal_show_temp(struct device *dev,
63 struct device_attribute *attr,
64 char *buf)
65{
66 struct ath11k *ar = dev_get_drvdata(dev);
67 int ret, temperature;
68 unsigned long time_left;
69
70 mutex_lock(&ar->conf_mutex);
71
72 /* Can't get temperature when the card is off */
73 if (ar->state != ATH11K_STATE_ON) {
74 ret = -ENETDOWN;
75 goto out;
76 }
77
78 reinit_completion(&ar->thermal.wmi_sync);
79 ret = ath11k_wmi_send_pdev_temperature_cmd(ar);
80 if (ret) {
81 ath11k_warn(ar->ab, "failed to read temperature %d\n", ret);
82 goto out;
83 }
84
85 if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags)) {
86 ret = -ESHUTDOWN;
87 goto out;
88 }
89
90 time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync,
91 ATH11K_THERMAL_SYNC_TIMEOUT_HZ);
92 if (!time_left) {
93 ath11k_warn(ar->ab, "failed to synchronize thermal read\n");
94 ret = -ETIMEDOUT;
95 goto out;
96 }
97
98 spin_lock_bh(&ar->data_lock);
99 temperature = ar->thermal.temperature;
100 spin_unlock_bh(&ar->data_lock);
101
102 /* display in millidegree Celsius */
103 ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
104out:
105 mutex_unlock(&ar->conf_mutex);
106 return ret;
107}
108
109void ath11k_thermal_event_temperature(struct ath11k *ar, int temperature)
110{
111 spin_lock_bh(&ar->data_lock);
112 ar->thermal.temperature = temperature;
113 spin_unlock_bh(&ar->data_lock);
114 complete(&ar->thermal.wmi_sync);
115}
116
117static SENSOR_DEVICE_ATTR(temp1_input, 0444, ath11k_thermal_show_temp,
118 NULL, 0);
119
120static struct attribute *ath11k_hwmon_attrs[] = {
121 &sensor_dev_attr_temp1_input.dev_attr.attr,
122 NULL,
123};
124ATTRIBUTE_GROUPS(ath11k_hwmon);
125
126int ath11k_thermal_set_throttling(struct ath11k *ar, u32 throttle_state)
127{
128 struct ath11k_base *sc = ar->ab;
129 struct thermal_mitigation_params param;
130 int ret = 0;
131
132 lockdep_assert_held(&ar->conf_mutex);
133
134 if (ar->state != ATH11K_STATE_ON)
135 return 0;
136
137 memset(¶m, 0, sizeof(param));
138 param.pdev_id = ar->pdev->pdev_id;
139 param.enable = throttle_state ? 1 : 0;
140 param.dc = ATH11K_THERMAL_DEFAULT_DUTY_CYCLE;
141 param.dc_per_event = 0xFFFFFFFF;
142
143 param.levelconf[0].tmplwm = ATH11K_THERMAL_TEMP_LOW_MARK;
144 param.levelconf[0].tmphwm = ATH11K_THERMAL_TEMP_HIGH_MARK;
145 param.levelconf[0].dcoffpercent = throttle_state;
146 param.levelconf[0].priority = 0; /* disable all data tx queues */
147
148 ret = ath11k_wmi_send_thermal_mitigation_param_cmd(ar, ¶m);
149 if (ret) {
150 ath11k_warn(sc, "failed to send thermal mitigation duty cycle %u ret %d\n",
151 throttle_state, ret);
152 }
153
154 return ret;
155}
156
157int ath11k_thermal_register(struct ath11k_base *sc)
158{
159 struct thermal_cooling_device *cdev;
160 struct device *hwmon_dev;
161 struct ath11k *ar;
162 struct ath11k_pdev *pdev;
163 int i, ret;
164
165 for (i = 0; i < sc->num_radios; i++) {
166 pdev = &sc->pdevs[i];
167 ar = pdev->ar;
168 if (!ar)
169 continue;
170
171 cdev = thermal_cooling_device_register("ath11k_thermal", ar,
172 &ath11k_thermal_ops);
173
174 if (IS_ERR(cdev)) {
175 ath11k_err(sc, "failed to setup thermal device result: %ld\n",
176 PTR_ERR(cdev));
177 ret = -EINVAL;
178 goto err_thermal_destroy;
179 }
180
181 ar->thermal.cdev = cdev;
182
183 ret = sysfs_create_link(&ar->hw->wiphy->dev.kobj, &cdev->device.kobj,
184 "cooling_device");
185 if (ret) {
186 ath11k_err(sc, "failed to create cooling device symlink\n");
187 goto err_thermal_destroy;
188 }
189
190 if (!IS_REACHABLE(CONFIG_HWMON))
191 return 0;
192
193 hwmon_dev = devm_hwmon_device_register_with_groups(&ar->hw->wiphy->dev,
194 "ath11k_hwmon", ar,
195 ath11k_hwmon_groups);
196 if (IS_ERR(hwmon_dev)) {
197 ath11k_err(ar->ab, "failed to register hwmon device: %ld\n",
198 PTR_ERR(hwmon_dev));
199 ret = -EINVAL;
200 goto err_thermal_destroy;
201 }
202 }
203
204 return 0;
205
206err_thermal_destroy:
207 ath11k_thermal_unregister(sc);
208 return ret;
209}
210
211void ath11k_thermal_unregister(struct ath11k_base *sc)
212{
213 struct ath11k *ar;
214 struct ath11k_pdev *pdev;
215 int i;
216
217 for (i = 0; i < sc->num_radios; i++) {
218 pdev = &sc->pdevs[i];
219 ar = pdev->ar;
220 if (!ar)
221 continue;
222
223 sysfs_remove_link(&ar->hw->wiphy->dev.kobj, "cooling_device");
224 thermal_cooling_device_unregister(ar->thermal.cdev);
225 }
226}