thermal_hwmon.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * thermal_hwmon.c - Generic Thermal Management hwmon support.
  3. *
  4. * Code based on Intel thermal_core.c. Copyrights of the original code:
  5. * Copyright (C) 2008 Intel Corp
  6. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  7. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  8. *
  9. * Copyright (C) 2013 Texas Instruments
  10. * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
  11. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; version 2 of the License.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. */
  28. #include <linux/hwmon.h>
  29. #include <linux/thermal.h>
  30. #include <linux/slab.h>
  31. #include <linux/err.h>
  32. #include "thermal_hwmon.h"
  33. /* hwmon sys I/F */
  34. /* thermal zone devices with the same type share one hwmon device */
  35. struct thermal_hwmon_device {
  36. char type[THERMAL_NAME_LENGTH];
  37. struct device *device;
  38. int count;
  39. struct list_head tz_list;
  40. struct list_head node;
  41. };
  42. struct thermal_hwmon_attr {
  43. struct device_attribute attr;
  44. char name[16];
  45. };
  46. /* one temperature input for each thermal zone */
  47. struct thermal_hwmon_temp {
  48. struct list_head hwmon_node;
  49. struct thermal_zone_device *tz;
  50. struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
  51. struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
  52. };
  53. static LIST_HEAD(thermal_hwmon_list);
  54. static DEFINE_MUTEX(thermal_hwmon_list_lock);
  55. static ssize_t
  56. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  57. {
  58. struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
  59. return sprintf(buf, "%s\n", hwmon->type);
  60. }
  61. static DEVICE_ATTR(name, 0444, name_show, NULL);
  62. static ssize_t
  63. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  64. {
  65. int temperature;
  66. int ret;
  67. struct thermal_hwmon_attr *hwmon_attr
  68. = container_of(attr, struct thermal_hwmon_attr, attr);
  69. struct thermal_hwmon_temp *temp
  70. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  71. temp_input);
  72. struct thermal_zone_device *tz = temp->tz;
  73. ret = thermal_zone_get_temp(tz, &temperature);
  74. if (ret)
  75. return ret;
  76. return sprintf(buf, "%d\n", temperature);
  77. }
  78. static ssize_t
  79. temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
  80. {
  81. struct thermal_hwmon_attr *hwmon_attr
  82. = container_of(attr, struct thermal_hwmon_attr, attr);
  83. struct thermal_hwmon_temp *temp
  84. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  85. temp_crit);
  86. struct thermal_zone_device *tz = temp->tz;
  87. int temperature;
  88. int ret;
  89. ret = tz->ops->get_crit_temp(tz, &temperature);
  90. if (ret)
  91. return ret;
  92. return sprintf(buf, "%d\n", temperature);
  93. }
  94. static struct thermal_hwmon_device *
  95. thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
  96. {
  97. struct thermal_hwmon_device *hwmon;
  98. mutex_lock(&thermal_hwmon_list_lock);
  99. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  100. if (!strcmp(hwmon->type, tz->type)) {
  101. mutex_unlock(&thermal_hwmon_list_lock);
  102. return hwmon;
  103. }
  104. mutex_unlock(&thermal_hwmon_list_lock);
  105. return NULL;
  106. }
  107. /* Find the temperature input matching a given thermal zone */
  108. static struct thermal_hwmon_temp *
  109. thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
  110. const struct thermal_zone_device *tz)
  111. {
  112. struct thermal_hwmon_temp *temp;
  113. mutex_lock(&thermal_hwmon_list_lock);
  114. list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
  115. if (temp->tz == tz) {
  116. mutex_unlock(&thermal_hwmon_list_lock);
  117. return temp;
  118. }
  119. mutex_unlock(&thermal_hwmon_list_lock);
  120. return NULL;
  121. }
  122. static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
  123. {
  124. int temp;
  125. return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
  126. }
  127. int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  128. {
  129. struct thermal_hwmon_device *hwmon;
  130. struct thermal_hwmon_temp *temp;
  131. int new_hwmon_device = 1;
  132. int result;
  133. hwmon = thermal_hwmon_lookup_by_type(tz);
  134. if (hwmon) {
  135. new_hwmon_device = 0;
  136. goto register_sys_interface;
  137. }
  138. hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
  139. if (!hwmon)
  140. return -ENOMEM;
  141. INIT_LIST_HEAD(&hwmon->tz_list);
  142. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  143. hwmon->device = hwmon_device_register(NULL);
  144. if (IS_ERR(hwmon->device)) {
  145. result = PTR_ERR(hwmon->device);
  146. goto free_mem;
  147. }
  148. dev_set_drvdata(hwmon->device, hwmon);
  149. result = device_create_file(hwmon->device, &dev_attr_name);
  150. if (result)
  151. goto free_mem;
  152. register_sys_interface:
  153. temp = kzalloc(sizeof(*temp), GFP_KERNEL);
  154. if (!temp) {
  155. result = -ENOMEM;
  156. goto unregister_name;
  157. }
  158. temp->tz = tz;
  159. hwmon->count++;
  160. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  161. "temp%d_input", hwmon->count);
  162. temp->temp_input.attr.attr.name = temp->temp_input.name;
  163. temp->temp_input.attr.attr.mode = 0444;
  164. temp->temp_input.attr.show = temp_input_show;
  165. sysfs_attr_init(&temp->temp_input.attr.attr);
  166. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  167. if (result)
  168. goto free_temp_mem;
  169. if (thermal_zone_crit_temp_valid(tz)) {
  170. snprintf(temp->temp_crit.name,
  171. sizeof(temp->temp_crit.name),
  172. "temp%d_crit", hwmon->count);
  173. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  174. temp->temp_crit.attr.attr.mode = 0444;
  175. temp->temp_crit.attr.show = temp_crit_show;
  176. sysfs_attr_init(&temp->temp_crit.attr.attr);
  177. result = device_create_file(hwmon->device,
  178. &temp->temp_crit.attr);
  179. if (result)
  180. goto unregister_input;
  181. }
  182. mutex_lock(&thermal_hwmon_list_lock);
  183. if (new_hwmon_device)
  184. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  185. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  186. mutex_unlock(&thermal_hwmon_list_lock);
  187. return 0;
  188. unregister_input:
  189. device_remove_file(hwmon->device, &temp->temp_input.attr);
  190. free_temp_mem:
  191. kfree(temp);
  192. unregister_name:
  193. if (new_hwmon_device) {
  194. device_remove_file(hwmon->device, &dev_attr_name);
  195. hwmon_device_unregister(hwmon->device);
  196. }
  197. free_mem:
  198. if (new_hwmon_device)
  199. kfree(hwmon);
  200. return result;
  201. }
  202. void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  203. {
  204. struct thermal_hwmon_device *hwmon;
  205. struct thermal_hwmon_temp *temp;
  206. hwmon = thermal_hwmon_lookup_by_type(tz);
  207. if (unlikely(!hwmon)) {
  208. /* Should never happen... */
  209. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  210. return;
  211. }
  212. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  213. if (unlikely(!temp)) {
  214. /* Should never happen... */
  215. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  216. return;
  217. }
  218. device_remove_file(hwmon->device, &temp->temp_input.attr);
  219. if (thermal_zone_crit_temp_valid(tz))
  220. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  221. mutex_lock(&thermal_hwmon_list_lock);
  222. list_del(&temp->hwmon_node);
  223. kfree(temp);
  224. if (!list_empty(&hwmon->tz_list)) {
  225. mutex_unlock(&thermal_hwmon_list_lock);
  226. return;
  227. }
  228. list_del(&hwmon->node);
  229. mutex_unlock(&thermal_hwmon_list_lock);
  230. device_remove_file(hwmon->device, &dev_attr_name);
  231. hwmon_device_unregister(hwmon->device);
  232. kfree(hwmon);
  233. }