thermal.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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
  25. ath10k_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
  26. unsigned long *state)
  27. {
  28. *state = ATH10K_THERMAL_THROTTLE_MAX;
  29. return 0;
  30. }
  31. static int
  32. ath10k_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
  33. unsigned long *state)
  34. {
  35. struct ath10k *ar = cdev->devdata;
  36. mutex_lock(&ar->conf_mutex);
  37. *state = ar->thermal.throttle_state;
  38. mutex_unlock(&ar->conf_mutex);
  39. return 0;
  40. }
  41. static int
  42. ath10k_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
  43. unsigned long throttle_state)
  44. {
  45. struct ath10k *ar = cdev->devdata;
  46. if (throttle_state > ATH10K_THERMAL_THROTTLE_MAX) {
  47. ath10k_warn(ar, "throttle state %ld is exceeding the limit %d\n",
  48. throttle_state, ATH10K_THERMAL_THROTTLE_MAX);
  49. return -EINVAL;
  50. }
  51. mutex_lock(&ar->conf_mutex);
  52. ar->thermal.throttle_state = throttle_state;
  53. ath10k_thermal_set_throttling(ar);
  54. mutex_unlock(&ar->conf_mutex);
  55. return 0;
  56. }
  57. static struct thermal_cooling_device_ops ath10k_thermal_ops = {
  58. .get_max_state = ath10k_thermal_get_max_throttle_state,
  59. .get_cur_state = ath10k_thermal_get_cur_throttle_state,
  60. .set_cur_state = ath10k_thermal_set_cur_throttle_state,
  61. };
  62. static 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. mutex_lock(&ar->conf_mutex);
  70. /* Can't get temperature when the card is off */
  71. if (ar->state != ATH10K_STATE_ON) {
  72. ret = -ENETDOWN;
  73. goto out;
  74. }
  75. reinit_completion(&ar->thermal.wmi_sync);
  76. ret = ath10k_wmi_pdev_get_temperature(ar);
  77. if (ret) {
  78. ath10k_warn(ar, "failed to read temperature %d\n", ret);
  79. goto out;
  80. }
  81. if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
  82. ret = -ESHUTDOWN;
  83. goto out;
  84. }
  85. time_left = wait_for_completion_timeout(&ar->thermal.wmi_sync,
  86. ATH10K_THERMAL_SYNC_TIMEOUT_HZ);
  87. if (!time_left) {
  88. ath10k_warn(ar, "failed to synchronize thermal read\n");
  89. ret = -ETIMEDOUT;
  90. goto out;
  91. }
  92. spin_lock_bh(&ar->data_lock);
  93. temperature = ar->thermal.temperature;
  94. spin_unlock_bh(&ar->data_lock);
  95. /* display in millidegree celcius */
  96. ret = snprintf(buf, PAGE_SIZE, "%d\n", temperature * 1000);
  97. out:
  98. mutex_unlock(&ar->conf_mutex);
  99. return ret;
  100. }
  101. void ath10k_thermal_event_temperature(struct ath10k *ar, int temperature)
  102. {
  103. spin_lock_bh(&ar->data_lock);
  104. ar->thermal.temperature = temperature;
  105. spin_unlock_bh(&ar->data_lock);
  106. complete(&ar->thermal.wmi_sync);
  107. }
  108. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ath10k_thermal_show_temp,
  109. NULL, 0);
  110. static struct attribute *ath10k_hwmon_attrs[] = {
  111. &sensor_dev_attr_temp1_input.dev_attr.attr,
  112. NULL,
  113. };
  114. ATTRIBUTE_GROUPS(ath10k_hwmon);
  115. void ath10k_thermal_set_throttling(struct ath10k *ar)
  116. {
  117. u32 period, duration, enabled;
  118. int ret;
  119. lockdep_assert_held(&ar->conf_mutex);
  120. if (!ar->wmi.ops->gen_pdev_set_quiet_mode)
  121. return;
  122. if (ar->state != ATH10K_STATE_ON)
  123. return;
  124. period = ar->thermal.quiet_period;
  125. duration = (period * ar->thermal.throttle_state) / 100;
  126. enabled = duration ? 1 : 0;
  127. ret = ath10k_wmi_pdev_set_quiet_mode(ar, period, duration,
  128. ATH10K_QUIET_START_OFFSET,
  129. enabled);
  130. if (ret) {
  131. ath10k_warn(ar, "failed to set quiet mode period %u duarion %u enabled %u ret %d\n",
  132. period, duration, enabled, ret);
  133. }
  134. }
  135. int ath10k_thermal_register(struct ath10k *ar)
  136. {
  137. struct thermal_cooling_device *cdev;
  138. struct device *hwmon_dev;
  139. int ret;
  140. cdev = thermal_cooling_device_register("ath10k_thermal", ar,
  141. &ath10k_thermal_ops);
  142. if (IS_ERR(cdev)) {
  143. ath10k_err(ar, "failed to setup thermal device result: %ld\n",
  144. PTR_ERR(cdev));
  145. return -EINVAL;
  146. }
  147. ret = sysfs_create_link(&ar->dev->kobj, &cdev->device.kobj,
  148. "cooling_device");
  149. if (ret) {
  150. ath10k_err(ar, "failed to create cooling device symlink\n");
  151. goto err_cooling_destroy;
  152. }
  153. ar->thermal.cdev = cdev;
  154. ar->thermal.quiet_period = ATH10K_QUIET_PERIOD_DEFAULT;
  155. /* Do not register hwmon device when temperature reading is not
  156. * supported by firmware
  157. */
  158. if (!(ar->wmi.ops->gen_pdev_get_temperature))
  159. return 0;
  160. /* Avoid linking error on devm_hwmon_device_register_with_groups, I
  161. * guess linux/hwmon.h is missing proper stubs. */
  162. if (!IS_REACHABLE(CONFIG_HWMON))
  163. return 0;
  164. hwmon_dev = devm_hwmon_device_register_with_groups(ar->dev,
  165. "ath10k_hwmon", ar,
  166. ath10k_hwmon_groups);
  167. if (IS_ERR(hwmon_dev)) {
  168. ath10k_err(ar, "failed to register hwmon device: %ld\n",
  169. PTR_ERR(hwmon_dev));
  170. ret = -EINVAL;
  171. goto err_remove_link;
  172. }
  173. return 0;
  174. err_remove_link:
  175. sysfs_remove_link(&ar->dev->kobj, "cooling_device");
  176. err_cooling_destroy:
  177. thermal_cooling_device_unregister(cdev);
  178. return ret;
  179. }
  180. void ath10k_thermal_unregister(struct ath10k *ar)
  181. {
  182. sysfs_remove_link(&ar->dev->kobj, "cooling_device");
  183. thermal_cooling_device_unregister(ar->thermal.cdev);
  184. }