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