Linux Audio

Check our new training course

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