2018-05-08 01:52:29 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-11-08 13:09:04 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
2020-05-11 20:24:52 +08:00
|
|
|
#include <linux/export.h>
|
2016-11-08 13:09:04 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
2020-05-11 20:24:51 +08:00
|
|
|
#include <linux/sysfs.h>
|
2016-11-08 13:09:04 +08:00
|
|
|
|
|
|
|
#include <trace/events/thermal.h>
|
|
|
|
|
|
|
|
#include "thermal_core.h"
|
|
|
|
|
|
|
|
int get_tz_trend(struct thermal_zone_device *tz, int trip)
|
|
|
|
{
|
|
|
|
enum thermal_trend trend;
|
|
|
|
|
|
|
|
if (tz->emul_temperature || !tz->ops->get_trend ||
|
|
|
|
tz->ops->get_trend(tz, trip, &trend)) {
|
|
|
|
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,
|
|
|
|
struct thermal_cooling_device *cdev, int trip)
|
|
|
|
{
|
|
|
|
struct thermal_instance *pos = NULL;
|
|
|
|
struct thermal_instance *target_instance = NULL;
|
|
|
|
|
|
|
|
mutex_lock(&tz->lock);
|
|
|
|
mutex_lock(&cdev->lock);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-11-10 23:24:55 +08:00
|
|
|
/**
|
|
|
|
* __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,
|
|
|
|
* and the tz->ops->get_temp callback must be provided.
|
|
|
|
* The function must be called under tz->lock.
|
|
|
|
*
|
|
|
|
* Return: On success returns 0, an error code otherwise
|
|
|
|
*/
|
2022-08-05 23:38:34 +08:00
|
|
|
int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
|
2016-11-08 13:09:04 +08:00
|
|
|
{
|
|
|
|
int ret = -EINVAL;
|
|
|
|
int count;
|
|
|
|
int crit_temp = INT_MAX;
|
thermal/core: Add a generic thermal_zone_get_trip() function
The thermal_zone_device_ops structure defines a set of ops family,
get_trip_temp(), get_trip_hyst(), get_trip_type(). Each of them is
returning a property of a trip point.
The result is the code is calling the ops everywhere to get a trip
point which is supposed to be defined in the backend driver. It is a
non-sense as a thermal trip can be generic and used by the backend
driver to declare its trip points.
Part of the thermal framework has been changed and all the OF thermal
drivers are using the same definition for the trip point and use a
thermal zone registration variant to pass those trip points which are
part of the thermal zone device structure.
Consequently, we can use a generic function to get the trip points
when they are stored in the thermal zone device structure.
This approach can be generalized to all the drivers and we can get rid
of the ops->get_trip_*. That will result to a much more simpler code
and make possible to rework how the thermal trip are handled in the
thermal core framework as discussed previously.
This change adds a function thermal_zone_get_trip() where we get the
thermal trip point structure which contains all the properties (type,
temp, hyst) instead of doing multiple calls to ops->get_trip_*.
That opens the door for trip point extension with more attributes. For
instance, replacing the trip points disabled bitmask with a 'disabled'
field in the structure.
Here we replace all the calls to ops->get_trip_* in the thermal core
code with a call to the thermal_zone_get_trip() function.
The thermal zone ops defines a callback to retrieve the critical
temperature. As the trip handling is being reworked, all the trip
points will be the same whatever the driver and consequently finding
the critical trip temperature will be just a loop to search for a
critical trip point type.
Provide such a generic function, so we encapsulate the ops
get_crit_temp() which can be removed when all the backend drivers are
using the generic trip points handling.
While at it, add the thermal_zone_get_num_trips() to encapsulate the
code more and reduce the grip with the thermal framework internals.
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Zhang Rui <rui.zhang@intel.com>
Link: https://lore.kernel.org/r/20221003092602.1323944-2-daniel.lezcano@linaro.org
2022-10-03 17:25:34 +08:00
|
|
|
struct thermal_trip trip;
|
2016-11-08 13:09:04 +08:00
|
|
|
|
2022-08-05 23:38:34 +08:00
|
|
|
lockdep_assert_held(&tz->lock);
|
2016-11-08 13:09:04 +08:00
|
|
|
|
|
|
|
ret = tz->ops->get_temp(tz, temp);
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
|
2022-07-23 04:00:04 +08:00
|
|
|
for (count = 0; count < tz->num_trips; count++) {
|
thermal/core: Add a generic thermal_zone_get_trip() function
The thermal_zone_device_ops structure defines a set of ops family,
get_trip_temp(), get_trip_hyst(), get_trip_type(). Each of them is
returning a property of a trip point.
The result is the code is calling the ops everywhere to get a trip
point which is supposed to be defined in the backend driver. It is a
non-sense as a thermal trip can be generic and used by the backend
driver to declare its trip points.
Part of the thermal framework has been changed and all the OF thermal
drivers are using the same definition for the trip point and use a
thermal zone registration variant to pass those trip points which are
part of the thermal zone device structure.
Consequently, we can use a generic function to get the trip points
when they are stored in the thermal zone device structure.
This approach can be generalized to all the drivers and we can get rid
of the ops->get_trip_*. That will result to a much more simpler code
and make possible to rework how the thermal trip are handled in the
thermal core framework as discussed previously.
This change adds a function thermal_zone_get_trip() where we get the
thermal trip point structure which contains all the properties (type,
temp, hyst) instead of doing multiple calls to ops->get_trip_*.
That opens the door for trip point extension with more attributes. For
instance, replacing the trip points disabled bitmask with a 'disabled'
field in the structure.
Here we replace all the calls to ops->get_trip_* in the thermal core
code with a call to the thermal_zone_get_trip() function.
The thermal zone ops defines a callback to retrieve the critical
temperature. As the trip handling is being reworked, all the trip
points will be the same whatever the driver and consequently finding
the critical trip temperature will be just a loop to search for a
critical trip point type.
Provide such a generic function, so we encapsulate the ops
get_crit_temp() which can be removed when all the backend drivers are
using the generic trip points handling.
While at it, add the thermal_zone_get_num_trips() to encapsulate the
code more and reduce the grip with the thermal framework internals.
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Zhang Rui <rui.zhang@intel.com>
Link: https://lore.kernel.org/r/20221003092602.1323944-2-daniel.lezcano@linaro.org
2022-10-03 17:25:34 +08:00
|
|
|
ret = __thermal_zone_get_trip(tz, count, &trip);
|
|
|
|
if (!ret && trip.type == THERMAL_TRIP_CRITICAL) {
|
|
|
|
crit_temp = trip.temperature;
|
2016-11-08 13:09:04 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-01 00:54:48 +08:00
|
|
|
/**
|
2022-08-05 23:38:34 +08:00
|
|
|
* 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.
|
2020-04-01 00:54:48 +08:00
|
|
|
*
|
2022-08-05 23:38:34 +08:00
|
|
|
* When a valid thermal zone reference is passed, it will fetch its
|
|
|
|
* temperature and fill @temp.
|
2020-04-01 00:54:48 +08:00
|
|
|
*
|
2022-08-05 23:38:34 +08:00
|
|
|
* Return: On success returns 0, an error code otherwise
|
2020-04-01 00:54:48 +08:00
|
|
|
*/
|
2022-08-05 23:38:34 +08:00
|
|
|
int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-11-10 23:24:55 +08:00
|
|
|
if (IS_ERR_OR_NULL(tz))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-08-05 23:38:34 +08:00
|
|
|
mutex_lock(&tz->lock);
|
2022-11-10 23:24:54 +08:00
|
|
|
|
2022-11-10 23:24:55 +08:00
|
|
|
if (!tz->ops->get_temp) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
2022-11-10 23:24:54 +08:00
|
|
|
if (device_is_registered(&tz->device))
|
|
|
|
ret = __thermal_zone_get_temp(tz, temp);
|
|
|
|
else
|
|
|
|
ret = -ENODEV;
|
|
|
|
|
2022-11-10 23:24:55 +08:00
|
|
|
unlock:
|
2022-08-05 23:38:34 +08:00
|
|
|
mutex_unlock(&tz->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
|
|
|
|
|
2020-07-06 18:55:38 +08:00
|
|
|
static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
|
|
|
|
int target)
|
|
|
|
{
|
|
|
|
if (cdev->ops->set_cur_state(cdev, target))
|
|
|
|
return;
|
|
|
|
|
|
|
|
thermal_notify_cdev_state_update(cdev->id, target);
|
|
|
|
thermal_cooling_device_stats_update(cdev, target);
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:43:06 +08:00
|
|
|
void __thermal_cdev_update(struct thermal_cooling_device *cdev)
|
2016-11-08 13:09:04 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2018-04-02 18:56:25 +08:00
|
|
|
|
2020-07-06 18:55:38 +08:00
|
|
|
thermal_cdev_set_cur_state(cdev, target);
|
2018-04-02 18:56:25 +08:00
|
|
|
|
2016-11-08 13:09:04 +08:00
|
|
|
trace_cdev_update(cdev, target);
|
|
|
|
dev_dbg(&cdev->device, "set to state %lu\n", target);
|
|
|
|
}
|
2021-04-22 19:43:06 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2016-11-08 13:09:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|