|  | 
|  | 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 "core.h" | 
|  | 10 | +#include "debug.h" | 
|  | 11 | + | 
|  | 12 | +static int | 
|  | 13 | +ath11k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev, | 
|  | 14 | +				      unsigned long *state) | 
|  | 15 | +{ | 
|  | 16 | +	*state = ATH11K_THERMAL_THROTTLE_MAX; | 
|  | 17 | + | 
|  | 18 | +	return 0; | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +static int | 
|  | 22 | +ath11k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev, | 
|  | 23 | +				      unsigned long *state) | 
|  | 24 | +{ | 
|  | 25 | +	struct ath11k *ar = cdev->devdata; | 
|  | 26 | + | 
|  | 27 | +	mutex_lock(&ar->conf_mutex); | 
|  | 28 | +	*state = ar->thermal.throttle_state; | 
|  | 29 | +	mutex_unlock(&ar->conf_mutex); | 
|  | 30 | + | 
|  | 31 | +	return 0; | 
|  | 32 | +} | 
|  | 33 | + | 
|  | 34 | +static int | 
|  | 35 | +ath11k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev, | 
|  | 36 | +				      unsigned long throttle_state) | 
|  | 37 | +{ | 
|  | 38 | +	struct ath11k *ar = cdev->devdata; | 
|  | 39 | +	int ret; | 
|  | 40 | + | 
|  | 41 | +	if (throttle_state > ATH11K_THERMAL_THROTTLE_MAX) { | 
|  | 42 | +		ath11k_warn(ar->ab, "throttle state %ld is exceeding the limit %d\n", | 
|  | 43 | +			    throttle_state, ATH11K_THERMAL_THROTTLE_MAX); | 
|  | 44 | +		return -EINVAL; | 
|  | 45 | +	} | 
|  | 46 | +	mutex_lock(&ar->conf_mutex); | 
|  | 47 | +	ret = ath11k_thermal_set_throttling(ar, throttle_state); | 
|  | 48 | +	if (ret == 0) | 
|  | 49 | +		ar->thermal.throttle_state = throttle_state; | 
|  | 50 | +	mutex_unlock(&ar->conf_mutex); | 
|  | 51 | +	return ret; | 
|  | 52 | +} | 
|  | 53 | + | 
|  | 54 | +static struct thermal_cooling_device_ops ath11k_thermal_ops = { | 
|  | 55 | +	.get_max_state = ath11k_thermal_get_max_throttle_state, | 
|  | 56 | +	.get_cur_state = ath11k_thermal_get_cur_throttle_state, | 
|  | 57 | +	.set_cur_state = ath11k_thermal_set_cur_throttle_state, | 
|  | 58 | +}; | 
|  | 59 | + | 
|  | 60 | +int ath11k_thermal_set_throttling(struct ath11k *ar, u32 throttle_state) | 
|  | 61 | +{ | 
|  | 62 | +	struct ath11k_base *sc = ar->ab; | 
|  | 63 | +	struct thermal_mitigation_params param; | 
|  | 64 | +	int ret = 0; | 
|  | 65 | + | 
|  | 66 | +	lockdep_assert_held(&ar->conf_mutex); | 
|  | 67 | + | 
|  | 68 | +	if (ar->state != ATH11K_STATE_ON) | 
|  | 69 | +		return 0; | 
|  | 70 | + | 
|  | 71 | +	memset(¶m, 0, sizeof(param)); | 
|  | 72 | +	param.pdev_id = ar->pdev->pdev_id; | 
|  | 73 | +	param.enable = throttle_state ? 1 : 0; | 
|  | 74 | +	param.dc = ATH11K_THERMAL_DEFAULT_DUTY_CYCLE; | 
|  | 75 | +	param.dc_per_event = 0xFFFFFFFF; | 
|  | 76 | + | 
|  | 77 | +	param.levelconf[0].tmplwm = ATH11K_THERMAL_TEMP_LOW_MARK; | 
|  | 78 | +	param.levelconf[0].tmphwm = ATH11K_THERMAL_TEMP_HIGH_MARK; | 
|  | 79 | +	param.levelconf[0].dcoffpercent = throttle_state; | 
|  | 80 | +	param.levelconf[0].priority = 0; /* disable all data tx queues */ | 
|  | 81 | + | 
|  | 82 | +	ret = ath11k_wmi_send_thermal_mitigation_param_cmd(ar, ¶m); | 
|  | 83 | +	if (ret) { | 
|  | 84 | +		ath11k_warn(sc, "failed to send thermal mitigation duty cycle %u ret %d\n", | 
|  | 85 | +			    throttle_state, ret); | 
|  | 86 | +	} | 
|  | 87 | + | 
|  | 88 | +	return ret; | 
|  | 89 | +} | 
|  | 90 | + | 
|  | 91 | +int ath11k_thermal_register(struct ath11k_base *sc) | 
|  | 92 | +{ | 
|  | 93 | +	struct thermal_cooling_device *cdev; | 
|  | 94 | +	struct ath11k *ar; | 
|  | 95 | +	struct ath11k_pdev *pdev; | 
|  | 96 | +	int i, ret; | 
|  | 97 | + | 
|  | 98 | +	for (i = 0; i < sc->num_radios; i++) { | 
|  | 99 | +		pdev = &sc->pdevs[i]; | 
|  | 100 | +		ar = pdev->ar; | 
|  | 101 | +		if (!ar) | 
|  | 102 | +			continue; | 
|  | 103 | + | 
|  | 104 | +		cdev = thermal_cooling_device_register("ath11k_thermal", ar, | 
|  | 105 | +						       &ath11k_thermal_ops); | 
|  | 106 | + | 
|  | 107 | +		if (IS_ERR(cdev)) { | 
|  | 108 | +			ath11k_err(sc, "failed to setup thermal device result: %ld\n", | 
|  | 109 | +				   PTR_ERR(cdev)); | 
|  | 110 | +			return -EINVAL; | 
|  | 111 | +		} | 
|  | 112 | + | 
|  | 113 | +		ret = sysfs_create_link(&ar->hw->wiphy->dev.kobj, &cdev->device.kobj, | 
|  | 114 | +					"cooling_device"); | 
|  | 115 | +		if (ret) { | 
|  | 116 | +			ath11k_err(sc, "failed to create cooling device symlink\n"); | 
|  | 117 | +			goto err_thermal_destroy; | 
|  | 118 | +		} | 
|  | 119 | + | 
|  | 120 | +		ar->thermal.cdev = cdev; | 
|  | 121 | +	} | 
|  | 122 | + | 
|  | 123 | +	return 0; | 
|  | 124 | + | 
|  | 125 | +err_thermal_destroy: | 
|  | 126 | +	ath11k_thermal_unregister(sc); | 
|  | 127 | +	return ret; | 
|  | 128 | +} | 
|  | 129 | + | 
|  | 130 | +void ath11k_thermal_unregister(struct ath11k_base *sc) | 
|  | 131 | +{ | 
|  | 132 | +	struct ath11k *ar; | 
|  | 133 | +	struct ath11k_pdev *pdev; | 
|  | 134 | +	int i; | 
|  | 135 | + | 
|  | 136 | +	for (i = 0; i < sc->num_radios; i++) { | 
|  | 137 | +		pdev = &sc->pdevs[i]; | 
|  | 138 | +		ar = pdev->ar; | 
|  | 139 | +		if (!ar) | 
|  | 140 | +			continue; | 
|  | 141 | + | 
|  | 142 | +		sysfs_remove_link(&ar->hw->wiphy->dev.kobj, "cooling_device"); | 
|  | 143 | +		thermal_cooling_device_unregister(ar->thermal.cdev); | 
|  | 144 | +	} | 
|  | 145 | +} | 
0 commit comments