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