1*4882a593Smuzhiyun // SPDX-License-Identifier: ISC
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2014-2015 Qualcomm Atheros, Inc.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/device.h>
7*4882a593Smuzhiyun #include <linux/sysfs.h>
8*4882a593Smuzhiyun #include <linux/thermal.h>
9*4882a593Smuzhiyun #include <linux/hwmon.h>
10*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
11*4882a593Smuzhiyun #include "core.h"
12*4882a593Smuzhiyun #include "debug.h"
13*4882a593Smuzhiyun #include "wmi-ops.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun static int
ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device * cdev,unsigned long * state)16*4882a593Smuzhiyun ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
17*4882a593Smuzhiyun unsigned long *state)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun *state = ATH10K_THERMAL_THROTTLE_MAX;
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun return 0;
22*4882a593Smuzhiyun }
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun static int
ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device * cdev,unsigned long * state)25*4882a593Smuzhiyun ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
26*4882a593Smuzhiyun unsigned long *state)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun struct ath10k *ar = cdev->devdata;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun mutex_lock(&ar->conf_mutex);
31*4882a593Smuzhiyun *state = ar->thermal.throttle_state;
32*4882a593Smuzhiyun mutex_unlock(&ar->conf_mutex);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun return 0;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun static int
ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device * cdev,unsigned long throttle_state)38*4882a593Smuzhiyun ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
39*4882a593Smuzhiyun unsigned long throttle_state)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun struct ath10k *ar = cdev->devdata;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) {
44*4882a593Smuzhiyun ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n",
45*4882a593Smuzhiyun throttle_state, ATH10K_THERMAL_THROTTLE_MAX);
46*4882a593Smuzhiyun return -EINVAL;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun mutex_lock(&ar->conf_mutex);
49*4882a593Smuzhiyun ar->thermal.throttle_state = throttle_state;
50*4882a593Smuzhiyun ath10k_thermal_set_throttling(ar);
51*4882a593Smuzhiyun mutex_unlock(&ar->conf_mutex);
52*4882a593Smuzhiyun return 0;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static const struct thermal_cooling_device_ops ath10k_thermal_ops = {
56*4882a593Smuzhiyun .get_max_state = ath10k_thermal_get_max_throttle_state,
57*4882a593Smuzhiyun .get_cur_state = ath10k_thermal_get_cur_throttle_state,
58*4882a593Smuzhiyun .set_cur_state = ath10k_thermal_set_cur_throttle_state,
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
ath10k_thermal_show_temp(struct device * dev,struct device_attribute * attr,char * buf)61*4882a593Smuzhiyun static ssize_t ath10k_thermal_show_temp(struct device *dev,
62*4882a593Smuzhiyun struct device_attribute *attr,
63*4882a593Smuzhiyun char *buf)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun struct ath10k *ar = dev_get_drvdata(dev);
66*4882a593Smuzhiyun int ret, temperature;
67*4882a593Smuzhiyun unsigned long time_left;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun mutex_lock(&ar->conf_mutex);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* Can't get temperature when the card is off */
72*4882a593Smuzhiyun if (ar->state != ATH10K_STATE_ON) {
73*4882a593Smuzhiyun ret = -ENETDOWN;
74*4882a593Smuzhiyun goto out;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun reinit_completion(&ar->thermal.wmi_sync);
78*4882a593Smuzhiyun ret = ath10k_wmi_pdev_get_temperature(ar);
79*4882a593Smuzhiyun if (ret) {
80*4882a593Smuzhiyun ath10k_warn(ar, "failed to read temperature %d\n", ret);
81*4882a593Smuzhiyun goto out;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
85*4882a593Smuzhiyun ret = -ESHUTDOWN;
86*4882a593Smuzhiyun goto out;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync,
90*4882a593Smuzhiyun ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
91*4882a593Smuzhiyun if (!time_left) {
92*4882a593Smuzhiyun ath10k_warn(ar, "failed to synchronize thermal read\n");
93*4882a593Smuzhiyun ret = -ETIMEDOUT;
94*4882a593Smuzhiyun goto out;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun spin_lock_bh(&ar->data_lock);
98*4882a593Smuzhiyun temperature = ar->thermal.temperature;
99*4882a593Smuzhiyun spin_unlock_bh(&ar->data_lock);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* display in millidegree celcius */
102*4882a593Smuzhiyun ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
103*4882a593Smuzhiyun out:
104*4882a593Smuzhiyun mutex_unlock(&ar->conf_mutex);
105*4882a593Smuzhiyun return ret;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
ath10k_thermal_event_temperature(struct ath10k * ar,int temperature)108*4882a593Smuzhiyun void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun spin_lock_bh(&ar->data_lock);
111*4882a593Smuzhiyun ar->thermal.temperature = temperature;
112*4882a593Smuzhiyun spin_unlock_bh(&ar->data_lock);
113*4882a593Smuzhiyun complete(&ar->thermal.wmi_sync);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR(temp1_input, 0444, ath10k_thermal_show_temp,
117*4882a593Smuzhiyun NULL, 0);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun static struct attribute *ath10k_hwmon_attrs[] = {
120*4882a593Smuzhiyun &sensor_dev_attr_temp1_input.dev_attr.attr,
121*4882a593Smuzhiyun NULL,
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun ATTRIBUTE_GROUPS(ath10k_hwmon);
124*4882a593Smuzhiyun
ath10k_thermal_set_throttling(struct ath10k * ar)125*4882a593Smuzhiyun void ath10k_thermal_set_throttling(struct ath10k *ar)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun u32 period, duration, enabled;
128*4882a593Smuzhiyun int ret;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun lockdep_assert_held(&ar->conf_mutex);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))
133*4882a593Smuzhiyun return;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (!ar->wmi.ops->gen_pdev_set_quiet_mode)
136*4882a593Smuzhiyun return;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if (ar->state != ATH10K_STATE_ON)
139*4882a593Smuzhiyun return;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun period = ar->thermal.quiet_period;
142*4882a593Smuzhiyun duration = (period * ar->thermal.throttle_state) / 100;
143*4882a593Smuzhiyun enabled = duration ? 1 : 0;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
146*4882a593Smuzhiyun ATH10K_QUIET_START_OFFSET,
147*4882a593Smuzhiyun enabled);
148*4882a593Smuzhiyun if (ret) {
149*4882a593Smuzhiyun ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
150*4882a593Smuzhiyun period, duration, enabled, ret);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
ath10k_thermal_register(struct ath10k * ar)154*4882a593Smuzhiyun int ath10k_thermal_register(struct ath10k *ar)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun struct thermal_cooling_device *cdev;
157*4882a593Smuzhiyun struct device *hwmon_dev;
158*4882a593Smuzhiyun int ret;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))
161*4882a593Smuzhiyun return 0;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun cdev = thermal_cooling_device_register("ath10k_thermal", ar,
164*4882a593Smuzhiyun &ath10k_thermal_ops);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun if (IS_ERR(cdev)) {
167*4882a593Smuzhiyun ath10k_err(ar, "failed to setup thermal device result: %ld\n",
168*4882a593Smuzhiyun PTR_ERR(cdev));
169*4882a593Smuzhiyun return -EINVAL;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
173*4882a593Smuzhiyun "cooling_device");
174*4882a593Smuzhiyun if (ret) {
175*4882a593Smuzhiyun ath10k_err(ar, "failed to create cooling device symlink\n");
176*4882a593Smuzhiyun goto err_cooling_destroy;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun ar->thermal.cdev = cdev;
180*4882a593Smuzhiyun ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* Do not register hwmon device when temperature reading is not
183*4882a593Smuzhiyun * supported by firmware
184*4882a593Smuzhiyun */
185*4882a593Smuzhiyun if (!(ar->wmi.ops->gen_pdev_get_temperature))
186*4882a593Smuzhiyun return 0;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* Avoid linking error on devm_hwmon_device_register_with_groups, I
189*4882a593Smuzhiyun * guess linux/hwmon.h is missing proper stubs.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun if (!IS_REACHABLE(CONFIG_HWMON))
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
195*4882a593Smuzhiyun "ath10k_hwmon", ar,
196*4882a593Smuzhiyun ath10k_hwmon_groups);
197*4882a593Smuzhiyun if (IS_ERR(hwmon_dev)) {
198*4882a593Smuzhiyun ath10k_err(ar, "failed to register hwmon device: %ld\n",
199*4882a593Smuzhiyun PTR_ERR(hwmon_dev));
200*4882a593Smuzhiyun ret = -EINVAL;
201*4882a593Smuzhiyun goto err_remove_link;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun return 0;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun err_remove_link:
206*4882a593Smuzhiyun sysfs_remove_link(&ar->dev->kobj, "cooling_device");
207*4882a593Smuzhiyun err_cooling_destroy:
208*4882a593Smuzhiyun thermal_cooling_device_unregister(cdev);
209*4882a593Smuzhiyun return ret;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
ath10k_thermal_unregister(struct ath10k * ar)212*4882a593Smuzhiyun void ath10k_thermal_unregister(struct ath10k *ar)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun if (!test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map))
215*4882a593Smuzhiyun return;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun sysfs_remove_link(&ar->dev->kobj, "cooling_device");
218*4882a593Smuzhiyun thermal_cooling_device_unregister(ar->thermal.cdev);
219*4882a593Smuzhiyun }
220