thermal_hwmon.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. long 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, "%ld\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. long temperature;
  88. int ret;
  89. ret = tz->ops->get_trip_temp(tz, 0, &temperature);
  90. if (ret)
  91. return ret;
  92. return sprintf(buf, "%ld\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. int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  123. {
  124. struct thermal_hwmon_device *hwmon;
  125. struct thermal_hwmon_temp *temp;
  126. int new_hwmon_device = 1;
  127. int result;
  128. hwmon = thermal_hwmon_lookup_by_type(tz);
  129. if (hwmon) {
  130. new_hwmon_device = 0;
  131. goto register_sys_interface;
  132. }
  133. hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
  134. if (!hwmon)
  135. return -ENOMEM;
  136. INIT_LIST_HEAD(&hwmon->tz_list);
  137. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  138. hwmon->device = hwmon_device_register(&tz->device);
  139. if (IS_ERR(hwmon->device)) {
  140. result = PTR_ERR(hwmon->device);
  141. goto free_mem;
  142. }
  143. dev_set_drvdata(hwmon->device, hwmon);
  144. result = device_create_file(hwmon->device, &dev_attr_name);
  145. if (result)
  146. goto free_mem;
  147. register_sys_interface:
  148. temp = kzalloc(sizeof(*temp), GFP_KERNEL);
  149. if (!temp) {
  150. result = -ENOMEM;
  151. goto unregister_name;
  152. }
  153. temp->tz = tz;
  154. hwmon->count++;
  155. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  156. "temp%d_input", hwmon->count);
  157. temp->temp_input.attr.attr.name = temp->temp_input.name;
  158. temp->temp_input.attr.attr.mode = 0444;
  159. temp->temp_input.attr.show = temp_input_show;
  160. sysfs_attr_init(&temp->temp_input.attr.attr);
  161. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  162. if (result)
  163. goto free_temp_mem;
  164. if (tz->ops->get_crit_temp) {
  165. unsigned long temperature;
  166. if (!tz->ops->get_crit_temp(tz, &temperature)) {
  167. snprintf(temp->temp_crit.name,
  168. sizeof(temp->temp_crit.name),
  169. "temp%d_crit", hwmon->count);
  170. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  171. temp->temp_crit.attr.attr.mode = 0444;
  172. temp->temp_crit.attr.show = temp_crit_show;
  173. sysfs_attr_init(&temp->temp_crit.attr.attr);
  174. result = device_create_file(hwmon->device,
  175. &temp->temp_crit.attr);
  176. if (result)
  177. goto unregister_input;
  178. }
  179. }
  180. mutex_lock(&thermal_hwmon_list_lock);
  181. if (new_hwmon_device)
  182. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  183. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  184. mutex_unlock(&thermal_hwmon_list_lock);
  185. return 0;
  186. unregister_input:
  187. device_remove_file(hwmon->device, &temp->temp_input.attr);
  188. free_temp_mem:
  189. kfree(temp);
  190. unregister_name:
  191. if (new_hwmon_device) {
  192. device_remove_file(hwmon->device, &dev_attr_name);
  193. hwmon_device_unregister(hwmon->device);
  194. }
  195. free_mem:
  196. if (new_hwmon_device)
  197. kfree(hwmon);
  198. return result;
  199. }
  200. void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  201. {
  202. struct thermal_hwmon_device *hwmon;
  203. struct thermal_hwmon_temp *temp;
  204. hwmon = thermal_hwmon_lookup_by_type(tz);
  205. if (unlikely(!hwmon)) {
  206. /* Should never happen... */
  207. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  208. return;
  209. }
  210. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  211. if (unlikely(!temp)) {
  212. /* Should never happen... */
  213. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  214. return;
  215. }
  216. device_remove_file(hwmon->device, &temp->temp_input.attr);
  217. if (tz->ops->get_crit_temp)
  218. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  219. mutex_lock(&thermal_hwmon_list_lock);
  220. list_del(&temp->hwmon_node);
  221. kfree(temp);
  222. if (!list_empty(&hwmon->tz_list)) {
  223. mutex_unlock(&thermal_hwmon_list_lock);
  224. return;
  225. }
  226. list_del(&hwmon->node);
  227. mutex_unlock(&thermal_hwmon_list_lock);
  228. device_remove_file(hwmon->device, &dev_attr_name);
  229. hwmon_device_unregister(hwmon->device);
  230. kfree(hwmon);
  231. }