Linux Audio

Check our new training course

Linux BSP development engineering services

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