thermal.c 5.4 KB

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