xref: /linux/drivers/thermal/thermal_hwmon.c (revision fab183d632628381b466a41479489541ac0e29a0)
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/err.h>
14 #include <linux/export.h>
15 #include <linux/hwmon.h>
16 #include <linux/slab.h>
17 #include <linux/thermal.h>
18 
19 #include "thermal_hwmon.h"
20 #include "thermal_core.h"
21 
22 /* hwmon sys I/F */
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;
27 	int count;
28 	struct list_head tz_list;
29 	struct list_head node;
30 };
31 
32 struct thermal_hwmon_attr {
33 	struct device_attribute attr;
34 	char name[16];
35 };
36 
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 */
43 	bool temp_crit_present;
44 };
45 
46 static LIST_HEAD(thermal_hwmon_list);
47 
48 static DEFINE_MUTEX(thermal_hwmon_list_lock);
49 
50 static ssize_t
temp_input_show(struct device * dev,struct device_attribute * attr,char * buf)51 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
52 {
53 	int temperature;
54 	int ret;
55 	struct thermal_hwmon_attr *hwmon_attr
56 			= container_of(attr, struct thermal_hwmon_attr, attr);
57 	struct thermal_hwmon_temp *temp
58 			= container_of(hwmon_attr, struct thermal_hwmon_temp,
59 				       temp_input);
60 	struct thermal_zone_device *tz = temp->tz;
61 
62 	ret = thermal_zone_get_temp(tz, &temperature);
63 
64 	if (ret)
65 		return ret;
66 
67 	return sysfs_emit(buf, "%d\n", temperature);
68 }
69 
70 static ssize_t
temp_crit_show(struct device * dev,struct device_attribute * attr,char * buf)71 temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
72 {
73 	struct thermal_hwmon_attr *hwmon_attr
74 			= container_of(attr, struct thermal_hwmon_attr, attr);
75 	struct thermal_hwmon_temp *temp
76 			= container_of(hwmon_attr, struct thermal_hwmon_temp,
77 				       temp_crit);
78 	struct thermal_zone_device *tz = temp->tz;
79 	int temperature;
80 	int ret;
81 
82 	guard(thermal_zone)(tz);
83 
84 	ret = tz->ops.get_crit_temp(tz, &temperature);
85 	if (ret)
86 		return ret;
87 
88 	return sysfs_emit(buf, "%d\n", temperature);
89 }
90 
91 
92 static struct thermal_hwmon_device *
thermal_hwmon_lookup_by_type(const struct thermal_zone_device * tz)93 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
94 {
95 	struct thermal_hwmon_device *hwmon;
96 	char type[THERMAL_NAME_LENGTH];
97 
98 	list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
99 		strscpy(type, tz->type);
100 		strreplace(type, '-', '_');
101 		if (!strcmp(hwmon->type, type))
102 			return hwmon;
103 	}
104 
105 	return NULL;
106 }
107 
thermal_zone_crit_temp_valid(struct thermal_zone_device * tz)108 static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
109 {
110 	int temp;
111 	return tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &temp);
112 }
113 
thermal_add_hwmon_sysfs(struct thermal_zone_device * tz)114 int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
115 {
116 	struct thermal_hwmon_device *hwmon;
117 	struct thermal_hwmon_temp *temp;
118 	int new_hwmon_device = 1;
119 	int result = 0;
120 
121 	mutex_lock(&thermal_hwmon_list_lock);
122 
123 	hwmon = thermal_hwmon_lookup_by_type(tz);
124 	if (hwmon) {
125 		new_hwmon_device = 0;
126 		goto register_sys_interface;
127 	}
128 
129 	hwmon = kzalloc_obj(*hwmon);
130 	if (!hwmon) {
131 		result = -ENOMEM;
132 		goto unlock;
133 	}
134 
135 	INIT_LIST_HEAD(&hwmon->tz_list);
136 	strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
137 	strreplace(hwmon->type, '-', '_');
138 	hwmon->device = hwmon_device_register_for_thermal(&tz->device,
139 							  hwmon->type, hwmon);
140 	if (IS_ERR(hwmon->device)) {
141 		result = PTR_ERR(hwmon->device);
142 		goto free_mem;
143 	}
144 
145  register_sys_interface:
146 	temp = kzalloc_obj(*temp);
147 	if (!temp) {
148 		result = -ENOMEM;
149 		goto unregister_name;
150 	}
151 
152 	temp->tz = tz;
153 	hwmon->count++;
154 
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 
165 	if (thermal_zone_crit_temp_valid(tz)) {
166 		snprintf(temp->temp_crit.name,
167 				sizeof(temp->temp_crit.name),
168 				"temp%d_crit", hwmon->count);
169 		temp->temp_crit.attr.attr.name = temp->temp_crit.name;
170 		temp->temp_crit.attr.attr.mode = 0444;
171 		temp->temp_crit.attr.show = temp_crit_show;
172 		sysfs_attr_init(&temp->temp_crit.attr.attr);
173 		result = device_create_file(hwmon->device,
174 					    &temp->temp_crit.attr);
175 		if (result)
176 			goto unregister_input;
177 
178 		temp->temp_crit_present = true;
179 	}
180 
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 
185 	goto unlock;
186 
187 unregister_input:
188 	device_remove_file(hwmon->device, &temp->temp_input.attr);
189 free_temp_mem:
190 	kfree(temp);
191 unregister_name:
192 	if (new_hwmon_device)
193 		hwmon_device_unregister(hwmon->device);
194 free_mem:
195 	if (new_hwmon_device)
196 		kfree(hwmon);
197 unlock:
198 	mutex_unlock(&thermal_hwmon_list_lock);
199 
200 	return result;
201 }
202 EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
203 
thermal_remove_hwmon_sysfs(struct thermal_zone_device * tz)204 void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
205 {
206 	struct thermal_hwmon_temp *temp, *entry;
207 	struct thermal_hwmon_device *hwmon;
208 	bool unregister;
209 
210 	guard(mutex)(&thermal_hwmon_list_lock);
211 
212 	hwmon = thermal_hwmon_lookup_by_type(tz);
213 	if (unlikely(!hwmon)) {
214 		/* Should never happen... */
215 		dev_dbg(&tz->device, "hwmon device lookup failed!\n");
216 		return;
217 	}
218 
219 	unregister = hwmon->device->parent == &tz->device;
220 
221 	list_for_each_entry_safe_reverse(temp, entry, &hwmon->tz_list, hwmon_node) {
222 		if (!unregister && temp->tz != tz)
223 			continue;
224 
225 		device_remove_file(hwmon->device, &temp->temp_input.attr);
226 		if (temp->temp_crit_present)
227 			device_remove_file(hwmon->device, &temp->temp_crit.attr);
228 
229 		list_del(&temp->hwmon_node);
230 		kfree(temp);
231 	}
232 
233 	if (unregister) {
234 		list_del(&hwmon->node);
235 		hwmon_device_unregister(hwmon->device);
236 		kfree(hwmon);
237 	}
238 }
239 EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
240 
devm_thermal_hwmon_release(struct device * dev,void * res)241 static void devm_thermal_hwmon_release(struct device *dev, void *res)
242 {
243 	thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
244 }
245 
devm_thermal_add_hwmon_sysfs(struct device * dev,struct thermal_zone_device * tz)246 int devm_thermal_add_hwmon_sysfs(struct device *dev, struct thermal_zone_device *tz)
247 {
248 	struct thermal_zone_device **ptr;
249 	int ret;
250 
251 	ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
252 			   GFP_KERNEL);
253 	if (!ptr) {
254 		dev_warn(dev, "Failed to allocate device resource data\n");
255 		return -ENOMEM;
256 	}
257 
258 	ret = thermal_add_hwmon_sysfs(tz);
259 	if (ret) {
260 		dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
261 		devres_free(ptr);
262 		return ret;
263 	}
264 
265 	*ptr = tz;
266 	devres_add(dev, ptr);
267 
268 	return ret;
269 }
270 EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
271 
272 MODULE_IMPORT_NS("HWMON_THERMAL");
273