thermal_hwmon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. strreplace(hwmon->type, '-', '_');
  122. hwmon->device = hwmon_device_register_with_info(&tz->device, hwmon->type,
  123. hwmon, NULL, NULL);
  124. if (IS_ERR(hwmon->device)) {
  125. result = PTR_ERR(hwmon->device);
  126. goto free_mem;
  127. }
  128. register_sys_interface:
  129. temp = kzalloc(sizeof(*temp), GFP_KERNEL);
  130. if (!temp) {
  131. result = -ENOMEM;
  132. goto unregister_name;
  133. }
  134. temp->tz = tz;
  135. hwmon->count++;
  136. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  137. "temp%d_input", hwmon->count);
  138. temp->temp_input.attr.attr.name = temp->temp_input.name;
  139. temp->temp_input.attr.attr.mode = 0444;
  140. temp->temp_input.attr.show = temp_input_show;
  141. sysfs_attr_init(&temp->temp_input.attr.attr);
  142. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  143. if (result)
  144. goto free_temp_mem;
  145. if (thermal_zone_crit_temp_valid(tz)) {
  146. snprintf(temp->temp_crit.name,
  147. sizeof(temp->temp_crit.name),
  148. "temp%d_crit", hwmon->count);
  149. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  150. temp->temp_crit.attr.attr.mode = 0444;
  151. temp->temp_crit.attr.show = temp_crit_show;
  152. sysfs_attr_init(&temp->temp_crit.attr.attr);
  153. result = device_create_file(hwmon->device,
  154. &temp->temp_crit.attr);
  155. if (result)
  156. goto unregister_input;
  157. }
  158. mutex_lock(&thermal_hwmon_list_lock);
  159. if (new_hwmon_device)
  160. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  161. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  162. mutex_unlock(&thermal_hwmon_list_lock);
  163. return 0;
  164. unregister_input:
  165. device_remove_file(hwmon->device, &temp->temp_input.attr);
  166. free_temp_mem:
  167. kfree(temp);
  168. unregister_name:
  169. if (new_hwmon_device)
  170. hwmon_device_unregister(hwmon->device);
  171. free_mem:
  172. if (new_hwmon_device)
  173. kfree(hwmon);
  174. return result;
  175. }
  176. EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
  177. void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  178. {
  179. struct thermal_hwmon_device *hwmon;
  180. struct thermal_hwmon_temp *temp;
  181. hwmon = thermal_hwmon_lookup_by_type(tz);
  182. if (unlikely(!hwmon)) {
  183. /* Should never happen... */
  184. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  185. return;
  186. }
  187. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  188. if (unlikely(!temp)) {
  189. /* Should never happen... */
  190. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  191. return;
  192. }
  193. device_remove_file(hwmon->device, &temp->temp_input.attr);
  194. if (thermal_zone_crit_temp_valid(tz))
  195. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  196. mutex_lock(&thermal_hwmon_list_lock);
  197. list_del(&temp->hwmon_node);
  198. kfree(temp);
  199. if (!list_empty(&hwmon->tz_list)) {
  200. mutex_unlock(&thermal_hwmon_list_lock);
  201. return;
  202. }
  203. list_del(&hwmon->node);
  204. mutex_unlock(&thermal_hwmon_list_lock);
  205. hwmon_device_unregister(hwmon->device);
  206. kfree(hwmon);
  207. }
  208. EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);