Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  thermal_helpers.c - helper functions to handle thermal devices
  4 *
  5 *  Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
  6 *
  7 *  Highly based on original thermal_core.c
  8 *  Copyright (C) 2008 Intel Corp
  9 *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
 10 *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
 11 */
 12
 13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 14
 15#include <linux/device.h>
 16#include <linux/err.h>
 17#include <linux/export.h>
 18#include <linux/slab.h>
 19#include <linux/string.h>
 20#include <linux/sysfs.h>
 21
 22#include <trace/events/thermal.h>
 23
 24#include "thermal_core.h"
 
 25
 26int get_tz_trend(struct thermal_zone_device *tz, int trip)
 27{
 28	enum thermal_trend trend;
 29
 30	if (tz->emul_temperature || !tz->ops->get_trend ||
 31	    tz->ops->get_trend(tz, trip, &trend)) {
 32		if (tz->temperature > tz->last_temperature)
 33			trend = THERMAL_TREND_RAISING;
 34		else if (tz->temperature < tz->last_temperature)
 35			trend = THERMAL_TREND_DROPPING;
 36		else
 37			trend = THERMAL_TREND_STABLE;
 38	}
 39
 40	return trend;
 41}
 42EXPORT_SYMBOL(get_tz_trend);
 43
 44struct thermal_instance *
 45get_thermal_instance(struct thermal_zone_device *tz,
 46		     struct thermal_cooling_device *cdev, int trip)
 47{
 48	struct thermal_instance *pos = NULL;
 49	struct thermal_instance *target_instance = NULL;
 
 50
 51	mutex_lock(&tz->lock);
 52	mutex_lock(&cdev->lock);
 53
 
 
 54	list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
 55		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
 56			target_instance = pos;
 57			break;
 58		}
 59	}
 60
 61	mutex_unlock(&cdev->lock);
 62	mutex_unlock(&tz->lock);
 63
 64	return target_instance;
 65}
 66EXPORT_SYMBOL(get_thermal_instance);
 67
 68/**
 69 * thermal_zone_get_temp() - returns the temperature of a thermal zone
 70 * @tz: a valid pointer to a struct thermal_zone_device
 71 * @temp: a valid pointer to where to store the resulting temperature.
 72 *
 73 * When a valid thermal zone reference is passed, it will fetch its
 74 * temperature and fill @temp.
 75 *
 
 
 
 
 76 * Return: On success returns 0, an error code otherwise
 77 */
 78int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
 79{
 80	int ret = -EINVAL;
 81	int count;
 82	int crit_temp = INT_MAX;
 83	enum thermal_trip_type type;
 84
 85	if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
 86		goto exit;
 87
 88	mutex_lock(&tz->lock);
 89
 90	ret = tz->ops->get_temp(tz, temp);
 91
 92	if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
 93		for (count = 0; count < tz->trips; count++) {
 94			ret = tz->ops->get_trip_type(tz, count, &type);
 95			if (!ret && type == THERMAL_TRIP_CRITICAL) {
 96				ret = tz->ops->get_trip_temp(tz, count,
 97						&crit_temp);
 98				break;
 99			}
100		}
101
102		/*
103		 * Only allow emulating a temperature when the real temperature
104		 * is below the critical temperature so that the emulation code
105		 * cannot hide critical conditions.
106		 */
107		if (!ret && *temp < crit_temp)
108			*temp = tz->emul_temperature;
109	}
110
111	mutex_unlock(&tz->lock);
112exit:
 
