thermal_hwmon.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. char type[THERMAL_NAME_LENGTH];
  77. mutex_lock(&thermal_hwmon_list_lock);
  78. list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
  79. strcpy(type, tz->type);
  80. strreplace(type, '-', '_');
  81. if (!strcmp(hwmon->type, type)) {
  82. mutex_unlock(&thermal_hwmon_list_lock);
  83. return hwmon;
  84. }
  85. }
  86. mutex_unlock(&thermal_hwmon_list_lock);
  87. return NULL;
  88. }
  89. /* Find the temperature input matching a given thermal zone */
  90. static struct thermal_hwmon_temp *
  91. thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
  92. const struct thermal_zone_device *tz)
  93. {
  94. struct thermal_hwmon_temp *temp;
  95. mutex_lock(&thermal_hwmon_list_lock);
  96. list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
  97. if (temp->tz == tz) {
  98. mutex_unlock(&thermal_hwmon_list_lock);
  99. return temp;
  100. }
  101. mutex_unlock(&thermal_hwmon_list_lock);
  102. return NULL;
  103. }
  104. static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
  105. {
  106. int temp;
  107. return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
  108. }
  109. int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  110. {
  111. struct thermal_hwmon_device *hwmon;
  112. struct thermal_hwmon_temp *temp;
  113. int new_hwmon_device = 1;
  114. int result;
  115. hwmon = thermal_hwmon_lookup_by_type(tz);
  116. if (hwmon) {
  117. new_hwmon_device = 0;
  118. goto register_sys_interface;
  119. }
  120. hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
  121. if (!hwmon)
  122. return -ENOMEM;
  123. INIT_LIST_HEAD(&hwmon->tz_list);
  124. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  125. strreplace(hwmon->type, '-', '_');
  126. hwmon->device = hwmon_device_register_with_info(&tz->device, hwmon->type,
  127. hwmon, NULL, NULL);
  128. if (IS_ERR(hwmon->device)) {
  129. result = PTR_ERR(hwmon->device);
  130. goto free_mem;
  131. }
  132. register_sys_interface:
  133. temp = kzalloc(sizeof(*temp), GFP_KERNEL);
  134. if (!temp) {
  135. result = -ENOMEM;
  136. goto unregister_name;
  137. }
  138. temp->tz = tz;
  139. hwmon->count++;
  140. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  141. "temp%d_input", hwmon->count);
  142. temp->temp_input.attr.attr.name = temp->temp_input.name;
  143. temp->temp_input.attr.attr.mode = 0444;
  144. temp->temp_input.attr.show = temp_input_show;
  145. sysfs_attr_init(&temp->temp_input.attr.attr);
  146. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  147. if (result)
  148. goto free_temp_mem;
  149. if (thermal_zone_crit_temp_valid(tz)) {
  150. snprintf(temp->temp_crit.name,
  151. sizeof(temp->temp_crit.name),
  152. "temp%d_crit", hwmon->count);
  153. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  154. temp->temp_crit.attr.attr.mode = 0444;
  155. temp->temp_crit.attr.show = temp_crit_show;
  156. sysfs_attr_init(&temp->temp_crit.attr.attr);
  157. result = device_create_file(hwmon->device,
  158. &temp->temp_crit.attr);
  159. if (result)
  160. goto unregister_input;
  161. }
  162. mutex_lock(&thermal_hwmon_list_lock);
  163. if (new_hwmon_device)
  164. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  165. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  166. mutex_unlock(&thermal_hwmon_list_lock);
  167. return 0;
  168. unregister_input:
  169. device_remove_file(hwmon->device, &temp->temp_input.attr);
  170. free_temp_mem:
  171. kfree(temp);
  172. unregister_name:
  173. if (new_hwmon_device)
  174. hwmon_device_unregister(hwmon->device);
  175. free_mem:
  176. if (new_hwmon_device)
  177. kfree(hwmon);
  178. return result;
  179. }
  180. EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
  181. void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  182. {
  183. struct thermal_hwmon_device *hwmon;
  184. struct thermal_hwmon_temp *temp;
  185. hwmon = thermal_hwmon_lookup_by_type(tz);
  186. if (unlikely(!hwmon)) {
  187. /* Should never happen... */
  188. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  189. return;
  190. }
  191. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  192. if (unlikely(!temp)) {
  193. /* Should never happen... */
  194. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  195. return;
  196. }
  197. device_remove_file(hwmon->device, &temp->temp_input.attr);
  198. if (thermal_zone_crit_temp_valid(tz))
  199. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  200. mutex_lock(&thermal_hwmon_list_lock);
  201. list_del(&temp->hwmon_node);
  202. kfree(temp);
  203. if (!list_empty(&hwmon->tz_list)) {
  204. mutex_unlock(&thermal_hwmon_list_lock);
  205. return;
  206. }
  207. list_del(&hwmon->node);
  208. mutex_unlock(&thermal_hwmon_list_lock);
  209. hwmon_device_unregister(hwmon->device);
  210. kfree(hwmon);
  211. }
  212. EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);