thermal.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  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. #include <linux/device.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/thermal.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/hwmon-sysfs.h>
  21. #include "core.h"
  22. #include "debug.h"
  23. #include "wmi-ops.h"
  24. static int ath10k_thermal_get_active_vifs(struct ath10k *ar,
  25. enum wmi_vdev_type type)
  26. {
  27. struct ath10k_vif *arvif;
  28. int count = 0;
  29. lockdep_assert_held(&ar->conf_mutex);
  30. list_for_each_entry(arvif, &ar->arvifs, list) {
  31. if (!arvif->is_started)
  32. continue;
  33. if (!arvif->is_up)
  34. continue;
  35. if (arvif->vdev_type != type)
  36. continue;
  37. count++;
  38. }
  39. return count;
  40. }
  41. static int ath10k_thermal_get_max_dutycycle(struct thermal_cooling_device *cdev,
  42. unsigned long *state)
  43. {
  44. *state = ATH10K_QUIET_DUTY_CYCLE_MAX;
  45. return 0;
  46. }
  47. static int ath10k_thermal_get_cur_dutycycle(struct thermal_cooling_device *cdev,
  48. unsigned long *state)
  49. {
  50. struct ath10k *ar = cdev->devdata;
  51. mutex_lock(&ar->conf_mutex);
  52. *state = ar->thermal.duty_cycle;
  53. mutex_unlock(&ar->conf_mutex);
  54. return 0;
  55. }
  56. static int ath10k_thermal_set_cur_dutycycle(struct thermal_cooling_device *cdev,
  57. unsigned long duty_cycle)
  58. {
  59. struct ath10k *ar = cdev->devdata;
  60. u32 period, duration, enabled;
  61. int num_bss, ret = 0;
  62. mutex_lock(&ar->conf_mutex);
  63. if (ar->state != ATH10K_STATE_ON) {
  64. ret = -ENETDOWN;
  65. goto out;
  66. }
  67. if (duty_cycle > ATH10K_QUIET_DUTY_CYCLE_MAX) {
  68. ath10k_warn(ar, "duty cycle %ld is exceeding the limit %d\n",
  69. duty_cycle, ATH10K_QUIET_DUTY_CYCLE_MAX);
  70. ret = -EINVAL;
  71. goto out;
  72. }
  73. /* TODO: Right now, thermal mitigation is handled only for single/multi
  74. * vif AP mode. Since quiet param is not validated in STA mode, it needs
  75. * to be investigated further to handle multi STA and multi-vif (AP+STA)
  76. * mode properly.
  77. */
  78. num_bss = ath10k_thermal_get_active_vifs(ar, WMI_VDEV_TYPE_AP);
  79. if (!num_bss) {
  80. ath10k_warn(ar, "no active AP interfaces\n");
  81. ret = -ENETDOWN;
  82. goto out;
  83. }
  84. period = max(ATH10K_QUIET_PERIOD_MIN,
  85. (ATH10K_QUIET_PERIOD_DEFAULT / num_bss));
  86. duration = (period * duty_cycle) / 100;
  87. enabled = duration ? 1 : 0;
  88. ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
  89. ATH10K_QUIET_START_OFFSET,
  90. enabled);
  91. if (ret) {
  92. ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
  93. period, duration, enabled, ret);
  94. goto out;
  95. }
  96. ar->thermal.duty_cycle = duty_cycle;
  97. out:
  98. mutex_unlock(&ar->conf_mutex);
  99. return ret;
  100. }
  101. static struct thermal_cooling_device_ops ath10k_thermal_ops = {
  102. .get_max_state = ath10k_thermal_get_max_dutycycle,
  103. .get_cur_state = ath10k_thermal_get_cur_dutycycle,
  104. .set_cur_state = ath10k_thermal_set_cur_dutycycle,
  105. };
  106. static ssize_t ath10k_thermal_show_temp(struct device *dev,
  107. struct device_attribute *attr,
  108. char *buf)
  109. {
  110. struct ath10k *ar = dev_get_drvdata(dev);
  111. int ret, temperature;
  112. mutex_lock(&ar->conf_mutex);
  113. /* Can't get temperature when the card is off */
  114. if (ar->state != ATH10K_STATE_ON) {
  115. ret = -ENETDOWN;
  116. goto out;
  117. }
  118. reinit_completion(&ar->thermal.wmi_sync);
  119. ret = ath10k_wmi_pdev_get_temperature(ar);
  120. if (ret) {
  121. ath10k_warn(ar, "failed to read temperature %d\n", ret);
  122. goto out;
  123. }
  124. if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
  125. ret = -ESHUTDOWN;
  126. goto out;
  127. }
  128. ret = wait_for_completion_timeout(&ar->thermal.wmi_sync,
  129. ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
  130. if (ret == 0) {
  131. ath10k_warn(ar, "failed to synchronize thermal read\n");
  132. ret = -ETIMEDOUT;
  133. goto out;
  134. }
  135. spin_lock_bh(&ar->data_lock);
  136. temperature = ar->thermal.temperature;
  137. spin_unlock_bh(&ar->data_lock);
  138. /* display in millidegree celcius */
  139. ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
  140. out:
  141. mutex_unlock(&ar->conf_mutex);
  142. return ret;
  143. }
  144. void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
  145. {
  146. spin_lock_bh(&ar->data_lock);
  147. ar->thermal.temperature = temperature;
  148. spin_unlock_bh(&ar->data_lock);
  149. complete(&ar->thermal.wmi_sync);
  150. }
  151. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ath10k_thermal_show_temp,
  152. NULL, 0);
  153. static struct attribute *ath10k_hwmon_attrs[] = {
  154. &sensor_dev_attr_temp1_input.dev_attr.attr,
  155. NULL,
  156. };
  157. ATTRIBUTE_GROUPS(ath10k_hwmon);
  158. int ath10k_thermal_register(struct ath10k *ar)
  159. {
  160. struct thermal_cooling_device *cdev;
  161. struct device *hwmon_dev;
  162. int ret;
  163. cdev = thermal_cooling_device_register("ath10k_thermal", ar,
  164. &ath10k_thermal_ops);
  165. if (IS_ERR(cdev)) {
  166. ath10k_err(ar, "failed to setup thermal device result: %ld\n",
  167. PTR_ERR(cdev));
  168. return -EINVAL;
  169. }
  170. ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
  171. "cooling_device");
  172. if (ret) {
  173. ath10k_err(ar, "failed to create thermal symlink\n");
  174. goto err_cooling_destroy;
  175. }
  176. ar->thermal.cdev = cdev;
  177. /* Do not register hwmon device when temperature reading is not
  178. * supported by firmware
  179. */
  180. if (ar->wmi.op_version != ATH10K_FW_WMI_OP_VERSION_10_2_4)
  181. return 0;
  182. /* Avoid linking error on devm_hwmon_device_register_with_groups, I
  183. * guess linux/hwmon.h is missing proper stubs. */
  184. if (!config_enabled(CONFIG_HWMON))
  185. return 0;
  186. hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
  187. "ath10k_hwmon", ar,
  188. ath10k_hwmon_groups);
  189. if (IS_ERR(hwmon_dev)) {
  190. ath10k_err(ar, "failed to register hwmon device: %ld\n",
  191. PTR_ERR(hwmon_dev));
  192. ret = -EINVAL;
  193. goto err_remove_link;
  194. }
  195. return 0;
  196. err_remove_link:
  197. sysfs_remove_link(&ar->dev->kobj, "thermal_sensor");
  198. err_cooling_destroy:
  199. thermal_cooling_device_unregister(cdev);
  200. return ret;
  201. }
  202. void ath10k_thermal_unregister(struct ath10k *ar)
  203. {
  204. thermal_cooling_device_unregister(ar->thermal.cdev);
  205. sysfs_remove_link(&ar->dev->kobj, "cooling_device");
  206. }