thermal_hwmon.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * thermal_hwmon.c - Generic Thermal Management hwmon support.
  4. *
  5. * Code based on Intel thermal_core.c. Copyrights of the original code:
  6. * Copyright (C) 2008 Intel Corp
  7. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  8. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  9. *
  10. * Copyright (C) 2013 Texas Instruments
  11. * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
  12. */
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/slab.h>
  17. #include <linux/thermal.h>
  18. #include "thermal_hwmon.h"
  19. #include "thermal_core.h"
  20. /* hwmon sys I/F */
  21. /* thermal zone devices with the same type share one hwmon device */
  22. struct thermal_hwmon_device {
  23. char type[THERMAL_NAME_LENGTH];
  24. struct device *device;
  25. int count;
  26. struct list_head tz_list;
  27. struct list_head node;
  28. };
  29. struct thermal_hwmon_attr {
  30. struct device_attribute attr;
  31. char name[16];
  32. };
  33. /* one temperature input for each thermal zone */
  34. struct thermal_hwmon_temp {
  35. struct list_head hwmon_node;
  36. struct thermal_zone_device *tz;
  37. struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
  38. struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
  39. };
  40. static LIST_HEAD(thermal_hwmon_list);
  41. static DEFINE_MUTEX(thermal_hwmon_list_lock);
  42. static ssize_t
  43. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  44. {
  45. int temperature;
  46. int ret;
  47. struct thermal_hwmon_attr *hwmon_attr
  48. = container_of(attr, struct thermal_hwmon_attr, attr);
  49. struct thermal_hwmon_temp *temp
  50. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  51. temp_input);
  52. struct thermal_zone_device *tz = temp->tz;
  53. ret = thermal_zone_get_temp(tz, &temperature);
  54. if (ret)
  55. return ret;
  56. return sysfs_emit(buf, "%d\n", temperature);
  57. }
  58. static ssize_t
  59. temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
  60. {
  61. struct thermal_hwmon_attr *hwmon_attr
  62. = container_of(attr, struct thermal_hwmon_attr, attr);
  63. struct thermal_hwmon_temp *temp
  64. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  65. temp_crit);
  66. struct thermal_zone_device *tz = temp->tz;
  67. int temperature;
  68. int ret;
  69. guard(thermal_zone)(tz);
  70. ret = tz->ops.get_crit_temp(tz, &temperature);
  71. if (ret)
  72. return ret;
  73. return sysfs_emit(buf, "%d\n", temperature);
  74. }
  75. static struct thermal_hwmon_device *
  76. thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
  77. {
  78. struct thermal_hwmon_device *hwmon;
  79. char type[THERMAL_NAME_LENGTH];
  80. mutex_lock(&thermal_hwmon_list_lock);
  81. list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
  82. strscpy(type, tz->type);
  83. strreplace(type, '-', '_');
  84. if (!strcmp(hwmon->type, type)) {
  85. mutex_unlock(&thermal_hwmon_list_lock);
  86. return hwmon;
  87. }
  88. }
  89. mutex_unlock(&thermal_hwmon_list_lock);
  90. return NULL;
  91. }
  92. /* Find the temperature input matching a given thermal zone */
  93. static struct thermal_hwmon_temp *
  94. thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
  95. const struct thermal_zone_device *tz)
  96. {
  97. struct thermal_hwmon_temp *temp;
  98. mutex_lock(&thermal_hwmon_list_lock);
  99. list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
  100. if (temp->tz == tz) {
  101. mutex_unlock(&thermal_hwmon_list_lock);
  102. return temp;
  103. }
  104. mutex_unlock(&thermal_hwmon_list_lock);
  105. return NULL;
  106. }
  107. static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
  108. {
  109. int temp;
  110. return tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &temp);
  111. }
  112. int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  113. {
  114. struct thermal_hwmon_device *hwmon;
  115. struct thermal_hwmon_temp *temp;
  116. int new_hwmon_device = 1;
  117. int result;
  118. hwmon = thermal_hwmon_lookup_by_type(tz);
  119. if (hwmon) {
  120. new_hwmon_device = 0;
  121. goto register_sys_interface;
  122. }
  123. hwmon = kzalloc_obj(*hwmon);
  124. if (!hwmon)
  125. return -ENOMEM;
  126. INIT_LIST_HEAD(&hwmon->tz_list);
  127. strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  128. strreplace(hwmon->type, '-', '_');
  129. hwmon->device = hwmon_device_register_for_thermal(&tz->device,
  130. hwmon->type, hwmon);
  131. if (IS_ERR(hwmon->device)) {
  132. result = PTR_ERR(hwmon->device);
  133. goto free_mem;
  134. }
  135. register_sys_interface:
  136. temp = kzalloc_obj(*temp);
  137. if (!temp) {
  138. result = -ENOMEM;
  139. goto unregister_name;
  140. }
  141. temp->tz = tz;
  142. hwmon->count++;
  143. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  144. "temp%d_input", hwmon->count);
  145. temp->temp_input.attr.attr.name = temp->temp_input.name;
  146. temp->temp_input.attr.attr.mode = 0444;
  147. temp->temp_input.attr.show = temp_input_show;
  148. sysfs_attr_init(&temp->temp_input.attr.attr);
  149. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  150. if (result)
  151. goto free_temp_mem;
  152. if (thermal_zone_crit_temp_valid(tz)) {
  153. snprintf(temp->temp_crit.name,
  154. sizeof(temp->temp_crit.name),
  155. "temp%d_crit", hwmon->count);
  156. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  157. temp->temp_crit.attr.attr.mode = 0444;
  158. temp->temp_crit.attr.show = temp_crit_show;
  159. sysfs_attr_init(&temp->temp_crit.attr.attr);
  160. result = device_create_file(hwmon->device,
  161. &temp->temp_crit.attr);
  162. if (result)
  163. goto unregister_input;
  164. }
  165. mutex_lock(&thermal_hwmon_list_lock);
  166. if (new_hwmon_device)
  167. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  168. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  169. mutex_unlock(&thermal_hwmon_list_lock);
  170. return 0;
  171. unregister_input:
  172. device_remove_file(hwmon->device, &temp->temp_input.attr);
  173. free_temp_mem:
  174. kfree(temp);
  175. unregister_name:
  176. if (new_hwmon_device)
  177. hwmon_device_unregister(hwmon->device);
  178. free_mem:
  179. kfree(hwmon);
  180. return result;
  181. }
  182. EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
  183. void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  184. {
  185. struct thermal_hwmon_device *hwmon;
  186. struct thermal_hwmon_temp *temp;
  187. hwmon = thermal_hwmon_lookup_by_type(tz);
  188. if (unlikely(!hwmon)) {
  189. /* Should never happen... */
  190. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  191. return;
  192. }
  193. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  194. if (unlikely(!temp)) {
  195. /* Should never happen... */
  196. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  197. return;
  198. }
  199. device_remove_file(hwmon->device, &temp->temp_input.attr);
  200. if (thermal_zone_crit_temp_valid(tz))
  201. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  202. mutex_lock(&thermal_hwmon_list_lock);
  203. list_del(&temp->hwmon_node);
  204. kfree(temp);
  205. if (!list_empty(&hwmon->tz_list)) {
  206. mutex_unlock(&thermal_hwmon_list_lock);
  207. return;
  208. }
  209. list_del(&hwmon->node);
  210. mutex_unlock(&thermal_hwmon_list_lock);
  211. hwmon_device_unregister(hwmon->device);
  212. kfree(hwmon);
  213. }
  214. EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
  215. static void devm_thermal_hwmon_release(struct device *dev, void *res)
  216. {
  217. thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
  218. }
  219. int devm_thermal_add_hwmon_sysfs(struct device *dev, struct thermal_zone_device *tz)
  220. {
  221. struct thermal_zone_device **ptr;
  222. int ret;
  223. ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
  224. GFP_KERNEL);
  225. if (!ptr) {
  226. dev_warn(dev, "Failed to allocate device resource data\n");
  227. return -ENOMEM;
  228. }
  229. ret = thermal_add_hwmon_sysfs(tz);
  230. if (ret) {
  231. dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
  232. devres_free(ptr);
  233. return ret;
  234. }
  235. *ptr = tz;
  236. devres_add(dev, ptr);
  237. return ret;
  238. }
  239. EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
  240. MODULE_IMPORT_NS("HWMON_THERMAL");