xref: /linux/drivers/thermal/thermal_trip.c (revision 1c9f8dff62d85ce00b0e99f774a84bd783af7cac)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
7  *
8  * Thermal trips handling
9  */
10 #include "thermal_core.h"
11 
12 int for_each_thermal_trip(struct thermal_zone_device *tz,
13 			  int (*cb)(struct thermal_trip *, void *),
14 			  void *data)
15 {
16 	int i, ret;
17 
18 	lockdep_assert_held(&tz->lock);
19 
20 	if (!tz->trips)
21 		return -ENODATA;
22 
23 	for (i = 0; i < tz->num_trips; i++) {
24 		ret = cb(&tz->trips[i], data);
25 		if (ret)
26 			return ret;
27 	}
28 
29 	return 0;
30 }
31 EXPORT_SYMBOL_GPL(for_each_thermal_trip);
32 
33 int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
34 {
35 	return tz->num_trips;
36 }
37 EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
38 
39 /**
40  * __thermal_zone_set_trips - Computes the next trip points for the driver
41  * @tz: a pointer to a thermal zone device structure
42  *
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
47  * interrupt).
48  *
49  * This function must be called with tz->lock held. Both tz and tz->ops
50  * must be valid pointers.
51  *
52  * It does not return a value
53  */
54 void __thermal_zone_set_trips(struct thermal_zone_device *tz)
55 {
56 	struct thermal_trip trip;
57 	int low = -INT_MAX, high = INT_MAX;
58 	int i, ret;
59 
60 	lockdep_assert_held(&tz->lock);
61 
62 	if (!tz->ops->set_trips)
63 		return;
64 
65 	for (i = 0; i < tz->num_trips; i++) {
66 		int trip_low;
67 
68 		ret = __thermal_zone_get_trip(tz, i , &trip);
69 		if (ret)
70 			return;
71 
72 		trip_low = trip.temperature - trip.hysteresis;
73 
74 		if (trip_low < tz->temperature && trip_low > low)
75 			low = trip_low;
76 
77 		if (trip.temperature > tz->temperature &&
78 		    trip.temperature < high)
79 			high = trip.temperature;
80 	}
81 
82 	/* No need to change trip points */
83 	if (tz->prev_low_trip == low && tz->prev_high_trip == high)
84 		return;
85 
86 	tz->prev_low_trip = low;
87 	tz->prev_high_trip = high;
88 
89 	dev_dbg(&tz->device,
90 		"new temperature boundaries: %d < x < %d\n", low, high);
91 
92 	/*
93 	 * Set a temperature window. When this window is left the driver
94 	 * must inform the thermal core via thermal_zone_device_update.
95 	 */
96 	ret = tz->ops->set_trips(tz, low, high);
97 	if (ret)
98 		dev_err(&tz->device, "Failed to set trips: %d\n", ret);
99 }
100 
101 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
102 			    struct thermal_trip *trip)
103 {
104 	int ret;
105 
106 	if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip)
107 		return -EINVAL;
108 
109 	if (tz->trips) {
110 		*trip = tz->trips[trip_id];
111 		return 0;
112 	}
113 
114 	if (tz->ops->get_trip_hyst) {
115 		ret = tz->ops->get_trip_hyst(tz, trip_id, &trip->hysteresis);
116 		if (ret)
117 			return ret;
118 	} else {
119 		trip->hysteresis = 0;
120 	}
121 
122 	ret = tz->ops->get_trip_temp(tz, trip_id, &trip->temperature);
123 	if (ret)
124 		return ret;
125 
126 	return tz->ops->get_trip_type(tz, trip_id, &trip->type);
127 }
128 EXPORT_SYMBOL_GPL(__thermal_zone_get_trip);
129 
130 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
131 			  struct thermal_trip *trip)
132 {
133 	int ret;
134 
135 	mutex_lock(&tz->lock);
136 	ret = __thermal_zone_get_trip(tz, trip_id, trip);
137 	mutex_unlock(&tz->lock);
138 
139 	return ret;
140 }
141 EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
142 
143 int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
144 			  const struct thermal_trip *trip)
145 {
146 	struct thermal_trip t;
147 	int ret;
148 
149 	if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips)
150 		return -EINVAL;
151 
152 	ret = __thermal_zone_get_trip(tz, trip_id, &t);
153 	if (ret)
154 		return ret;
155 
156 	if (t.type != trip->type)
157 		return -EINVAL;
158 
159 	if (t.temperature != trip->temperature && tz->ops->set_trip_temp) {
160 		ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature);
161 		if (ret)
162 			return ret;
163 	}
164 
165 	if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) {
166 		ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis);
167 		if (ret)
168 			return ret;
169 	}
170 
171 	if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis))
172 		tz->trips[trip_id] = *trip;
173 
174 	thermal_notify_tz_trip_change(tz->id, trip_id, trip->type,
175 				      trip->temperature, trip->hysteresis);
176 
177 	__thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
178 
179 	return 0;
180 }
181