xref: /linux/drivers/thermal/thermal_of.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  of-thermal.c - Generic Thermal Management device tree support.
4  *
5  *  Copyright (C) 2013 Texas Instruments
6  *  Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/of.h>
14 #include <linux/slab.h>
15 #include <linux/thermal.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
18 
19 #include "thermal_core.h"
20 
21 /***   functions parsing device tree nodes   ***/
22 
23 /*
24  * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
25  * into the device tree binding of 'trip', property type.
26  */
27 static const char * const trip_types[] = {
28 	[THERMAL_TRIP_ACTIVE]	= "active",
29 	[THERMAL_TRIP_PASSIVE]	= "passive",
30 	[THERMAL_TRIP_HOT]	= "hot",
31 	[THERMAL_TRIP_CRITICAL]	= "critical",
32 };
33 
34 /**
35  * thermal_of_get_trip_type - Get phy mode for given device_node
36  * @np:	Pointer to the given device_node
37  * @type: Pointer to resulting trip type
38  *
39  * The function gets trip type string from property 'type',
40  * and store its index in trip_types table in @type,
41  *
42  * Return: 0 on success, or errno in error case.
43  */
44 static int thermal_of_get_trip_type(struct device_node *np,
45 				    enum thermal_trip_type *type)
46 {
47 	const char *t;
48 	int err, i;
49 
50 	err = of_property_read_string(np, "type", &t);
51 	if (err < 0)
52 		return err;
53 
54 	for (i = 0; i < ARRAY_SIZE(trip_types); i++)
55 		if (!strcasecmp(t, trip_types[i])) {
56 			*type = i;
57 			return 0;
58 		}
59 
60 	return -ENODEV;
61 }
62 
63 static int thermal_of_populate_trip(struct device_node *np,
64 				    struct thermal_trip *trip)
65 {
66 	int prop;
67 	int ret;
68 
69 	ret = of_property_read_u32(np, "temperature", &prop);
70 	if (ret < 0) {
71 		pr_err("missing temperature property\n");
72 		return ret;
73 	}
74 	trip->temperature = prop;
75 
76 	ret = of_property_read_u32(np, "hysteresis", &prop);
77 	if (ret < 0) {
78 		pr_err("missing hysteresis property\n");
79 		return ret;
80 	}
81 	trip->hysteresis = prop;
82 
83 	ret = thermal_of_get_trip_type(np, &trip->type);
84 	if (ret < 0) {
85 		pr_err("wrong trip type property\n");
86 		return ret;
87 	}
88 
89 	trip->flags = THERMAL_TRIP_FLAG_RW_TEMP;
90 
91 	trip->priv = np;
92 
93 	return 0;
94 }
95 
96 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
97 {
98 	struct thermal_trip *tt;
99 	struct device_node *trips;
100 	int ret, count;
101 
102 	*ntrips = 0;
103 
104 	trips = of_get_child_by_name(np, "trips");
105 	if (!trips)
106 		return NULL;
107 
108 	count = of_get_child_count(trips);
109 	if (!count)
110 		return NULL;
111 
112 	tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
113 	if (!tt) {
114 		ret = -ENOMEM;
115 		goto out_of_node_put;
116 	}
117 
118 	*ntrips = count;
119 
120 	count = 0;
121 	for_each_child_of_node_scoped(trips, trip) {
122 		ret = thermal_of_populate_trip(trip, &tt[count++]);
123 		if (ret)
124 			goto out_kfree;
125 	}
126 
127 	of_node_put(trips);
128 
129 	return tt;
130 
131 out_kfree:
132 	kfree(tt);
133 out_of_node_put:
134 	of_node_put(trips);
135 
136 	return ERR_PTR(ret);
137 }
138 
139 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)
140 {
141 	struct device_node *np, *tz;
142 	struct of_phandle_args sensor_specs;
143 
144 	np = of_find_node_by_name(NULL, "thermal-zones");
145 	if (!np) {
146 		pr_debug("No thermal zones description\n");
147 		return ERR_PTR(-ENODEV);
148 	}
149 
150 	/*
151 	 * Search for each thermal zone, a defined sensor
152 	 * corresponding to the one passed as parameter
153 	 */
154 	for_each_available_child_of_node_scoped(np, child) {
155 
156 		int count, i;
157 
158 		count = of_count_phandle_with_args(child, "thermal-sensors",
159 						   "#thermal-sensor-cells");
160 		if (count <= 0) {
161 			pr_err("%pOFn: missing thermal sensor\n", child);
162 			tz = ERR_PTR(-EINVAL);
163 			goto out;
164 		}
165 
166 		for (i = 0; i < count; i++) {
167 
168 			int ret;
169 
170 			ret = of_parse_phandle_with_args(child, "thermal-sensors",
171 							 "#thermal-sensor-cells",
172 							 i, &sensor_specs);
173 			if (ret < 0) {
174 				pr_err("%pOFn: Failed to read thermal-sensors cells: %d\n", child, ret);
175 				tz = ERR_PTR(ret);
176 				goto out;
177 			}
178 
179 			if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ?
180 								  sensor_specs.args[0] : 0)) {
181 				pr_debug("sensor %pOFn id=%d belongs to %pOFn\n", sensor, id, child);
182 				tz = no_free_ptr(child);
183 				goto out;
184 			}
185 		}
186 	}
187 	tz = ERR_PTR(-ENODEV);
188 out:
189 	of_node_put(np);
190 	return tz;
191 }
192 
193 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay)
194 {
195 	int ret;
196 
197 	ret = of_property_read_u32(np, "polling-delay-passive", pdelay);
198 	if (ret == -EINVAL) {
199 		*pdelay = 0;
200 	} else if (ret < 0) {
201 		pr_err("%pOFn: Couldn't get polling-delay-passive: %d\n", np, ret);
202 		return ret;
203 	}
204 
205 	ret = of_property_read_u32(np, "polling-delay", delay);
206 	if (ret == -EINVAL) {
207 		*delay = 0;
208 	} else if (ret < 0) {
209 		pr_err("%pOFn: Couldn't get polling-delay: %d\n", np, ret);
210 		return ret;
211 	}
212 
213 	return 0;
214 }
215 
216 static void thermal_of_parameters_init(struct device_node *np,
217 				       struct thermal_zone_params *tzp)
218 {
219 	int coef[2];
220 	int ncoef = ARRAY_SIZE(coef);
221 	int prop, ret;
222 
223 	tzp->no_hwmon = true;
224 
225 	if (!of_property_read_u32(np, "sustainable-power", &prop))
226 		tzp->sustainable_power = prop;
227 
228 	/*
229 	 * For now, the thermal framework supports only one sensor per
230 	 * thermal zone. Thus, we are considering only the first two
231 	 * values as slope and offset.
232 	 */
233 	ret = of_property_read_u32_array(np, "coefficients", coef, ncoef);
234 	if (ret) {
235 		coef[0] = 1;
236 		coef[1] = 0;
237 	}
238 
239 	tzp->slope = coef[0];
240 	tzp->offset = coef[1];
241 }
242 
243 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz)
244 {
245 	struct device_node *np, *tz_np;
246 
247 	np = of_find_node_by_name(NULL, "thermal-zones");
248 	if (!np)
249 		return ERR_PTR(-ENODEV);
250 
251 	tz_np = of_get_child_by_name(np, tz->type);
252 
253 	of_node_put(np);
254 
255 	if (!tz_np)
256 		return ERR_PTR(-ENODEV);
257 
258 	return tz_np;
259 }
260 
261 static bool thermal_of_get_cooling_spec(struct device_node *map_np, int index,
262 					struct thermal_cooling_device *cdev,
263 					struct cooling_spec *c)
264 {
265 	struct of_phandle_args cooling_spec;
266 	int ret, weight = THERMAL_WEIGHT_DEFAULT;
267 
268 	of_property_read_u32(map_np, "contribution", &weight);
269 
270 	ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
271 					 index, &cooling_spec);
272 
273 	if (ret < 0) {
274 		pr_err("Invalid cooling-device entry\n");
275 		return false;
276 	}
277 
278 	of_node_put(cooling_spec.np);
279 
280 	if (cooling_spec.args_count < 2) {
281 		pr_err("wrong reference to cooling device, missing limits\n");
282 		return false;
283 	}
284 
285 	if (cooling_spec.np != cdev->np)
286 		return false;
287 
288 	c->lower = cooling_spec.args[0];
289 	c->upper = cooling_spec.args[1];
290 	c->weight = weight;
291 
292 	return true;
293 }
294 
295 static bool thermal_of_should_bind(struct thermal_zone_device *tz,
296 				   const struct thermal_trip *trip,
297 				   struct thermal_cooling_device *cdev,
298 				   struct cooling_spec *c)
299 {
300 	struct device_node *tz_np, *cm_np, *child;
301 	bool result = false;
302 
303 	tz_np = thermal_of_zone_get_by_name(tz);
304 	if (IS_ERR(tz_np)) {
305 		pr_err("Failed to get node tz by name\n");
306 		return false;
307 	}
308 
309 	cm_np = of_get_child_by_name(tz_np, "cooling-maps");
310 	if (!cm_np)
311 		goto out;
312 
313 	/* Look up the trip and the cdev in the cooling maps. */
314 	for_each_child_of_node(cm_np, child) {
315 		struct device_node *tr_np;
316 		int count, i;
317 
318 		tr_np = of_parse_phandle(child, "trip", 0);
319 		if (tr_np != trip->priv)
320 			continue;
321 
322 		/* The trip has been found, look up the cdev. */
323 		count = of_count_phandle_with_args(child, "cooling-device", "#cooling-cells");
324 		if (count <= 0)
325 			pr_err("Add a cooling_device property with at least one device\n");
326 
327 		for (i = 0; i < count; i++) {
328 			result = thermal_of_get_cooling_spec(child, i, cdev, c);
329 			if (result)
330 				break;
331 		}
332 
333 		of_node_put(child);
334 		break;
335 	}
336 
337 	of_node_put(cm_np);
338 out:
339 	of_node_put(tz_np);
340 
341 	return result;
342 }
343 
344 /**
345  * thermal_of_zone_unregister - Cleanup the specific allocated ressources
346  *
347  * This function disables the thermal zone and frees the different
348  * ressources allocated specific to the thermal OF.
349  *
350  * @tz: a pointer to the thermal zone structure
351  */
352 static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
353 {
354 	thermal_zone_device_disable(tz);
355 	thermal_zone_device_unregister(tz);
356 }
357 
358 /**
359  * thermal_of_zone_register - Register a thermal zone with device node
360  * sensor
361  *
362  * The thermal_of_zone_register() parses a device tree given a device
363  * node sensor and identifier. It searches for the thermal zone
364  * associated to the couple sensor/id and retrieves all the thermal
365  * zone properties and registers new thermal zone with those
366  * properties.
367  *
368  * @sensor: A device node pointer corresponding to the sensor in the device tree
369  * @id: An integer as sensor identifier
370  * @data: A private data to be stored in the thermal zone dedicated private area
371  * @ops: A set of thermal sensor ops
372  *
373  * Return: a valid thermal zone structure pointer on success.
374  *	- EINVAL: if the device tree thermal description is malformed
375  *	- ENOMEM: if one structure can not be allocated
376  *	- Other negative errors are returned by the underlying called functions
377  */
378 static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
379 							    const struct thermal_zone_device_ops *ops)
380 {
381 	struct thermal_zone_device_ops of_ops = *ops;
382 	struct thermal_zone_device *tz;
383 	struct thermal_trip *trips;
384 	struct thermal_zone_params tzp = {};
385 	struct device_node *np;
386 	const char *action;
387 	int delay, pdelay;
388 	int ntrips;
389 	int ret;
390 
391 	np = of_thermal_zone_find(sensor, id);
392 	if (IS_ERR(np)) {
393 		if (PTR_ERR(np) != -ENODEV)
394 			pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
395 		return ERR_CAST(np);
396 	}
397 
398 	trips = thermal_of_trips_init(np, &ntrips);
399 	if (IS_ERR(trips)) {
400 		pr_err("Failed to parse trip points for %pOFn id=%d\n", sensor, id);
401 		ret = PTR_ERR(trips);
402 		goto out_of_node_put;
403 	}
404 
405 	if (!trips)
406 		pr_info("No trip points found for %pOFn id=%d\n", sensor, id);
407 
408 	ret = thermal_of_monitor_init(np, &delay, &pdelay);
409 	if (ret) {
410 		pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
411 		goto out_kfree_trips;
412 	}
413 
414 	thermal_of_parameters_init(np, &tzp);
415 
416 	of_ops.should_bind = thermal_of_should_bind;
417 
418 	ret = of_property_read_string(np, "critical-action", &action);
419 	if (!ret)
420 		if (!of_ops.critical && !strcasecmp(action, "reboot"))
421 			of_ops.critical = thermal_zone_device_critical_reboot;
422 
423 	tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
424 						     data, &of_ops, &tzp,
425 						     pdelay, delay);
426 	if (IS_ERR(tz)) {
427 		ret = PTR_ERR(tz);
428 		pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
429 		goto out_kfree_trips;
430 	}
431 
432 	of_node_put(np);
433 	kfree(trips);
434 
435 	ret = thermal_zone_device_enable(tz);
436 	if (ret) {
437 		pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
438 		       tz->type, tz->id, ret);
439 		thermal_of_zone_unregister(tz);
440 		return ERR_PTR(ret);
441 	}
442 
443 	return tz;
444 
445 out_kfree_trips:
446 	kfree(trips);
447 out_of_node_put:
448 	of_node_put(np);
449 
450 	return ERR_PTR(ret);
451 }
452 
453 static void devm_thermal_of_zone_release(struct device *dev, void *res)
454 {
455 	thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
456 }
457 
458 static int devm_thermal_of_zone_match(struct device *dev, void *res,
459 				      void *data)
460 {
461 	struct thermal_zone_device **r = res;
462 
463 	if (WARN_ON(!r || !*r))
464 		return 0;
465 
466 	return *r == data;
467 }
468 
469 /**
470  * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle
471  *
472  * This function is the device version of the thermal_of_zone_register() function.
473  *
474  * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle
475  * @sensor_id: the sensor identifier
476  * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field
477  * @ops: a pointer to the ops structure associated with the sensor
478  */
479 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data,
480 							  const struct thermal_zone_device_ops *ops)
481 {
482 	struct thermal_zone_device **ptr, *tzd;
483 
484 	ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr),
485 			   GFP_KERNEL);
486 	if (!ptr)
487 		return ERR_PTR(-ENOMEM);
488 
489 	tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops);
490 	if (IS_ERR(tzd)) {
491 		devres_free(ptr);
492 		return tzd;
493 	}
494 
495 	*ptr = tzd;
496 	devres_add(dev, ptr);
497 
498 	return tzd;
499 }
500 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register);
501 
502 /**
503  * devm_thermal_of_zone_unregister - Resource managed version of
504  *				thermal_of_zone_unregister().
505  * @dev: Device for which which resource was allocated.
506  * @tz: a pointer to struct thermal_zone where the sensor is registered.
507  *
508  * This function removes the sensor callbacks and private data from the
509  * thermal zone device registered with devm_thermal_zone_of_sensor_register()
510  * API. It will also silent the zone by remove the .get_temp() and .get_trend()
511  * thermal zone device callbacks.
512  * Normally this function will not need to be called and the resource
513  * management code will ensure that the resource is freed.
514  */
515 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz)
516 {
517 	WARN_ON(devres_release(dev, devm_thermal_of_zone_release,
518 			       devm_thermal_of_zone_match, tz));
519 }
520 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);
521