1 // SPDX-License-Identifier: GPL-2.0
3 * thermal_hwmon.c - Generic Thermal Management hwmon support.
5 * Code based on Intel thermal_core.c. Copyrights of the original code:
6 * Copyright (C) 2008 Intel Corp
10 * Copyright (C) 2013 Texas Instruments
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/hwmon.h>
16 #include <linux/slab.h>
17 #include <linux/thermal.h>
19 #include "thermal_hwmon.h"
20 #include "thermal_core.h"
23 /* thermal zone devices with the same type share one hwmon device */
24 struct thermal_hwmon_device {
25 char type[THERMAL_NAME_LENGTH];
26 struct device *device;
28 struct list_head tz_list;
29 struct list_head node;
32 struct thermal_hwmon_attr {
33 struct device_attribute attr;
37 /* one temperature input for each thermal zone */
38 struct thermal_hwmon_temp {
39 struct list_head hwmon_node;
40 struct thermal_zone_device *tz;
41 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
42 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
45 static LIST_HEAD(thermal_hwmon_list);
47 static DEFINE_MUTEX(thermal_hwmon_list_lock);
50 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
54 struct thermal_hwmon_attr *hwmon_attr
55 = container_of(attr, struct thermal_hwmon_attr, attr);
56 struct thermal_hwmon_temp *temp
57 = container_of(hwmon_attr, struct thermal_hwmon_temp,
59 struct thermal_zone_device *tz = temp->tz;
61 ret = thermal_zone_get_temp(tz, &temperature);
66 return sprintf(buf, "%d\n", temperature);
70 temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
72 struct thermal_hwmon_attr *hwmon_attr
73 = container_of(attr, struct thermal_hwmon_attr, attr);
74 struct thermal_hwmon_temp *temp
75 = container_of(hwmon_attr, struct thermal_hwmon_temp,
77 struct thermal_zone_device *tz = temp->tz;
81 mutex_lock(&tz->lock);
83 if (device_is_registered(&tz->device))
84 ret = tz->ops->get_crit_temp(tz, &temperature);
88 mutex_unlock(&tz->lock);
93 return sprintf(buf, "%d\n", temperature);
97 static struct thermal_hwmon_device *
98 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
100 struct thermal_hwmon_device *hwmon;
101 char type[THERMAL_NAME_LENGTH];
103 mutex_lock(&thermal_hwmon_list_lock);
104 list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
105 strcpy(type, tz->type);
106 strreplace(type, '-', '_');
107 if (!strcmp(hwmon->type, type)) {
108 mutex_unlock(&thermal_hwmon_list_lock);
112 mutex_unlock(&thermal_hwmon_list_lock);
117 /* Find the temperature input matching a given thermal zone */
118 static struct thermal_hwmon_temp *
119 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
120 const struct thermal_zone_device *tz)
122 struct thermal_hwmon_temp *temp;
124 mutex_lock(&thermal_hwmon_list_lock);
125 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
126 if (temp->tz == tz) {
127 mutex_unlock(&thermal_hwmon_list_lock);
130 mutex_unlock(&thermal_hwmon_list_lock);
135 static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
138 return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
141 int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
143 struct thermal_hwmon_device *hwmon;
144 struct thermal_hwmon_temp *temp;
145 int new_hwmon_device = 1;
148 hwmon = thermal_hwmon_lookup_by_type(tz);
150 new_hwmon_device = 0;
151 goto register_sys_interface;
154 hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
158 INIT_LIST_HEAD(&hwmon->tz_list);
159 strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
160 strreplace(hwmon->type, '-', '_');
161 hwmon->device = hwmon_device_register_for_thermal(&tz->device,
163 if (IS_ERR(hwmon->device)) {
164 result = PTR_ERR(hwmon->device);
168 register_sys_interface:
169 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
172 goto unregister_name;
178 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
179 "temp%d_input", hwmon->count);
180 temp->temp_input.attr.attr.name = temp->temp_input.name;
181 temp->temp_input.attr.attr.mode = 0444;
182 temp->temp_input.attr.show = temp_input_show;
183 sysfs_attr_init(&temp->temp_input.attr.attr);
184 result = device_create_file(hwmon->device, &temp->temp_input.attr);
188 if (thermal_zone_crit_temp_valid(tz)) {
189 snprintf(temp->temp_crit.name,
190 sizeof(temp->temp_crit.name),
191 "temp%d_crit", hwmon->count);
192 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
193 temp->temp_crit.attr.attr.mode = 0444;
194 temp->temp_crit.attr.show = temp_crit_show;
195 sysfs_attr_init(&temp->temp_crit.attr.attr);
196 result = device_create_file(hwmon->device,
197 &temp->temp_crit.attr);
199 goto unregister_input;
202 mutex_lock(&thermal_hwmon_list_lock);
203 if (new_hwmon_device)
204 list_add_tail(&hwmon->node, &thermal_hwmon_list);
205 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
206 mutex_unlock(&thermal_hwmon_list_lock);
211 device_remove_file(hwmon->device, &temp->temp_input.attr);
215 if (new_hwmon_device)
216 hwmon_device_unregister(hwmon->device);
222 EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
224 void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
226 struct thermal_hwmon_device *hwmon;
227 struct thermal_hwmon_temp *temp;
229 hwmon = thermal_hwmon_lookup_by_type(tz);
230 if (unlikely(!hwmon)) {
231 /* Should never happen... */
232 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
236 temp = thermal_hwmon_lookup_temp(hwmon, tz);
237 if (unlikely(!temp)) {
238 /* Should never happen... */
239 dev_dbg(&tz->device, "temperature input lookup failed!\n");
243 device_remove_file(hwmon->device, &temp->temp_input.attr);
244 if (thermal_zone_crit_temp_valid(tz))
245 device_remove_file(hwmon->device, &temp->temp_crit.attr);
247 mutex_lock(&thermal_hwmon_list_lock);
248 list_del(&temp->hwmon_node);
250 if (!list_empty(&hwmon->tz_list)) {
251 mutex_unlock(&thermal_hwmon_list_lock);
254 list_del(&hwmon->node);
255 mutex_unlock(&thermal_hwmon_list_lock);
257 hwmon_device_unregister(hwmon->device);
260 EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
262 static void devm_thermal_hwmon_release(struct device *dev, void *res)
264 thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
267 int devm_thermal_add_hwmon_sysfs(struct device *dev, struct thermal_zone_device *tz)
269 struct thermal_zone_device **ptr;
272 ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
275 dev_warn(dev, "Failed to allocate device resource data\n");
279 ret = thermal_add_hwmon_sysfs(tz);
281 dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
287 devres_add(dev, ptr);
291 EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
293 MODULE_IMPORT_NS(HWMON_THERMAL);