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