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 *),
18 lockdep_assert_held(&tz->lock);
23 for (i = 0; i < tz->num_trips; i++) {
24 ret = cb(&tz->trips[i], data);
31 EXPORT_SYMBOL_GPL(for_each_thermal_trip);
33 int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
37 EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
40 * __thermal_zone_set_trips - Computes the next trip points for the driver
41 * @tz: a pointer to a thermal zone device structure
43 * The function computes the next temperature boundaries by browsing
44 * the trip points. The result is the closer low and high trip points
45 * to the current temperature. These values are passed to the backend
46 * driver to let it set its own notification mechanism (usually an
49 * This function must be called with tz->lock held. Both tz and tz->ops
50 * must be valid pointers.
52 * It does not return a value
54 void __thermal_zone_set_trips(struct thermal_zone_device *tz)
56 struct thermal_trip trip;
57 int low = -INT_MAX, high = INT_MAX;
58 bool same_trip = false;
61 lockdep_assert_held(&tz->lock);
63 if (!tz->ops->set_trips)
66 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) {
82 if (trip.temperature > tz->temperature &&
83 trip.temperature < high) {
84 high = trip.temperature;
89 /* No need to change trip points */
90 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
94 * If "high" and "low" are the same, skip the change unless this is the
97 if (same_trip && (tz->prev_low_trip != -INT_MAX ||
98 tz->prev_high_trip != INT_MAX))
101 tz->prev_low_trip = low;
102 tz->prev_high_trip = high;
105 "new temperature boundaries: %d < x < %d\n", low, high);
108 * Set a temperature window. When this window is left the driver
109 * must inform the thermal core via thermal_zone_device_update.
111 ret = tz->ops->set_trips(tz, low, high);
113 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
116 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
117 struct thermal_trip *trip)
119 if (!tz || !tz->trips || trip_id < 0 || trip_id >= tz->num_trips || !trip)
122 *trip = tz->trips[trip_id];
125 EXPORT_SYMBOL_GPL(__thermal_zone_get_trip);
127 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
128 struct thermal_trip *trip)
132 mutex_lock(&tz->lock);
133 ret = __thermal_zone_get_trip(tz, trip_id, trip);
134 mutex_unlock(&tz->lock);
138 EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
140 int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
141 const struct thermal_trip *trip)
143 struct thermal_trip t;
146 if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips)
149 ret = __thermal_zone_get_trip(tz, trip_id, &t);
153 if (t.type != trip->type)
156 if (t.temperature != trip->temperature && tz->ops->set_trip_temp) {
157 ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature);
162 if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) {
163 ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis);
168 if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis))
169 tz->trips[trip_id] = *trip;
171 thermal_notify_tz_trip_change(tz->id, trip_id, trip->type,
172 trip->temperature, trip->hysteresis);
174 __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);