1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Intel Corp
6 * Copyright 2022 Linaro Limited
8 * Thermal trips handling
10 #include "thermal_core.h"
12 static const char *trip_type_names[] = {
13 [THERMAL_TRIP_ACTIVE] = "active",
14 [THERMAL_TRIP_PASSIVE] = "passive",
15 [THERMAL_TRIP_HOT] = "hot",
16 [THERMAL_TRIP_CRITICAL] = "critical",
19 const char *thermal_trip_type_name(enum thermal_trip_type trip_type)
21 if (trip_type < THERMAL_TRIP_ACTIVE || trip_type > THERMAL_TRIP_CRITICAL)
24 return trip_type_names[trip_type];
27 int for_each_thermal_trip(struct thermal_zone_device *tz,
28 int (*cb)(struct thermal_trip *, void *),
31 struct thermal_trip_desc *td;
34 for_each_trip_desc(tz, td) {
35 ret = cb(&td->trip, data);
42 EXPORT_SYMBOL_GPL(for_each_thermal_trip);
44 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
45 int (*cb)(struct thermal_trip *, void *),
50 mutex_lock(&tz->lock);
51 ret = for_each_thermal_trip(tz, cb, data);
52 mutex_unlock(&tz->lock);
56 EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip);
58 int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
62 EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
65 * thermal_zone_set_trips - Computes the next trip points for the driver
66 * @tz: a pointer to a thermal zone device structure
68 * The function computes the next temperature boundaries by browsing
69 * the trip points. The result is the closer low and high trip points
70 * to the current temperature. These values are passed to the backend
71 * driver to let it set its own notification mechanism (usually an
74 * This function must be called with tz->lock held. Both tz and tz->ops
75 * must be valid pointers.
77 * It does not return a value
79 void thermal_zone_set_trips(struct thermal_zone_device *tz)
81 const struct thermal_trip_desc *td;
82 int low = -INT_MAX, high = INT_MAX;
85 lockdep_assert_held(&tz->lock);
87 if (!tz->ops.set_trips)
90 for_each_trip_desc(tz, td) {
91 if (td->threshold < tz->temperature && td->threshold > low)
94 if (td->threshold > tz->temperature && td->threshold < high)
98 /* No need to change trip points */
99 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
102 tz->prev_low_trip = low;
103 tz->prev_high_trip = high;
106 "new temperature boundaries: %d < x < %d\n", low, high);
109 * Set a temperature window. When this window is left the driver
110 * must inform the thermal core via thermal_zone_device_update.
112 ret = tz->ops.set_trips(tz, low, high);
114 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
117 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
118 struct thermal_trip *trip)
120 if (!tz || !trip || trip_id < 0 || trip_id >= tz->num_trips)
123 mutex_lock(&tz->lock);
124 *trip = tz->trips[trip_id].trip;
125 mutex_unlock(&tz->lock);
129 EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
131 int thermal_zone_trip_id(const struct thermal_zone_device *tz,
132 const struct thermal_trip *trip)
135 * Assume the trip to be located within the bounds of the thermal
136 * zone's trips[] table.
138 return trip_to_trip_desc(trip) - tz->trips;
141 void thermal_zone_trip_updated(struct thermal_zone_device *tz,
142 const struct thermal_trip *trip)
144 thermal_notify_tz_trip_change(tz, trip);
145 __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
148 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
149 struct thermal_trip *trip, int temp)
151 if (trip->temperature == temp)
154 WRITE_ONCE(trip->temperature, temp);
155 thermal_notify_tz_trip_change(tz, trip);
157 if (temp == THERMAL_TEMP_INVALID) {
158 struct thermal_trip_desc *td = trip_to_trip_desc(trip);
160 if (tz->temperature >= td->threshold) {
162 * The trip has been crossed on the way up, so some
163 * adjustments are needed to compensate for the lack
164 * of it going forward.
166 if (trip->type == THERMAL_TRIP_PASSIVE) {
168 WARN_ON_ONCE(tz->passive < 0);
170 thermal_zone_trip_down(tz, trip);
173 * Invalidate the threshold to avoid triggering a spurious
174 * trip crossing notification when the trip becomes valid.
176 td->threshold = INT_MAX;
179 EXPORT_SYMBOL_GPL(thermal_zone_set_trip_temp);