113	return ret;
114}
115EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
116
117/**
118 * thermal_zone_set_trips - Computes the next trip points for the driver
119 * @tz: a pointer to a thermal zone device structure
 
120 *
121 * The function computes the next temperature boundaries by browsing
122 * the trip points. The result is the closer low and high trip points
123 * to the current temperature. These values are passed to the backend
124 * driver to let it set its own notification mechanism (usually an
125 * interrupt).
126 *
127 * It does not return a value
128 */
129void thermal_zone_set_trips(struct thermal_zone_device *tz)
130{
131	int low = -INT_MAX;
132	int high = INT_MAX;
133	int trip_temp, hysteresis;
134	int i, ret;
135
136	mutex_lock(&tz->lock);
137
138	if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
139		goto exit;
140
141	for (i = 0; i < tz->trips; i++) {
142		int trip_low;
143
144		tz->ops->get_trip_temp(tz, i, &trip_temp);
145		tz->ops->get_trip_hyst(tz, i, &hysteresis);
146
147		trip_low = trip_temp - hysteresis;
148
149		if (trip_low < tz->temperature && trip_low > low)
150			low = trip_low;
151
152		if (trip_temp > tz->temperature && trip_temp < high)
153			high = trip_temp;
 
154	}
155
156	/* No need to change trip points */
157	if (tz->prev_low_trip == low && tz->prev_high_trip == high)
158		goto exit;
159
160	tz->prev_low_trip = low;
161	tz->prev_high_trip = high;
162
163	dev_dbg(&tz->device,
164		"new temperature boundaries: %d < x < %d\n", low, high);
 
 
 
 
 
165
166	/*
167	 * Set a temperature window. When this window is left the driver
168	 * must inform the thermal core via thermal_zone_device_update.
169	 */
170	ret = tz->ops->set_trips(tz, low, high);
171	if (ret)
172		dev_err(&tz->device, "Failed to set trips: %d\n", ret);
173
174exit:
175	mutex_unlock(&tz->lock);
176}
177
178static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
179				       int target)
180{
181	if (cdev->ops->set_cur_state(cdev, target))
182		return;
183
184	thermal_notify_cdev_state_update(cdev->id, target);
185	thermal_cooling_device_stats_update(cdev, target);
186}
187
188void thermal_cdev_update(struct thermal_cooling_device *cdev)
189{
190	struct thermal_instance *instance;
191	unsigned long target = 0;
192
193	mutex_lock(&cdev->lock);
194	/* cooling device is updated*/
195	if (cdev->updated) {
196		mutex_unlock(&cdev->lock);
197		return;
198	}
199
200	/* Make sure cdev enters the deepest cooling state */
201	list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
202		dev_dbg(&cdev->device, "zone%d->target=%lu\n",
203			instance->tz->id, instance->target);
204		if (instance->target == THERMAL_NO_TARGET)
205			continue;
206		if (instance->target > target)
207			target = instance->target;
208	}
209
210	thermal_cdev_set_cur_state(cdev, target);
211
212	cdev->updated = true;
213	mutex_unlock(&cdev->lock);
214	trace_cdev_update(cdev, target);
215	dev_dbg(&cdev->device, "set to state %lu\n", target);
216}
217EXPORT_SYMBOL(thermal_cdev_update);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
219/**
220 * thermal_zone_get_slope - return the slope attribute of the thermal zone
221 * @tz: thermal zone device with the slope attribute
222 *
223 * Return: If the thermal zone device has a slope attribute, return it, else
224 * return 1.
225 */
226int thermal_zone_get_slope(struct thermal_zone_device *tz)
227{
228	if (tz && tz->tzp)
229		return tz->tzp->slope;
230	return 1;
231}
232EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
233
234/**
235 * thermal_zone_get_offset - return the offset attribute of the thermal zone
236 * @tz: thermal zone device with the offset attribute
237 *
238 * Return: If the thermal zone device has a offset attribute, return it, else
239 * return 0.
240 */
241int thermal_zone_get_offset(struct thermal_zone_device *tz)
242{
243	if (tz && tz->tzp)
244		return tz->tzp->offset;
245	return 0;
246}
247EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  thermal_helpers.c - helper functions to handle thermal devices
  4 *
  5 *  Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
  6 *
  7 *  Highly based on original thermal_core.c
  8 *  Copyright (C) 2008 Intel Corp
  9 *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
 10 *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
 11 */
 12
 13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 14
 15#include <linux/device.h>
 16#include <linux/err.h>
 17#include <linux/export.h>
 18#include <linux/slab.h>
 19#include <linux/string.h>
 20#include <linux/sysfs.h>
 21
 
 
 22#include "thermal_core.h"
 23#include "thermal_trace.h"
 24
 25int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip)
 26{
 27	enum thermal_trend trend;
 28
 29	if (tz->emul_temperature || !tz->ops->get_trend ||
 30	    tz->ops->get_trend(tz, trip, &trend)) {
 31		if (tz->temperature > tz->last_temperature)
 32			trend = THERMAL_TREND_RAISING;
 33		else if (tz->temperature < tz->last_temperature)
 34			trend = THERMAL_TREND_DROPPING;
 35		else
 36			trend = THERMAL_TREND_STABLE;
 37	}
 38
 39	return trend;
 40}
 
 41
 42struct thermal_instance *
 43get_thermal_instance(struct thermal_zone_device *tz,
 44		     struct thermal_cooling_device *cdev, int trip_index)
 45{
 46	struct thermal_instance *pos = NULL;
 47	struct thermal_instance *target_instance = NULL;
 48	const struct thermal_trip *trip;
 49
 50	mutex_lock(&tz->lock);
 51	mutex_lock(&cdev->lock);
 52
 53	trip = &tz->trips[trip_index];
 54
 55	list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
 56		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
 57			target_instance = pos;
 58			break;
 59		}
 60	}
 61
 62	mutex_unlock(&cdev->lock);
 63	mutex_unlock(&tz->lock);
 64
 65	return target_instance;
 66}
 67EXPORT_SYMBOL(get_thermal_instance);
 68
 69/**
 70 * __thermal_zone_get_temp() - returns the temperature of a thermal zone
 71 * @tz: a valid pointer to a struct thermal_zone_device
 72 * @temp: a valid pointer to where to store the resulting temperature.
 73 *
 74 * When a valid thermal zone reference is passed, it will fetch its
 75 * temperature and fill @temp.
 76 *
 77 * Both tz and tz->ops must be valid pointers when calling this function,
 78 * and the tz->ops->get_temp callback must be provided.
 79 * The function must be called under tz->lock.
 80 *
 81 * Return: On success returns 0, an error code otherwise
 82 */
 83int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
 84{
 85	const struct thermal_trip *trip;
 
 86	int crit_temp = INT_MAX;
 87	int ret = -EINVAL;
 
 
 
 88
 89	lockdep_assert_held(&tz->lock);
 90
 91	ret = tz->ops->get_temp(tz, temp);
 92
 93	if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
 94		for_each_trip(tz, trip) {
 95			if (trip->type == THERMAL_TRIP_CRITICAL) {
 96				crit_temp = trip->temperature;
 
 
 97				break;
 98			}
 99		}
100
101		/*
102		 * Only allow emulating a temperature when the real temperature
103		 * is below the critical temperature so that the emulation code
104		 * cannot hide critical conditions.
105		 */
106		if (!ret && *temp < crit_temp)
107			*temp = tz->emul_temperature;
108	}
109
110	if (ret)
111		dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);
112
113	return ret;
114}
 
