thermal_helpers.c 5.88 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  thermal_helpers.c - helper functions to handle thermal devices
 *
 *  Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
 *
 *  Highly based on original thermal_core.c
 *  Copyright (C) 2008 Intel Corp
 *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
 *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/device.h>
#include <linux/err.h>
17
#include <linux/export.h>
18 19
#include <linux/slab.h>
#include <linux/string.h>
20
#include <linux/sysfs.h>
21 22

#include "thermal_core.h"
23
#include "thermal_trace.h"
24

25
int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip)
26 27 28
{
	enum thermal_trend trend;

29 30
	if (tz->emul_temperature || !tz->ops.get_trend ||
	    tz->ops.get_trend(tz, trip, &trend)) {
31 32 33 34 35 36 37 38 39 40 41 42 43
		if (tz->temperature > tz->last_temperature)
			trend = THERMAL_TREND_RAISING;
		else if (tz->temperature < tz->last_temperature)
			trend = THERMAL_TREND_DROPPING;
		else
			trend = THERMAL_TREND_STABLE;
	}

	return trend;
}

struct thermal_instance *
get_thermal_instance(struct thermal_zone_device *tz,
44
		     struct thermal_cooling_device *cdev, int trip_index)
45 46 47
{
	struct thermal_instance *pos = NULL;
	struct thermal_instance *target_instance = NULL;
48
	const struct thermal_trip *trip;
49 50 51 52

	mutex_lock(&tz->lock);
	mutex_lock(&cdev->lock);

53
	trip = &tz->trips[trip_index].trip;
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68
	list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
		if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
			target_instance = pos;
			break;
		}
	}

	mutex_unlock(&cdev->lock);
	mutex_unlock(&tz->lock);

	return target_instance;
}
EXPORT_SYMBOL(get_thermal_instance);

69 70 71 72 73 74 75 76 77
/**
 * __thermal_zone_get_temp() - returns the temperature of a thermal zone
 * @tz: a valid pointer to a struct thermal_zone_device
 * @temp: a valid pointer to where to store the resulting temperature.
 *
 * When a valid thermal zone reference is passed, it will fetch its
 * temperature and fill @temp.
 *
 * 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 80 81 82
 * The function must be called under tz->lock.
 *
 * Return: On success returns 0, an error code otherwise
 */
83
int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
84
{
85
	const struct thermal_trip_desc *td;
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 95 96
		for_each_trip_desc(tz, td) {
			const struct thermal_trip *trip = &td->trip;

97 98
			if (trip->type == THERMAL_TRIP_CRITICAL) {
				crit_temp = trip->temperature;
99 100 101 102 103 104 105 106 107 108 109 110 111
				break;
			}
		}

		/*
		 * Only allow emulating a temperature when the real temperature
		 * is below the critical temperature so that the emulation code
		 * cannot hide critical conditions.
		 */
		if (!ret && *temp < crit_temp)
			*temp = tz->emul_temperature;
	}

112 113 114
	if (ret)
		dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);

115 116 117
	return ret;
}

118
/**
119 120 121
 * thermal_zone_get_temp() - returns the temperature of a thermal zone
 * @tz: a valid pointer to a struct thermal_zone_device
 * @temp: a valid pointer to where to store the resulting temperature.
122
 *
123 124
 * When a valid thermal zone reference is passed, it will fetch its
 * temperature and fill @temp.
125
 *
126
 * Return: On success returns 0, an error code otherwise
127
 */
128 129 130 131
int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
{
	int ret;

132 133 134
	if (IS_ERR_OR_NULL(tz))
		return -EINVAL;

135
	mutex_lock(&tz->lock);
136

137
	if (!tz->ops.get_temp) {
138 139 140 141
		ret = -EINVAL;
		goto unlock;
	}

142
	ret = __thermal_zone_get_temp(tz, temp);
143

144
unlock:
145 146 147 148 149 150
	mutex_unlock(&tz->lock);

	return ret;
}
EXPORT_SYMBOL_GPL(thermal_zone_get_temp);

151
static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
152
{
153 154 155 156 157 158 159
	int ret;

	/*
	 * No check is needed for the ops->set_cur_state as the
	 * registering function checked the ops are correctly set
	 */
	ret = cdev->ops->set_cur_state(cdev, state);
160 161
	if (ret)
		return ret;
162

163 164 165 166 167
	thermal_notify_cdev_state_update(cdev, state);
	thermal_cooling_device_stats_update(cdev, state);
	thermal_debug_cdev_state_update(cdev, state);

	return 0;
168 169
}

170
void __thermal_cdev_update(struct thermal_cooling_device *cdev)
171 172 173 174 175 176 177 178 179 180 181 182 183
{
	struct thermal_instance *instance;
	unsigned long target = 0;

	/* Make sure cdev enters the deepest cooling state */
	list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
		dev_dbg(&cdev->device, "zone%d->target=%lu\n",
			instance->tz->id, instance->target);
		if (instance->target == THERMAL_NO_TARGET)
			continue;
		if (instance->target > target)
			target = instance->target;
	}
184

185
	thermal_cdev_set_cur_state(cdev, target);
186

187 188 189
	trace_cdev_update(cdev, target);
	dev_dbg(&cdev->device, "set to state %lu\n", target);
}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

/**
 * thermal_cdev_update - update cooling device state if needed
 * @cdev:	pointer to struct thermal_cooling_device
 *
 * Update the cooling device state if there is a need.
 */
void thermal_cdev_update(struct thermal_cooling_device *cdev)
{
	mutex_lock(&cdev->lock);
	if (!cdev->updated) {
		__thermal_cdev_update(cdev);
		cdev->updated = true;
	}
	mutex_unlock(&cdev->lock);
}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

/**
 * thermal_zone_get_slope - return the slope attribute of the thermal zone
 * @tz: thermal zone device with the slope attribute
 *
 * Return: If the thermal zone device has a slope attribute, return it, else
 * return 1.
 */
int thermal_zone_get_slope(struct thermal_zone_device *tz)
{
	if (tz && tz->tzp)
		return tz->tzp->slope;
	return 1;
}
EXPORT_SYMBOL_GPL(thermal_zone_get_slope);

/**
 * thermal_zone_get_offset - return the offset attribute of the thermal zone
 * @tz: thermal zone device with the offset attribute
 *
 * Return: If the thermal zone device has a offset attribute, return it, else
 * return 0.
 */
int thermal_zone_get_offset(struct thermal_zone_device *tz)
{
	if (tz && tz->tzp)
		return tz->tzp->offset;
	return 0;
}
EXPORT_SYMBOL_GPL(thermal_zone_get_offset);