2023-01-23 08:27:56 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Intel Corp
|
|
|
|
* Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
|
|
|
|
* Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
|
|
|
|
* Copyright 2022 Linaro Limited
|
|
|
|
*
|
|
|
|
* Thermal trips handling
|
|
|
|
*/
|
|
|
|
#include "thermal_core.h"
|
|
|
|
|
2024-05-28 08:00:51 -07:00
|
|
|
static const char *trip_type_names[] = {
|
|
|
|
[THERMAL_TRIP_ACTIVE] = "active",
|
|
|
|
[THERMAL_TRIP_PASSIVE] = "passive",
|
|
|
|
[THERMAL_TRIP_HOT] = "hot",
|
|
|
|
[THERMAL_TRIP_CRITICAL] = "critical",
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *thermal_trip_type_name(enum thermal_trip_type trip_type)
|
|
|
|
{
|
|
|
|
if (trip_type < THERMAL_TRIP_ACTIVE || trip_type > THERMAL_TRIP_CRITICAL)
|
|
|
|
return "unknown";
|
|
|
|
|
|
|
|
return trip_type_names[trip_type];
|
|
|
|
}
|
|
|
|
|
2023-08-07 11:11:07 -07:00
|
|
|
int for_each_thermal_trip(struct thermal_zone_device *tz,
|
|
|
|
int (*cb)(struct thermal_trip *, void *),
|
|
|
|
void *data)
|
2023-01-23 08:27:56 -07:00
|
|
|
{
|
2024-04-02 11:56:43 -07:00
|
|
|
struct thermal_trip_desc *td;
|
2023-10-12 11:26:46 -07:00
|
|
|
int ret;
|
2023-01-23 08:27:56 -07:00
|
|
|
|
2024-04-02 11:56:43 -07:00
|
|
|
for_each_trip_desc(tz, td) {
|
|
|
|
ret = cb(&td->trip, data);
|
2023-01-23 08:27:56 -07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-07 11:11:07 -07:00
|
|
|
EXPORT_SYMBOL_GPL(for_each_thermal_trip);
|
2023-01-23 08:27:56 -07:00
|
|
|
|
2023-10-03 06:17:24 -07:00
|
|
|
int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
|
|
|
|
int (*cb)(struct thermal_trip *, void *),
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&tz->lock);
|
|
|
|
ret = for_each_thermal_trip(tz, cb, data);
|
|
|
|
mutex_unlock(&tz->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip);
|
|
|
|
|
2024-08-16 01:12:32 -07:00
|
|
|
void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high)
|
2023-01-23 08:27:56 -07:00
|
|
|
{
|
2023-12-04 12:41:30 -07:00
|
|
|
int ret;
|
2023-01-23 08:27:56 -07:00
|
|
|
|
|
|
|
lockdep_assert_held(&tz->lock);
|
|
|
|
|
2024-02-22 10:18:01 -07:00
|
|
|
if (!tz->ops.set_trips)
|
2023-01-23 08:27:56 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* No need to change trip points */
|
|
|
|
if (tz->prev_low_trip == low && tz->prev_high_trip == high)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tz->prev_low_trip = low;
|
|
|
|
tz->prev_high_trip = high;
|
|
|
|
|
|
|
|
dev_dbg(&tz->device,
|
|
|
|
"new temperature boundaries: %d < x < %d\n", low, high);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a temperature window. When this window is left the driver
|
|
|
|
* must inform the thermal core via thermal_zone_device_update.
|
|
|
|
*/
|
2024-02-22 10:18:01 -07:00
|
|
|
ret = tz->ops.set_trips(tz, low, high);
|
2023-01-23 08:27:56 -07:00
|
|
|
if (ret)
|
|
|
|
dev_err(&tz->device, "Failed to set trips: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
2023-12-15 12:53:52 -07:00
|
|
|
int thermal_zone_trip_id(const struct thermal_zone_device *tz,
|
2023-09-21 10:52:44 -07:00
|
|
|
const struct thermal_trip *trip)
|
|
|
|
{
|
2023-10-12 11:25:06 -07:00
|
|
|
/*
|
|
|
|
* Assume the trip to be located within the bounds of the thermal
|
|
|
|
* zone's trips[] table.
|
|
|
|
*/
|
2024-04-02 11:56:43 -07:00
|
|
|
return trip_to_trip_desc(trip) - tz->trips;
|
2023-09-21 10:52:44 -07:00
|
|
|
}
|
2024-04-25 07:07:38 -07:00
|
|
|
|
2024-07-29 09:27:25 -07:00
|
|
|
void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz,
|
|
|
|
struct thermal_trip *trip, int hyst)
|
2023-12-05 05:24:08 -07:00
|
|
|
{
|
2024-07-29 09:27:25 -07:00
|
|
|
WRITE_ONCE(trip->hysteresis, hyst);
|
2024-01-03 04:49:57 -07:00
|
|
|
thermal_notify_tz_trip_change(tz, trip);
|
2023-12-05 05:24:08 -07:00
|
|
|
}
|
2023-12-05 12:18:39 -07:00
|
|
|
|
|
|
|
void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
|
|
|
|
struct thermal_trip *trip, int temp)
|
|
|
|
{
|
|
|
|
if (trip->temperature == temp)
|
|
|
|
return;
|
|
|
|
|
2024-05-28 09:52:13 -07:00
|
|
|
WRITE_ONCE(trip->temperature, temp);
|
2024-05-23 09:05:03 -07:00
|
|
|
thermal_notify_tz_trip_change(tz, trip);
|
|
|
|
|
2024-05-17 02:24:03 -07:00
|
|
|
if (temp == THERMAL_TEMP_INVALID) {
|
|
|
|
struct thermal_trip_desc *td = trip_to_trip_desc(trip);
|
|
|
|
|
2024-05-23 09:05:03 -07:00
|
|
|
if (tz->temperature >= td->threshold) {
|
2024-05-17 02:24:03 -07:00
|
|
|
/*
|
2024-05-23 09:05:03 -07:00
|
|
|
* The trip has been crossed on the way up, so some
|
|
|
|
* adjustments are needed to compensate for the lack
|
|
|
|
* of it going forward.
|
2024-05-17 02:24:03 -07:00
|
|
|
*/
|
2024-05-23 09:05:03 -07:00
|
|
|
if (trip->type == THERMAL_TRIP_PASSIVE) {
|
|
|
|
tz->passive--;
|
|
|
|
WARN_ON_ONCE(tz->passive < 0);
|
|
|
|
}
|
|
|
|
thermal_zone_trip_down(tz, trip);
|
2024-05-17 02:24:03 -07:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Invalidate the threshold to avoid triggering a spurious
|
|
|
|
* trip crossing notification when the trip becomes valid.
|
|
|
|
*/
|
|
|
|
td->threshold = INT_MAX;
|
|
|
|
}
|
2023-12-05 12:18:39 -07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(thermal_zone_set_trip_temp);
|