xref: /openbmc/linux/drivers/thermal/thermal_of.c (revision 91b0383f)
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 /***   Private data structures to represent thermal device tree data ***/
23 
24 /**
25  * struct __thermal_cooling_bind_param - a cooling device for a trip point
26  * @cooling_device: a pointer to identify the referred cooling device
27  * @min: minimum cooling state used at this trip point
28  * @max: maximum cooling state used at this trip point
29  */
30 
31 struct __thermal_cooling_bind_param {
32 	struct device_node *cooling_device;
33 	unsigned long min;
34 	unsigned long max;
35 };
36 
37 /**
38  * struct __thermal_bind_param - a match between trip and cooling device
39  * @tcbp: a pointer to an array of cooling devices
40  * @count: number of elements in array
41  * @trip_id: the trip point index
42  * @usage: the percentage (from 0 to 100) of cooling contribution
43  */
44 
45 struct __thermal_bind_params {
46 	struct __thermal_cooling_bind_param *tcbp;
47 	unsigned int count;
48 	unsigned int trip_id;
49 	unsigned int usage;
50 };
51 
52 /**
53  * struct __thermal_zone - internal representation of a thermal zone
54  * @passive_delay: polling interval while passive cooling is activated
55  * @polling_delay: zone polling interval
56  * @slope: slope of the temperature adjustment curve
57  * @offset: offset of the temperature adjustment curve
58  * @ntrips: number of trip points
59  * @trips: an array of trip points (0..ntrips - 1)
60  * @num_tbps: number of thermal bind params
61  * @tbps: an array of thermal bind params (0..num_tbps - 1)
62  * @sensor_data: sensor private data used while reading temperature and trend
63  * @ops: set of callbacks to handle the thermal zone based on DT
64  */
65 
66 struct __thermal_zone {
67 	int passive_delay;
68 	int polling_delay;
69 	int slope;
70 	int offset;
71 
72 	/* trip data */
73 	int ntrips;
74 	struct thermal_trip *trips;
75 
76 	/* cooling binding data */
77 	int num_tbps;
78 	struct __thermal_bind_params *tbps;
79 
80 	/* sensor interface */
81 	void *sensor_data;
82 	const struct thermal_zone_of_device_ops *ops;
83 };
84 
85 /***   DT thermal zone device callbacks   ***/
86 
87 static int of_thermal_get_temp(struct thermal_zone_device *tz,
88 			       int *temp)
89 {
90 	struct __thermal_zone *data = tz->devdata;
91 
92 	if (!data->ops || !data->ops->get_temp)
93 		return -EINVAL;
94 
95 	return data->ops->get_temp(data->sensor_data, temp);
96 }
97 
98 static int of_thermal_set_trips(struct thermal_zone_device *tz,
99 				int low, int high)
100 {
101 	struct __thermal_zone *data = tz->devdata;
102 
103 	if (!data->ops || !data->ops->set_trips)
104 		return -EINVAL;
105 
106 	return data->ops->set_trips(data->sensor_data, low, high);
107 }
108 
109 /**
110  * of_thermal_get_ntrips - function to export number of available trip
111  *			   points.
112  * @tz: pointer to a thermal zone
113  *
114  * This function is a globally visible wrapper to get number of trip points
115  * stored in the local struct __thermal_zone
116  *
117  * Return: number of available trip points, -ENODEV when data not available
118  */
119 int of_thermal_get_ntrips(struct thermal_zone_device *tz)
120 {
121 	struct __thermal_zone *data = tz->devdata;
122 
123 	if (!data || IS_ERR(data))
124 		return -ENODEV;
125 
126 	return data->ntrips;
127 }
128 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
129 
130 /**
131  * of_thermal_is_trip_valid - function to check if trip point is valid
132  *
133  * @tz:	pointer to a thermal zone
134  * @trip:	trip point to evaluate
135  *
136  * This function is responsible for checking if passed trip point is valid
137  *
138  * Return: true if trip point is valid, false otherwise
139  */
140 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
141 {
142 	struct __thermal_zone *data = tz->devdata;
143 
144 	if (!data || trip >= data->ntrips || trip < 0)
145 		return false;
146 
147 	return true;
148 }
149 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
150 
151 /**
152  * of_thermal_get_trip_points - function to get access to a globally exported
153  *				trip points
154  *
155  * @tz:	pointer to a thermal zone
156  *
157  * This function provides a pointer to trip points table
158  *
159  * Return: pointer to trip points table, NULL otherwise
160  */
161 const struct thermal_trip *
162 of_thermal_get_trip_points(struct thermal_zone_device *tz)
163 {
164 	struct __thermal_zone *data = tz->devdata;
165 
166 	if (!data)
167 		return NULL;
168 
169 	return data->trips;
170 }
171 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
172 
173 /**
174  * of_thermal_set_emul_temp - function to set emulated temperature
175  *
176  * @tz:	pointer to a thermal zone
177  * @temp:	temperature to set
178  *
179  * This function gives the ability to set emulated value of temperature,
180  * which is handy for debugging
181  *
182  * Return: zero on success, error code otherwise
183  */
184 static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
185 				    int temp)
186 {
187 	struct __thermal_zone *data = tz->devdata;
188 
189 	if (!data->ops || !data->ops->set_emul_temp)
190 		return -EINVAL;
191 
192 	return data->ops->set_emul_temp(data->sensor_data, temp);
193 }
194 
195 static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
196 				enum thermal_trend *trend)
197 {
198 	struct __thermal_zone *data = tz->devdata;
199 
200 	if (!data->ops || !data->ops->get_trend)
201 		return -EINVAL;
202 
203 	return data->ops->get_trend(data->sensor_data, trip, trend);
204 }
205 
206 static int of_thermal_bind(struct thermal_zone_device *thermal,
207 			   struct thermal_cooling_device *cdev)
208 {
209 	struct __thermal_zone *data = thermal->devdata;
210 	struct __thermal_bind_params *tbp;
211 	struct __thermal_cooling_bind_param *tcbp;
212 	int i, j;
213 
214 	if (!data || IS_ERR(data))
215 		return -ENODEV;
216 
217 	/* find where to bind */
218 	for (i = 0; i < data->num_tbps; i++) {
219 		tbp = data->tbps + i;
220 
221 		for (j = 0; j < tbp->count; j++) {
222 			tcbp = tbp->tcbp + j;
223 
224 			if (tcbp->cooling_device == cdev->np) {
225 				int ret;
226 
227 				ret = thermal_zone_bind_cooling_device(thermal,
228 						tbp->trip_id, cdev,
229 						tcbp->max,
230 						tcbp->min,
231 						tbp->usage);
232 				if (ret)
233 					return ret;
234 			}
235 		}
236 	}
237 
238 	return 0;
239 }
240 
241 static int of_thermal_unbind(struct thermal_zone_device *thermal,
242 			     struct thermal_cooling_device *cdev)
243 {
244 	struct __thermal_zone *data = thermal->devdata;
245 	struct __thermal_bind_params *tbp;
246 	struct __thermal_cooling_bind_param *tcbp;
247 	int i, j;
248 
249 	if (!data || IS_ERR(data))
250 		return -ENODEV;
251 
252 	/* find where to unbind */
253 	for (i = 0; i < data->num_tbps; i++) {
254 		tbp = data->tbps + i;
255 
256 		for (j = 0; j < tbp->count; j++) {
257 			tcbp = tbp->tcbp + j;
258 
259 			if (tcbp->cooling_device == cdev->np) {
260 				int ret;
261 
262 				ret = thermal_zone_unbind_cooling_device(thermal,
263 							tbp->trip_id, cdev);
264 				if (ret)
265 					return ret;
266 			}
267 		}
268 	}
269 
270 	return 0;
271 }
272 
273 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
274 				    enum thermal_trip_type *type)
275 {
276 	struct __thermal_zone *data = tz->devdata;
277 
278 	if (trip >= data->ntrips || trip < 0)
279 		return -EDOM;
280 
281 	*type = data->trips[trip].type;
282 
283 	return 0;
284 }
285 
286 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
287 				    int *temp)
288 {
289 	struct __thermal_zone *data = tz->devdata;
290 
291 	if (trip >= data->ntrips || trip < 0)
292 		return -EDOM;
293 
294 	*temp = data->trips[trip].temperature;
295 
296 	return 0;
297 }
298 
299 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
300 				    int temp)
301 {
302 	struct __thermal_zone *data = tz->devdata;
303 
304 	if (trip >= data->ntrips || trip < 0)
305 		return -EDOM;
306 
307 	if (data->ops && data->ops->set_trip_temp) {
308 		int ret;
309 
310 		ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
311 		if (ret)
312 			return ret;
313 	}
314 
315 	/* thermal framework should take care of data->mask & (1 << trip) */
316 	data->trips[trip].temperature = temp;
317 
318 	return 0;
319 }
320 
321 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
322 				    int *hyst)
323 {
324 	struct __thermal_zone *data = tz->devdata;
325 
326 	if (trip >= data->ntrips || trip < 0)
327 		return -EDOM;
328 
329 	*hyst = data->trips[trip].hysteresis;
330 
331 	return 0;
332 }
333 
334 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
335 				    int hyst)
336 {
337 	struct __thermal_zone *data = tz->devdata;
338 
339 	if (trip >= data->ntrips || trip < 0)
340 		return -EDOM;
341 
342 	/* thermal framework should take care of data->mask & (1 << trip) */
343 	data->trips[trip].hysteresis = hyst;
344 
345 	return 0;
346 }
347 
348 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
349 				    int *temp)
350 {
351 	struct __thermal_zone *data = tz->devdata;
352 	int i;
353 
354 	for (i = 0; i < data->ntrips; i++)
355 		if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
356 			*temp = data->trips[i].temperature;
357 			return 0;
358 		}
359 
360 	return -EINVAL;
361 }
362 
363 static struct thermal_zone_device_ops of_thermal_ops = {
364 	.get_trip_type = of_thermal_get_trip_type,
365 	.get_trip_temp = of_thermal_get_trip_temp,
366 	.set_trip_temp = of_thermal_set_trip_temp,
367 	.get_trip_hyst = of_thermal_get_trip_hyst,
368 	.set_trip_hyst = of_thermal_set_trip_hyst,
369 	.get_crit_temp = of_thermal_get_crit_temp,
370 
371 	.bind = of_thermal_bind,
372 	.unbind = of_thermal_unbind,
373 };
374 
375 /***   sensor API   ***/
376 
377 static struct thermal_zone_device *
378 thermal_zone_of_add_sensor(struct device_node *zone,
379 			   struct device_node *sensor, void *data,
380 			   const struct thermal_zone_of_device_ops *ops)
381 {
382 	struct thermal_zone_device *tzd;
383 	struct __thermal_zone *tz;
384 
385 	tzd = thermal_zone_get_zone_by_name(zone->name);
386 	if (IS_ERR(tzd))
387 		return ERR_PTR(-EPROBE_DEFER);
388 
389 	tz = tzd->devdata;
390 
391 	if (!ops)
392 		return ERR_PTR(-EINVAL);
393 
394 	mutex_lock(&tzd->lock);
395 	tz->ops = ops;
396 	tz->sensor_data = data;
397 
398 	tzd->ops->get_temp = of_thermal_get_temp;
399 	tzd->ops->get_trend = of_thermal_get_trend;
400 
401 	/*
402 	 * The thermal zone core will calculate the window if they have set the
403 	 * optional set_trips pointer.
404 	 */
405 	if (ops->set_trips)
406 		tzd->ops->set_trips = of_thermal_set_trips;
407 
408 	if (ops->set_emul_temp)
409 		tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
410 
411 	mutex_unlock(&tzd->lock);
412 
413 	return tzd;
414 }
415 
416 /**
417  * thermal_zone_of_get_sensor_id - get sensor ID from a DT thermal zone
418  * @tz_np: a valid thermal zone device node.
419  * @sensor_np: a sensor node of a valid sensor device.
420  * @id: the sensor ID returned if success.
421  *
422  * This function will get sensor ID from a given thermal zone node and
423  * the sensor node must match the temperature provider @sensor_np.
424  *
425  * Return: 0 on success, proper error code otherwise.
426  */
427 
428 int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
429 				  struct device_node *sensor_np,
430 				  u32 *id)
431 {
432 	struct of_phandle_args sensor_specs;
433 	int ret;
434 
435 	ret = of_parse_phandle_with_args(tz_np,
436 					 "thermal-sensors",
437 					 "#thermal-sensor-cells",
438 					 0,
439 					 &sensor_specs);
440 	if (ret)
441 		return ret;
442 
443 	if (sensor_specs.np != sensor_np) {
444 		of_node_put(sensor_specs.np);
445 		return -ENODEV;
446 	}
447 
448 	if (sensor_specs.args_count > 1)
449 		pr_warn("%pOFn: too many cells in sensor specifier %d\n",
450 		     sensor_specs.np, sensor_specs.args_count);
451 
452 	*id = sensor_specs.args_count ? sensor_specs.args[0] : 0;
453 
454 	of_node_put(sensor_specs.np);
455 
456 	return 0;
457 }
458 EXPORT_SYMBOL_GPL(thermal_zone_of_get_sensor_id);
459 
460 /**
461  * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
462  * @dev: a valid struct device pointer of a sensor device. Must contain
463  *       a valid .of_node, for the sensor node.
464  * @sensor_id: a sensor identifier, in case the sensor IP has more
465  *             than one sensors
466  * @data: a private pointer (owned by the caller) that will be passed
467  *        back, when a temperature reading is needed.
468  * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
469  *
470  * This function will search the list of thermal zones described in device
471  * tree and look for the zone that refer to the sensor device pointed by
472  * @dev->of_node as temperature providers. For the zone pointing to the
473  * sensor node, the sensor will be added to the DT thermal zone device.
474  *
475  * The thermal zone temperature is provided by the @get_temp function
476  * pointer. When called, it will have the private pointer @data back.
477  *
478  * The thermal zone temperature trend is provided by the @get_trend function
479  * pointer. When called, it will have the private pointer @data back.
480  *
481  * TODO:
482  * 01 - This function must enqueue the new sensor instead of using
483  * it as the only source of temperature values.
484  *
485  * 02 - There must be a way to match the sensor with all thermal zones
486  * that refer to it.
487  *
488  * Return: On success returns a valid struct thermal_zone_device,
489  * otherwise, it returns a corresponding ERR_PTR(). Caller must
490  * check the return value with help of IS_ERR() helper.
491  */
492 struct thermal_zone_device *
493 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
494 				const struct thermal_zone_of_device_ops *ops)
495 {
496 	struct device_node *np, *child, *sensor_np;
497 	struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
498 
499 	np = of_find_node_by_name(NULL, "thermal-zones");
500 	if (!np)
501 		return ERR_PTR(-ENODEV);
502 
503 	if (!dev || !dev->of_node) {
504 		of_node_put(np);
505 		return ERR_PTR(-ENODEV);
506 	}
507 
508 	sensor_np = of_node_get(dev->of_node);
509 
510 	for_each_available_child_of_node(np, child) {
511 		int ret, id;
512 
513 		/* For now, thermal framework supports only 1 sensor per zone */
514 		ret = thermal_zone_of_get_sensor_id(child, sensor_np, &id);
515 		if (ret)
516 			continue;
517 
518 		if (id == sensor_id) {
519 			tzd = thermal_zone_of_add_sensor(child, sensor_np,
520 							 data, ops);
521 			if (!IS_ERR(tzd))
522 				thermal_zone_device_enable(tzd);
523 
524 			of_node_put(child);
525 			goto exit;
526 		}
527 	}
528 exit:
529 	of_node_put(sensor_np);
530 	of_node_put(np);
531 
532 	return tzd;
533 }
534 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
535 
536 /**
537  * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
538  * @dev: a valid struct device pointer of a sensor device. Must contain
539  *       a valid .of_node, for the sensor node.
540  * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
541  *
542  * This function removes the sensor callbacks and private data from the
543  * thermal zone device registered with thermal_zone_of_sensor_register()
544  * API. It will also silent the zone by remove the .get_temp() and .get_trend()
545  * thermal zone device callbacks.
546  *
547  * TODO: When the support to several sensors per zone is added, this
548  * function must search the sensor list based on @dev parameter.
549  *
550  */
551 void thermal_zone_of_sensor_unregister(struct device *dev,
552 				       struct thermal_zone_device *tzd)
553 {
554 	struct __thermal_zone *tz;
555 
556 	if (!dev || !tzd || !tzd->devdata)
557 		return;
558 
559 	tz = tzd->devdata;
560 
561 	/* no __thermal_zone, nothing to be done */
562 	if (!tz)
563 		return;
564 
565 	/* stop temperature polling */
566 	thermal_zone_device_disable(tzd);
567 
568 	mutex_lock(&tzd->lock);
569 	tzd->ops->get_temp = NULL;
570 	tzd->ops->get_trend = NULL;
571 	tzd->ops->set_emul_temp = NULL;
572 
573 	tz->ops = NULL;
574 	tz->sensor_data = NULL;
575 	mutex_unlock(&tzd->lock);
576 }
577 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
578 
579 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
580 {
581 	thermal_zone_of_sensor_unregister(dev,
582 					  *(struct thermal_zone_device **)res);
583 }
584 
585 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
586 					     void *data)
587 {
588 	struct thermal_zone_device **r = res;
589 
590 	if (WARN_ON(!r || !*r))
591 		return 0;
592 
593 	return *r == data;
594 }
595 
596 /**
597  * devm_thermal_zone_of_sensor_register - Resource managed version of
598  *				thermal_zone_of_sensor_register()
599  * @dev: a valid struct device pointer of a sensor device. Must contain
600  *       a valid .of_node, for the sensor node.
601  * @sensor_id: a sensor identifier, in case the sensor IP has more
602  *	       than one sensors
603  * @data: a private pointer (owned by the caller) that will be passed
604  *	  back, when a temperature reading is needed.
605  * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
606  *
607  * Refer thermal_zone_of_sensor_register() for more details.
608  *
609  * Return: On success returns a valid struct thermal_zone_device,
610  * otherwise, it returns a corresponding ERR_PTR(). Caller must
611  * check the return value with help of IS_ERR() helper.
612  * Registered thermal_zone_device device will automatically be
613  * released when device is unbounded.
614  */
615 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
616 	struct device *dev, int sensor_id,
617 	void *data, const struct thermal_zone_of_device_ops *ops)
618 {
619 	struct thermal_zone_device **ptr, *tzd;
620 
621 	ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
622 			   GFP_KERNEL);
623 	if (!ptr)
624 		return ERR_PTR(-ENOMEM);
625 
626 	tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
627 	if (IS_ERR(tzd)) {
628 		devres_free(ptr);
629 		return tzd;
630 	}
631 
632 	*ptr = tzd;
633 	devres_add(dev, ptr);
634 
635 	return tzd;
636 }
637 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
638 
639 /**
640  * devm_thermal_zone_of_sensor_unregister - Resource managed version of
641  *				thermal_zone_of_sensor_unregister().
642  * @dev: Device for which which resource was allocated.
643  * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
644  *
645  * This function removes the sensor callbacks and private data from the
646  * thermal zone device registered with devm_thermal_zone_of_sensor_register()
647  * API. It will also silent the zone by remove the .get_temp() and .get_trend()
648  * thermal zone device callbacks.
649  * Normally this function will not need to be called and the resource
650  * management code will ensure that the resource is freed.
651  */
652 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
653 					    struct thermal_zone_device *tzd)
654 {
655 	WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
656 			       devm_thermal_zone_of_sensor_match, tzd));
657 }
658 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
659 
660 /***   functions parsing device tree nodes   ***/
661 
662 /**
663  * thermal_of_populate_bind_params - parse and fill cooling map data
664  * @np: DT node containing a cooling-map node
665  * @__tbp: data structure to be filled with cooling map info
666  * @trips: array of thermal zone trip points
667  * @ntrips: number of trip points inside trips.
668  *
669  * This function parses a cooling-map type of node represented by
670  * @np parameter and fills the read data into @__tbp data structure.
671  * It needs the already parsed array of trip points of the thermal zone
672  * in consideration.
673  *
674  * Return: 0 on success, proper error code otherwise
675  */
676 static int thermal_of_populate_bind_params(struct device_node *np,
677 					   struct __thermal_bind_params *__tbp,
678 					   struct thermal_trip *trips,
679 					   int ntrips)
680 {
681 	struct of_phandle_args cooling_spec;
682 	struct __thermal_cooling_bind_param *__tcbp;
683 	struct device_node *trip;
684 	int ret, i, count;
685 	u32 prop;
686 
687 	/* Default weight. Usage is optional */
688 	__tbp->usage = THERMAL_WEIGHT_DEFAULT;
689 	ret = of_property_read_u32(np, "contribution", &prop);
690 	if (ret == 0)
691 		__tbp->usage = prop;
692 
693 	trip = of_parse_phandle(np, "trip", 0);
694 	if (!trip) {
695 		pr_err("missing trip property\n");
696 		return -ENODEV;
697 	}
698 
699 	/* match using device_node */
700 	for (i = 0; i < ntrips; i++)
701 		if (trip == trips[i].np) {
702 			__tbp->trip_id = i;
703 			break;
704 		}
705 
706 	if (i == ntrips) {
707 		ret = -ENODEV;
708 		goto end;
709 	}
710 
711 	count = of_count_phandle_with_args(np, "cooling-device",
712 					   "#cooling-cells");
713 	if (count <= 0) {
714 		pr_err("Add a cooling_device property with at least one device\n");
715 		ret = -ENOENT;
716 		goto end;
717 	}
718 
719 	__tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL);
720 	if (!__tcbp) {
721 		ret = -ENOMEM;
722 		goto end;
723 	}
724 
725 	for (i = 0; i < count; i++) {
726 		ret = of_parse_phandle_with_args(np, "cooling-device",
727 				"#cooling-cells", i, &cooling_spec);
728 		if (ret < 0) {
729 			pr_err("Invalid cooling-device entry\n");
730 			goto free_tcbp;
731 		}
732 
733 		__tcbp[i].cooling_device = cooling_spec.np;
734 
735 		if (cooling_spec.args_count >= 2) { /* at least min and max */
736 			__tcbp[i].min = cooling_spec.args[0];
737 			__tcbp[i].max = cooling_spec.args[1];
738 		} else {
739 			pr_err("wrong reference to cooling device, missing limits\n");
740 		}
741 	}
742 
743 	__tbp->tcbp = __tcbp;
744 	__tbp->count = count;
745 
746 	goto end;
747 
748 free_tcbp:
749 	for (i = i - 1; i >= 0; i--)
750 		of_node_put(__tcbp[i].cooling_device);
751 	kfree(__tcbp);
752 end:
753 	of_node_put(trip);
754 
755 	return ret;
756 }
757 
758 /*
759  * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
760  * into the device tree binding of 'trip', property type.
761  */
762 static const char * const trip_types[] = {
763 	[THERMAL_TRIP_ACTIVE]	= "active",
764 	[THERMAL_TRIP_PASSIVE]	= "passive",
765 	[THERMAL_TRIP_HOT]	= "hot",
766 	[THERMAL_TRIP_CRITICAL]	= "critical",
767 };
768 
769 /**
770  * thermal_of_get_trip_type - Get phy mode for given device_node
771  * @np:	Pointer to the given device_node
772  * @type: Pointer to resulting trip type
773  *
774  * The function gets trip type string from property 'type',
775  * and store its index in trip_types table in @type,
776  *
777  * Return: 0 on success, or errno in error case.
778  */
779 static int thermal_of_get_trip_type(struct device_node *np,
780 				    enum thermal_trip_type *type)
781 {
782 	const char *t;
783 	int err, i;
784 
785 	err = of_property_read_string(np, "type", &t);
786 	if (err < 0)
787 		return err;
788 
789 	for (i = 0; i < ARRAY_SIZE(trip_types); i++)
790 		if (!strcasecmp(t, trip_types[i])) {
791 			*type = i;
792 			return 0;
793 		}
794 
795 	return -ENODEV;
796 }
797 
798 /**
799  * thermal_of_populate_trip - parse and fill one trip point data
800  * @np: DT node containing a trip point node
801  * @trip: trip point data structure to be filled up
802  *
803  * This function parses a trip point type of node represented by
804  * @np parameter and fills the read data into @trip data structure.
805  *
806  * Return: 0 on success, proper error code otherwise
807  */
808 static int thermal_of_populate_trip(struct device_node *np,
809 				    struct thermal_trip *trip)
810 {
811 	int prop;
812 	int ret;
813 
814 	ret = of_property_read_u32(np, "temperature", &prop);
815 	if (ret < 0) {
816 		pr_err("missing temperature property\n");
817 		return ret;
818 	}
819 	trip->temperature = prop;
820 
821 	ret = of_property_read_u32(np, "hysteresis", &prop);
822 	if (ret < 0) {
823 		pr_err("missing hysteresis property\n");
824 		return ret;
825 	}
826 	trip->hysteresis = prop;
827 
828 	ret = thermal_of_get_trip_type(np, &trip->type);
829 	if (ret < 0) {
830 		pr_err("wrong trip type property\n");
831 		return ret;
832 	}
833 
834 	/* Required for cooling map matching */
835 	trip->np = np;
836 	of_node_get(np);
837 
838 	return 0;
839 }
840 
841 /**
842  * thermal_of_build_thermal_zone - parse and fill one thermal zone data
843  * @np: DT node containing a thermal zone node
844  *
845  * This function parses a thermal zone type of node represented by
846  * @np parameter and fills the read data into a __thermal_zone data structure
847  * and return this pointer.
848  *
849  * TODO: Missing properties to parse: thermal-sensor-names
850  *
851  * Return: On success returns a valid struct __thermal_zone,
852  * otherwise, it returns a corresponding ERR_PTR(). Caller must
853  * check the return value with help of IS_ERR() helper.
854  */
855 static struct __thermal_zone
856 __init *thermal_of_build_thermal_zone(struct device_node *np)
857 {
858 	struct device_node *child = NULL, *gchild;
859 	struct __thermal_zone *tz;
860 	int ret, i;
861 	u32 prop, coef[2];
862 
863 	if (!np) {
864 		pr_err("no thermal zone np\n");
865 		return ERR_PTR(-EINVAL);
866 	}
867 
868 	tz = kzalloc(sizeof(*tz), GFP_KERNEL);
869 	if (!tz)
870 		return ERR_PTR(-ENOMEM);
871 
872 	ret = of_property_read_u32(np, "polling-delay-passive", &prop);
873 	if (ret < 0) {
874 		pr_err("%pOFn: missing polling-delay-passive property\n", np);
875 		goto free_tz;
876 	}
877 	tz->passive_delay = prop;
878 
879 	ret = of_property_read_u32(np, "polling-delay", &prop);
880 	if (ret < 0) {
881 		pr_err("%pOFn: missing polling-delay property\n", np);
882 		goto free_tz;
883 	}
884 	tz->polling_delay = prop;
885 
886 	/*
887 	 * REVIST: for now, the thermal framework supports only
888 	 * one sensor per thermal zone. Thus, we are considering
889 	 * only the first two values as slope and offset.
890 	 */
891 	ret = of_property_read_u32_array(np, "coefficients", coef, 2);
892 	if (ret == 0) {
893 		tz->slope = coef[0];
894 		tz->offset = coef[1];
895 	} else {
896 		tz->slope = 1;
897 		tz->offset = 0;
898 	}
899 
900 	/* trips */
901 	child = of_get_child_by_name(np, "trips");
902 
903 	/* No trips provided */
904 	if (!child)
905 		goto finish;
906 
907 	tz->ntrips = of_get_child_count(child);
908 	if (tz->ntrips == 0) /* must have at least one child */
909 		goto finish;
910 
911 	tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL);
912 	if (!tz->trips) {
913 		ret = -ENOMEM;
914 		goto free_tz;
915 	}
916 
917 	i = 0;
918 	for_each_child_of_node(child, gchild) {
919 		ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
920 		if (ret)
921 			goto free_trips;
922 	}
923 
924 	of_node_put(child);
925 
926 	/* cooling-maps */
927 	child = of_get_child_by_name(np, "cooling-maps");
928 
929 	/* cooling-maps not provided */
930 	if (!child)
931 		goto finish;
932 
933 	tz->num_tbps = of_get_child_count(child);
934 	if (tz->num_tbps == 0)
935 		goto finish;
936 
937 	tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL);
938 	if (!tz->tbps) {
939 		ret = -ENOMEM;
940 		goto free_trips;
941 	}
942 
943 	i = 0;
944 	for_each_child_of_node(child, gchild) {
945 		ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
946 						      tz->trips, tz->ntrips);
947 		if (ret)
948 			goto free_tbps;
949 	}
950 
951 finish:
952 	of_node_put(child);
953 
954 	return tz;
955 
956 free_tbps:
957 	for (i = i - 1; i >= 0; i--) {
958 		struct __thermal_bind_params *tbp = tz->tbps + i;
959 		int j;
960 
961 		for (j = 0; j < tbp->count; j++)
962 			of_node_put(tbp->tcbp[j].cooling_device);
963 
964 		kfree(tbp->tcbp);
965 	}
966 
967 	kfree(tz->tbps);
968 free_trips:
969 	for (i = 0; i < tz->ntrips; i++)
970 		of_node_put(tz->trips[i].np);
971 	kfree(tz->trips);
972 	of_node_put(gchild);
973 free_tz:
974 	kfree(tz);
975 	of_node_put(child);
976 
977 	return ERR_PTR(ret);
978 }
979 
980 static __init void of_thermal_free_zone(struct __thermal_zone *tz)
981 {
982 	struct __thermal_bind_params *tbp;
983 	int i, j;
984 
985 	for (i = 0; i < tz->num_tbps; i++) {
986 		tbp = tz->tbps + i;
987 
988 		for (j = 0; j < tbp->count; j++)
989 			of_node_put(tbp->tcbp[j].cooling_device);
990 
991 		kfree(tbp->tcbp);
992 	}
993 
994 	kfree(tz->tbps);
995 	for (i = 0; i < tz->ntrips; i++)
996 		of_node_put(tz->trips[i].np);
997 	kfree(tz->trips);
998 	kfree(tz);
999 }
1000 
1001 /**
1002  * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1003  *
1004  * Finds all zones parsed and added to the thermal framework and remove them
1005  * from the system, together with their resources.
1006  *
1007  */
1008 static __init void of_thermal_destroy_zones(void)
1009 {
1010 	struct device_node *np, *child;
1011 
1012 	np = of_find_node_by_name(NULL, "thermal-zones");
1013 	if (!np) {
1014 		pr_debug("unable to find thermal zones\n");
1015 		return;
1016 	}
1017 
1018 	for_each_available_child_of_node(np, child) {
1019 		struct thermal_zone_device *zone;
1020 
1021 		zone = thermal_zone_get_zone_by_name(child->name);
1022 		if (IS_ERR(zone))
1023 			continue;
1024 
1025 		thermal_zone_device_unregister(zone);
1026 		kfree(zone->tzp);
1027 		kfree(zone->ops);
1028 		of_thermal_free_zone(zone->devdata);
1029 	}
1030 	of_node_put(np);
1031 }
1032 
1033 /**
1034  * of_parse_thermal_zones - parse device tree thermal data
1035  *
1036  * Initialization function that can be called by machine initialization
1037  * code to parse thermal data and populate the thermal framework
1038  * with hardware thermal zones info. This function only parses thermal zones.
1039  * Cooling devices and sensor devices nodes are supposed to be parsed
1040  * by their respective drivers.
1041  *
1042  * Return: 0 on success, proper error code otherwise
1043  *
1044  */
1045 int __init of_parse_thermal_zones(void)
1046 {
1047 	struct device_node *np, *child;
1048 	struct __thermal_zone *tz;
1049 	struct thermal_zone_device_ops *ops;
1050 
1051 	np = of_find_node_by_name(NULL, "thermal-zones");
1052 	if (!np) {
1053 		pr_debug("unable to find thermal zones\n");
1054 		return 0; /* Run successfully on systems without thermal DT */
1055 	}
1056 
1057 	for_each_available_child_of_node(np, child) {
1058 		struct thermal_zone_device *zone;
1059 		struct thermal_zone_params *tzp;
1060 		int i, mask = 0;
1061 		u32 prop;
1062 
1063 		tz = thermal_of_build_thermal_zone(child);
1064 		if (IS_ERR(tz)) {
1065 			pr_err("failed to build thermal zone %pOFn: %ld\n",
1066 			       child,
1067 			       PTR_ERR(tz));
1068 			continue;
1069 		}
1070 
1071 		ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1072 		if (!ops)
1073 			goto exit_free;
1074 
1075 		tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1076 		if (!tzp) {
1077 			kfree(ops);
1078 			goto exit_free;
1079 		}
1080 
1081 		/* No hwmon because there might be hwmon drivers registering */
1082 		tzp->no_hwmon = true;
1083 
1084 		if (!of_property_read_u32(child, "sustainable-power", &prop))
1085 			tzp->sustainable_power = prop;
1086 
1087 		for (i = 0; i < tz->ntrips; i++)
1088 			mask |= 1 << i;
1089 
1090 		/* these two are left for temperature drivers to use */
1091 		tzp->slope = tz->slope;
1092 		tzp->offset = tz->offset;
1093 
1094 		zone = thermal_zone_device_register(child->name, tz->ntrips,
1095 						    mask, tz,
1096 						    ops, tzp,
1097 						    tz->passive_delay,
1098 						    tz->polling_delay);
1099 		if (IS_ERR(zone)) {
1100 			pr_err("Failed to build %pOFn zone %ld\n", child,
1101 			       PTR_ERR(zone));
1102 			kfree(tzp);
1103 			kfree(ops);
1104 			of_thermal_free_zone(tz);
1105 			/* attempting to build remaining zones still */
1106 		}
1107 	}
1108 	of_node_put(np);
1109 
1110 	return 0;
1111 
1112 exit_free:
1113 	of_node_put(child);
1114 	of_node_put(np);
1115 	of_thermal_free_zone(tz);
1116 
1117 	/* no memory available, so free what we have built */
1118 	of_thermal_destroy_zones();
1119 
1120 	return -ENOMEM;
1121 }
1122