115
116/**
117 * thermal_zone_get_temp() - returns the temperature of a thermal zone
118 * @tz: a valid pointer to a struct thermal_zone_device
119 * @temp: a valid pointer to where to store the resulting temperature.
120 *
121 * When a valid thermal zone reference is passed, it will fetch its
122 * temperature and fill @temp.
 
 
 
123 *
124 * Return: On success returns 0, an error code otherwise
125 */
126int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
127{
128	int ret;
 
 
 
129
130	if (IS_ERR_OR_NULL(tz))
131		return -EINVAL;
 
 
 
 
 
132
133	mutex_lock(&tz->lock);
 
 
 
 
 
 
134
135	if (!tz->ops->get_temp) {
136		ret = -EINVAL;
137		goto unlock;
138	}
139
140	ret = __thermal_zone_get_temp(tz, temp);
 
 
141
142unlock:
143	mutex_unlock(&tz->lock);
144
145	return ret;
146}
147EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
148
149static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
150{
151	int ret;
152
153	/*
154	 * No check is needed for the ops->set_cur_state as the
155	 * registering function checked the ops are correctly set
156	 */
157	ret = cdev->ops->set_cur_state(cdev, state);
158	if (ret)
159		return ret;
160
161	thermal_notify_cdev_state_update(cdev, state);
162	thermal_cooling_device_stats_update(cdev, state);
163	thermal_debug_cdev_state_update(cdev, state);
 
 
 
 
 
 
164
165	return 0;
 
166}
167
168void __thermal_cdev_update(struct thermal_cooling_device *cdev)
169{
170	struct thermal_instance *instance;
171	unsigned long target = 0;
172
 
 
 
 
 
 
 
173	/* Make sure cdev enters the deepest cooling state */
174	list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
175		dev_dbg(&cdev->device, "zone%d->target=%lu\n",
176			instance->tz->id, instance->target);
177		if (instance->target == THERMAL_NO_TARGET)
178			continue;
179		if (instance->target > target)
180			target = instance->target;
181	}
182
183	thermal_cdev_set_cur_state(cdev, target);
184
 
 
185	trace_cdev_update(cdev, target);
186	dev_dbg(&cdev->device, "set to state %lu\n", target);
187}
188
189/**
190 * thermal_cdev_update - update cooling device state if needed
191 * @cdev:	pointer to struct thermal_cooling_device
192 *
193 * Update the cooling device state if there is a need.
194 */
195void thermal_cdev_update(struct thermal_cooling_device *cdev)
196{
197	mutex_lock(&cdev->lock);
198	if (!cdev->updated) {
199		__thermal_cdev_update(cdev);
200		cdev->updated = true;
201	}
202	mutex_unlock(&cdev->lock);
203}
204
205/**
206 * thermal_zone_get_slope - return the slope attribute of the thermal zone
207 * @tz: thermal zone device with the slope attribute
208 *
209 * Return: If the thermal zone device has a slope attribute, return it, else
210 * return 1.
211 */
212int thermal_zone_get_slope(struct thermal_zone_device *tz)
213{
214	if (tz && tz->tzp)
215		return tz->tzp->slope;
216	return 1;
217}
218EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
219
220/**
221 * thermal_zone_get_offset - return the offset attribute of the thermal zone
222 * @tz: thermal zone device with the offset attribute
223 *
224 * Return: If the thermal zone device has a offset attribute, return it, else
225 * return 0.
226 */
227int thermal_zone_get_offset(struct thermal_zone_device *tz)
228{
229	if (tz && tz->tzp)
230		return tz->tzp->offset;
231	return 0;
232}
233EXPORT_SYMBOL_GPL(thermal_zone_get_offset);