1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Intel Corp
4 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
5 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
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 *),
16 struct thermal_trip *trip;
19 for_each_trip(tz, trip) {
27 EXPORT_SYMBOL_GPL(for_each_thermal_trip);
29 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
30 int (*cb)(struct thermal_trip *, void *),
35 mutex_lock(&tz->lock);
36 ret = for_each_thermal_trip(tz, cb, data);
37 mutex_unlock(&tz->lock);
41 EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip);
43 int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
47 EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
50 * __thermal_zone_set_trips - Computes the next trip points for the driver
51 * @tz: a pointer to a thermal zone device structure
53 * The function computes the next temperature boundaries by browsing
54 * the trip points. The result is the closer low and high trip points
55 * to the current temperature. These values are passed to the backend
56 * driver to let it set its own notification mechanism (usually an
59 * This function must be called with tz->lock held. Both tz and tz->ops
60 * must be valid pointers.
62 * It does not return a value
64 void __thermal_zone_set_trips(struct thermal_zone_device *tz)
66 const struct thermal_trip *trip;
67 int low = -INT_MAX, high = INT_MAX;
68 bool same_trip = false;
71 lockdep_assert_held(&tz->lock);
73 if (!tz->ops->set_trips)
76 for_each_trip(tz, trip) {
80 trip_low = trip->temperature - trip->hysteresis;
82 if (trip_low < tz->temperature && trip_low > low) {
88 if (trip->temperature > tz->temperature &&
89 trip->temperature < high) {
90 high = trip->temperature;
95 /* No need to change trip points */
96 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
100 * If "high" and "low" are the same, skip the change unless this is the
103 if (same_trip && (tz->prev_low_trip != -INT_MAX ||
104 tz->prev_high_trip != INT_MAX))
107 tz->prev_low_trip = low;
108 tz->prev_high_trip = high;
111 "new temperature boundaries: %d < x < %d\n", low, high);
114 * Set a temperature window. When this window is left the driver
115 * must inform the thermal core via thermal_zone_device_update.
117 ret = tz->ops->set_trips(tz, low, high);
119 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
122 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
123 struct thermal_trip *trip)
125 if (!tz || !tz->trips || trip_id < 0 || trip_id >= tz->num_trips || !trip)
128 *trip = tz->trips[trip_id];
131 EXPORT_SYMBOL_GPL(__thermal_zone_get_trip);
133 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
134 struct thermal_trip *trip)
138 mutex_lock(&tz->lock);
139 ret = __thermal_zone_get_trip(tz, trip_id, trip);
140 mutex_unlock(&tz->lock);
144 EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
146 int thermal_zone_trip_id(const struct thermal_zone_device *tz,
147 const struct thermal_trip *trip)
150 * Assume the trip to be located within the bounds of the thermal
151 * zone's trips[] table.
153 return trip - tz->trips;
155 void thermal_zone_trip_updated(struct thermal_zone_device *tz,
156 const struct thermal_trip *trip)
158 thermal_notify_tz_trip_change(tz, trip);
159 __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
162 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
163 struct thermal_trip *trip, int temp)
165 if (trip->temperature == temp)
168 trip->temperature = temp;
169 thermal_notify_tz_trip_change(tz, trip);
171 EXPORT_SYMBOL_GPL(thermal_zone_set_trip_temp);