blob: 01bae867cae91d7a2d4dc947dc17cd2387ef3588 [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
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053026static int
27ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
28 unsigned long *state)
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020029{
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053030 *state = ATH10K_THERMAL_THROTTLE_MAX;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020031
32 return 0;
33}
34
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053035static int
36ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
37 unsigned long *state)
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020038{
39 struct ath10k *ar = cdev->devdata;
40
41 mutex_lock(&ar->conf_mutex);
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053042 *state = ar->thermal.throttle_state;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020043 mutex_unlock(&ar->conf_mutex);
44
45 return 0;
46}
47
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053048static int
49ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
50 unsigned long throttle_state)
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020051{
52 struct ath10k *ar = cdev->devdata;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020053
Rajkumar Manoharan28bf0c42015-03-15 20:36:24 +053054 if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) {
55 ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n",
56 throttle_state, ATH10K_THERMAL_THROTTLE_MAX);
57 return -EINVAL;
58 }
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020059 mutex_lock(&ar->conf_mutex);
Rajkumar Manoharan28bf0c42015-03-15 20:36:24 +053060 ar->thermal.throttle_state = throttle_state;
Rajkumar Manoharan8515b5c2015-03-15 20:36:22 +053061 ath10k_thermal_set_throttling(ar);
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020062 mutex_unlock(&ar->conf_mutex);
Rajkumar Manoharan9936fa52015-03-15 20:36:25 +053063 return 0;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020064}
65
66static struct thermal_cooling_device_ops ath10k_thermal_ops = {
Rajkumar Manoharan972f0512015-03-15 20:36:21 +053067 .get_max_state = ath10k_thermal_get_max_throttle_state,
68 .get_cur_state = ath10k_thermal_get_cur_throttle_state,
69 .set_cur_state = ath10k_thermal_set_cur_throttle_state,
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +020070};
71
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +020072static ssize_t ath10k_thermal_show_temp(struct device *dev,
73 struct device_attribute *attr,
74 char *buf)
75{
76 struct ath10k *ar = dev_get_drvdata(dev);
77 int ret, temperature;
Nicholas Mc Guiref4395392015-03-30 15:39:20 +030078 unsigned long time_left;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +020079
80 mutex_lock(&ar->conf_mutex);
81
82 /* Can't get temperature when the card is off */
83 if (ar->state != ATH10K_STATE_ON) {
84 ret = -ENETDOWN;
85 goto out;
86 }
87
88 reinit_completion(&ar->thermal.wmi_sync);
89 ret = ath10k_wmi_pdev_get_temperature(ar);
90 if (ret) {
91 ath10k_warn(ar, "failed to read temperature %d\n", ret);
92 goto out;
93 }
94
95 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
96 ret = -ESHUTDOWN;
97 goto out;
98 }
99
Nicholas Mc Guiref4395392015-03-30 15:39:20 +0300100 time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync,
101 ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
102 if (!time_left) {
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200103 ath10k_warn(ar, "failed to synchronize thermal read\n");
104 ret = -ETIMEDOUT;
105 goto out;
106 }
107
108 spin_lock_bh(&ar->data_lock);
109 temperature = ar->thermal.temperature;
110 spin_unlock_bh(&ar->data_lock);
111
Rajkumar Manoharan6d481612015-01-13 12:44:24 +0530112 /* display in millidegree celcius */
113 ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200114out:
115 mutex_unlock(&ar->conf_mutex);
116 return ret;
117}
118
119void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
120{
121 spin_lock_bh(&ar->data_lock);
122 ar->thermal.temperature = temperature;
123 spin_unlock_bh(&ar->data_lock);
124 complete(&ar->thermal.wmi_sync);
125}
126
127static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ath10k_thermal_show_temp,
128 NULL, 0);
129
130static struct attribute *ath10k_hwmon_attrs[] = {
131 &sensor_dev_attr_temp1_input.dev_attr.attr,
132 NULL,
133};
134ATTRIBUTE_GROUPS(ath10k_hwmon);
135
Rajkumar Manoharan8515b5c2015-03-15 20:36:22 +0530136void ath10k_thermal_set_throttling(struct ath10k *ar)
137{
138 u32 period, duration, enabled;
139 int ret;
140
141 lockdep_assert_held(&ar->conf_mutex);
142
Rajkumar Manoharan9936fa52015-03-15 20:36:25 +0530143 if (ar->state != ATH10K_STATE_ON)
144 return;
145
Rajkumar Manoharan8515b5c2015-03-15 20:36:22 +0530146 period = ar->thermal.quiet_period;
147 duration = (period * ar->thermal.throttle_state) / 100;
148 enabled = duration ? 1 : 0;
149
150 ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
151 ATH10K_QUIET_START_OFFSET,
152 enabled);
153 if (ret) {
154 ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
155 period, duration, enabled, ret);
156 }
157}
158
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200159int ath10k_thermal_register(struct ath10k *ar)
160{
161 struct thermal_cooling_device *cdev;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200162 struct device *hwmon_dev;
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200163 int ret;
164
165 cdev = thermal_cooling_device_register("ath10k_thermal", ar,
166 &ath10k_thermal_ops);
167
168 if (IS_ERR(cdev)) {
169 ath10k_err(ar, "failed to setup thermal device result: %ld\n",
170 PTR_ERR(cdev));
171 return -EINVAL;
172 }
173
174 ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
175 "cooling_device");
176 if (ret) {
Rajkumar Manoharan7e47e8e2015-03-12 19:32:00 +0200177 ath10k_err(ar, "failed to create cooling device symlink\n");
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200178 goto err_cooling_destroy;
179 }
180
181 ar->thermal.cdev = cdev;
Rajkumar Manoharan63fb32d2015-03-15 20:36:20 +0530182 ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT;
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200183
184 /* Do not register hwmon device when temperature reading is not
185 * supported by firmware
186 */
187 if (ar->wmi.op_version != ATH10K_FW_WMI_OP_VERSION_10_2_4)
188 return 0;
189
Kalle Valo96bba982015-01-07 17:04:10 +0200190 /* Avoid linking error on devm_hwmon_device_register_with_groups, I
191 * guess linux/hwmon.h is missing proper stubs. */
Rajkumar Manoharana4031af2015-01-13 14:21:45 +0530192 if (!config_enabled(CONFIG_HWMON))
Kalle Valo96bba982015-01-07 17:04:10 +0200193 return 0;
194
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200195 hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
196 "ath10k_hwmon", ar,
197 ath10k_hwmon_groups);
198 if (IS_ERR(hwmon_dev)) {
199 ath10k_err(ar, "failed to register hwmon device: %ld\n",
200 PTR_ERR(hwmon_dev));
201 ret = -EINVAL;
202 goto err_remove_link;
203 }
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200204 return 0;
205
Rajkumar Manoharanac2953f2014-12-17 12:22:26 +0200206err_remove_link:
Rajkumar Manoharan7e47e8e2015-03-12 19:32:00 +0200207 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
Rajkumar Manoharanfe6f36d2014-12-17 12:22:07 +0200208err_cooling_destroy:
209 thermal_cooling_device_unregister(cdev);
210 return ret;
211}
212
213void ath10k_thermal_unregister(struct ath10k *ar)
214{
215 thermal_cooling_device_unregister(ar->thermal.cdev);
216 sysfs_remove_link(&ar->dev->kobj, "cooling_device");
217}