thermal_hwmon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/hwmon.h>
  14. #include <linux/thermal.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include "thermal_hwmon.h"
  18. /* hwmon sys I/F */
  19. /* thermal zone devices with the same type share one hwmon device */
  20. struct thermal_hwmon_device {
  21. char type[THERMAL_NAME_LENGTH];
  22. struct device *device;
  23. int count;
  24. struct list_head tz_list;
  25. struct list_head node;
  26. };
  27. struct thermal_hwmon_attr {
  28. struct device_attribute attr;
  29. char name[16];
  30. };
  31. /* one temperature input for each thermal zone */
  32. struct thermal_hwmon_temp {
  33. struct list_head hwmon_node;
  34. struct thermal_zone_device *tz;
  35. struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
  36. struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
  37. };
  38. static LIST_HEAD(thermal_hwmon_list);
  39. static DEFINE_MUTEX(thermal_hwmon_list_lock);
  40. static ssize_t
  41. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  42. {
  43. int temperature;
  44. int ret;
  45. struct thermal_hwmon_attr *hwmon_attr
  46. = container_of(attr, struct thermal_hwmon_attr, attr);
  47. struct thermal_hwmon_temp *temp
  48. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  49. temp_input);
  50. struct thermal_zone_device *tz = temp->tz;
  51. ret = thermal_zone_get_temp(tz, &temperature);
  52. if (ret)
  53. return ret;
  54. return sprintf(buf, "%d\n", temperature);
  55. }
  56. static ssize_t
  57. temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
  58. {
  59. struct thermal_hwmon_attr *hwmon_attr
  60. = container_of(attr, struct thermal_hwmon_attr, attr);
  61. struct thermal_hwmon_temp *temp
  62. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  63. temp_crit);
  64. struct thermal_zone_device *tz = temp->tz;
  65. int temperature;
  66. int ret;
  67. ret = tz->ops->get_crit_temp(tz, &temperature);
  68. if (ret)
  69. return ret;
  70. return sprintf(buf, "%d\n", temperature);
  71. }
  72. static struct thermal_hwmon_device *
  73. thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
  74. {
  75. struct thermal_hwmon_device *hwmon;
  76. mutex_lock(&thermal_hwmon_list_lock);
  77. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  78. if (!strcmp(hwmon->type, tz->type)) {
  79. mutex_unlock(&thermal_hwmon_list_lock);
  80. return hwmon;
  81. }
  82. mutex_unlock(&thermal_hwmon_list_lock);
  83. return NULL;
  84. }
  85. /* Find the temperature input matching a given thermal zone */
  86. static struct thermal_hwmon_temp *
  87. thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
  88. const struct thermal_zone_device *tz)
  89. {
  90. struct thermal_hwmon_temp *temp;
  91. mutex_lock(&thermal_hwmon_list_lock);
  92. list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
  93. if (temp->tz == tz) {
  94. mutex_unlock(&thermal_hwmon_list_lock);
  95. return temp;
  96. }
  97. mutex_unlock(&thermal_hwmon_list_lock);
  98. return NULL;
  99. }
  100. static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
  101. {
  102. int temp;
  103. return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
  104. }
  105. int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  106. {
  107. struct thermal_hwmon_device *hwmon;
  108. struct thermal_hwmon_temp *temp;
  109. int new_hwmon_device = 1;
  110. int result;
  111. hwmon = thermal_hwmon_lookup_by_type(tz);
  112. if (hwmon) {
  113. new_hwmon_device = 0;
  114. goto register_sys_interface;
  115. }
  116. hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
  117. if (!hwmon)
  118. return -ENOMEM;
  119. INIT_LIST_HEAD(&hwmon->tz_list);
  120. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  121. hwmon->device = hwmon_device_register_with_info(NULL, hwmon->type,
  122. hwmon, NULL, NULL);
  123. if (IS_ERR(hwmon->device)) {
  124. result = PTR_ERR(hwmon->device);
  125. goto free_mem;
  126. }
  127. register_sys_interface:
  128. temp = kzalloc(sizeof(*temp), GFP_KERNEL);
  129. if (!temp) {
  130. result = -ENOMEM;
  131. goto unregister_name;
  132. }
  133. temp->tz = tz;
  134. hwmon->count++;
  135. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  136. "temp%d_input", hwmon->count);
  137. temp->temp_input.attr.attr.name = temp->temp_input.name;
  138. temp->temp_input.attr.attr.mode = 0444;
  139. temp->temp_input.attr.show = temp_input_show;
  140. sysfs_attr_init(&temp->temp_input.attr.attr);
  141. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  142. if (result)
  143. goto free_temp_mem;
  144. if (thermal_zone_crit_temp_valid(tz)) {
  145. snprintf(temp->temp_crit.name,
  146. sizeof(temp->temp_crit.name),
  147. "temp%d_crit", hwmon->count);
  148. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  149. temp->temp_crit.attr.attr.mode = 0444;
  150. temp->temp_crit.attr.show = temp_crit_show;
  151. sysfs_attr_init(&temp->temp_crit.attr.attr);
  152. result = device_create_file(hwmon->device,
  153. &temp->temp_crit.attr);
  154. if (result)
  155. goto unregister_input;
  156. }
  157. mutex_lock(&thermal_hwmon_list_lock);
  158. if (new_hwmon_device)
  159. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  160. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  161. mutex_unlock(&thermal_hwmon_list_lock);
  162. return 0;
  163. unregister_input:
  164. device_remove_file(hwmon->device, &temp->temp_input.attr);
  165. free_temp_mem:
  166. kfree(temp);
  167. unregister_name:
  168. if (new_hwmon_device)
  169. hwmon_device_unregister(hwmon->device);
  170. free_mem:
  171. if (new_hwmon_device)
  172. kfree(hwmon);
  173. return result;
  174. }
  175. EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
  176. void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  177. {
  178. struct thermal_hwmon_device *hwmon;
  179. struct thermal_hwmon_temp *temp;
  180. hwmon = thermal_hwmon_lookup_by_type(tz);
  181. if (unlikely(!hwmon)) {
  182. /* Should never happen... */
  183. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  184. return;
  185. }
  186. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  187. if (unlikely(!temp)) {
  188. /* Should never happen... */
  189. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  190. return;
  191. }
  192. device_remove_file(hwmon->device, &temp->temp_input.attr);
  193. if (thermal_zone_crit_temp_valid(tz))
  194. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  195. mutex_lock(&thermal_hwmon_list_lock);
  196. list_del(&temp->hwmon_node);
  197. kfree(temp);
  198. if (!list_empty(&hwmon->tz_list)) {
  199. mutex_unlock(&thermal_hwmon_list_lock);
  200. return;
  201. }
  202. list_del(&hwmon->node);
  203. mutex_unlock(&thermal_hwmon_list_lock);
  204. hwmon_device_unregister(hwmon->device);
  205. kfree(hwmon);
  206. }
  207. EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);