xref: /linux/drivers/thermal/thermal_hwmon.c (revision ceaebef91d1aff671a512d5f003766ea41152d94)
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
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
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 *
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 	mutex_lock(&thermal_hwmon_list_lock);
99 	list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
100 		strscpy(type, tz->type);
101 		strreplace(type, '-', '_');
102 		if (!strcmp(hwmon->type, type)) {
103 			mutex_unlock(&thermal_hwmon_list_lock);
104 			return hwmon;
105 		}
106 	}
107 	mutex_unlock(&thermal_hwmon_list_lock);
108 
109 	return NULL;
110 }
111 
112 /* Find the temperature input matching a given thermal zone */
113 static struct thermal_hwmon_temp *
114 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
115 			  const struct thermal_zone_device *tz)
116 {
117 	struct thermal_hwmon_temp *temp;
118 
119 	mutex_lock(&thermal_hwmon_list_lock);
120 	list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
121 		if (temp->tz == tz) {
122 			mutex_unlock(&thermal_hwmon_list_lock);
123 			return temp;
124 		}
125 	mutex_unlock(&thermal_hwmon_list_lock);
126 
127 	return NULL;
128 }
129 
130 static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
131 {
132 	int temp;
133 	return tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &temp);
134 }
135 
136 int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
137 {
138 	struct thermal_hwmon_device *hwmon;
139 	struct thermal_hwmon_temp *temp;
140 	int new_hwmon_device = 1;
141 	int result;
142 
143 	hwmon = thermal_hwmon_lookup_by_type(tz);
144 	if (hwmon) {
145 		new_hwmon_device = 0;
146 		goto register_sys_interface;
147 	}
148 
149 	hwmon = kzalloc_obj(*hwmon);
150 	if (!hwmon)
151 		return -ENOMEM;
152 
153 	INIT_LIST_HEAD(&hwmon->tz_list);
154 	strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
155 	strreplace(hwmon->type, '-', '_');
156 	hwmon->device = hwmon_device_register_for_thermal(&tz->device,
157 							  hwmon->type, hwmon);
158 	if (IS_ERR(hwmon->device)) {
159 		result = PTR_ERR(hwmon->device);
160 		goto free_mem;
161 	}
162 
163  register_sys_interface:
164 	temp = kzalloc_obj(*temp);
165 	if (!temp) {
166 		result = -ENOMEM;
167 		goto unregister_name;
168 	}
169 
170 	temp->tz = tz;
171 	hwmon->count++;
172 
173 	snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
174 		 "temp%d_input", hwmon->count);
175 	temp->temp_input.attr.attr.name = temp->temp_input.name;
176 	temp->temp_input.attr.attr.mode = 0444;
177 	temp->temp_input.attr.show = temp_input_show;
178 	sysfs_attr_init(&temp->temp_input.attr.attr);
179 	result = device_create_file(hwmon->device, &temp->temp_input.attr);
180 	if (result)
181 		goto free_temp_mem;
182 
183 	if (thermal_zone_crit_temp_valid(tz)) {
184 		snprintf(temp->temp_crit.name,
185 				sizeof(temp->temp_crit.name),
186 				"temp%d_crit", hwmon->count);
187 		temp->temp_crit.attr.attr.name = temp->temp_crit.name;
188 		temp->temp_crit.attr.attr.mode = 0444;
189 		temp->temp_crit.attr.show = temp_crit_show;
190 		sysfs_attr_init(&temp->temp_crit.attr.attr);
191 		result = device_create_file(hwmon->device,
192 					    &temp->temp_crit.attr);
193 		if (result)
194 			goto unregister_input;
195 
196 		temp->temp_crit_present = true;
197 	}
198 
199 	mutex_lock(&thermal_hwmon_list_lock);
200 	if (new_hwmon_device)
201 		list_add_tail(&hwmon->node, &thermal_hwmon_list);
202 	list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
203 	mutex_unlock(&thermal_hwmon_list_lock);
204 
205 	return 0;
206 
207  unregister_input:
208 	device_remove_file(hwmon->device, &temp->temp_input.attr);
209  free_temp_mem:
210 	kfree(temp);
211  unregister_name:
212 	if (new_hwmon_device)
213 		hwmon_device_unregister(hwmon->device);
214  free_mem:
215 	if (new_hwmon_device)
216 		kfree(hwmon);
217 
218 	return result;
219 }
220 EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
221 
222 void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
223 {
224 	struct thermal_hwmon_device *hwmon;
225 	struct thermal_hwmon_temp *temp;
226 
227 	hwmon = thermal_hwmon_lookup_by_type(tz);
228 	if (unlikely(!hwmon)) {
229 		/* Should never happen... */
230 		dev_dbg(&tz->device, "hwmon device lookup failed!\n");
231 		return;
232 	}
233 
234 	temp = thermal_hwmon_lookup_temp(hwmon, tz);
235 	if (unlikely(!temp)) {
236 		/* Should never happen... */
237 		dev_dbg(&tz->device, "temperature input lookup failed!\n");
238 		return;
239 	}
240 
241 	device_remove_file(hwmon->device, &temp->temp_input.attr);
242 	if (temp->temp_crit_present)
243 		device_remove_file(hwmon->device, &temp->temp_crit.attr);
244 
245 	mutex_lock(&thermal_hwmon_list_lock);
246 	list_del(&temp->hwmon_node);
247 	kfree(temp);
248 	if (!list_empty(&hwmon->tz_list)) {
249 		mutex_unlock(&thermal_hwmon_list_lock);
250 		return;
251 	}
252 	list_del(&hwmon->node);
253 	mutex_unlock(&thermal_hwmon_list_lock);
254 
255 	hwmon_device_unregister(hwmon->device);
256 	kfree(hwmon);
257 }
258 EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
259 
260 static void devm_thermal_hwmon_release(struct device *dev, void *res)
261 {
262 	thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
263 }
264 
265 int devm_thermal_add_hwmon_sysfs(struct device *dev, struct thermal_zone_device *tz)
266 {
267 	struct thermal_zone_device **ptr;
268 	int ret;
269 
270 	ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
271 			   GFP_KERNEL);
272 	if (!ptr) {
273 		dev_warn(dev, "Failed to allocate device resource data\n");
274 		return -ENOMEM;
275 	}
276 
277 	ret = thermal_add_hwmon_sysfs(tz);
278 	if (ret) {
279 		dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
280 		devres_free(ptr);
281 		return ret;
282 	}
283 
284 	*ptr = tz;
285 	devres_add(dev, ptr);
286 
287 	return ret;
288 }
289 EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
290 
291 MODULE_IMPORT_NS("HWMON_THERMAL");
292