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 int __for_each_thermal_trip(struct thermal_zone_device *tz,
13 int (*cb)(struct thermal_trip *, void *),
17 struct thermal_trip trip;
19 lockdep_assert_held(&tz->lock);
21 for (i = 0; i < tz->num_trips; i++) {
23 ret = __thermal_zone_get_trip(tz, i, &trip);
27 ret = cb(&trip, data);
35 int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
39 EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
42 * __thermal_zone_set_trips - Computes the next trip points for the driver
43 * @tz: a pointer to a thermal zone device structure
45 * The function computes the next temperature boundaries by browsing
46 * the trip points. The result is the closer low and high trip points
47 * to the current temperature. These values are passed to the backend
48 * driver to let it set its own notification mechanism (usually an
51 * This function must be called with tz->lock held. Both tz and tz->ops
52 * must be valid pointers.
54 * It does not return a value
56 void __thermal_zone_set_trips(struct thermal_zone_device *tz)
58 struct thermal_trip trip;
59 int low = -INT_MAX, high = INT_MAX;
62 lockdep_assert_held(&tz->lock);
64 if (!tz->ops->set_trips)
67 for (i = 0; i < tz->num_trips; i++) {
70 ret = __thermal_zone_get_trip(tz, i , &trip);
74 trip_low = trip.temperature - trip.hysteresis;
76 if (trip_low < tz->temperature && trip_low > low)
79 if (trip.temperature > tz->temperature &&
80 trip.temperature < high)
81 high = trip.temperature;
84 /* No need to change trip points */
85 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
88 tz->prev_low_trip = low;
89 tz->prev_high_trip = high;
92 "new temperature boundaries: %d < x < %d\n", low, high);
95 * Set a temperature window. When this window is left the driver
96 * must inform the thermal core via thermal_zone_device_update.
98 ret = tz->ops->set_trips(tz, low, high);
100 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
103 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
104 struct thermal_trip *trip)
108 if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip)
112 *trip = tz->trips[trip_id];
116 if (tz->ops->get_trip_hyst) {
117 ret = tz->ops->get_trip_hyst(tz, trip_id, &trip->hysteresis);
121 trip->hysteresis = 0;
124 ret = tz->ops->get_trip_temp(tz, trip_id, &trip->temperature);
128 return tz->ops->get_trip_type(tz, trip_id, &trip->type);
130 EXPORT_SYMBOL_GPL(__thermal_zone_get_trip);
132 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
133 struct thermal_trip *trip)
137 mutex_lock(&tz->lock);
138 ret = __thermal_zone_get_trip(tz, trip_id, trip);
139 mutex_unlock(&tz->lock);
143 EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
145 int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
146 const struct thermal_trip *trip)
148 struct thermal_trip t;
151 if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips)
154 ret = __thermal_zone_get_trip(tz, trip_id, &t);
158 if (t.type != trip->type)
161 if (t.temperature != trip->temperature && tz->ops->set_trip_temp) {
162 ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature);
167 if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) {
168 ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis);
173 if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis))
174 tz->trips[trip_id] = *trip;
176 thermal_notify_tz_trip_change(tz->id, trip_id, trip->type,
177 trip->temperature, trip->hysteresis);
179 __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);