Skip to content

Commit 5b8de18

Browse files
dlezcanorafaeljw
authored andcommitted
thermal/core: Move the thermal trip code to a dedicated file
The thermal_core.c files contains a lot of functions handling different thermal components like the governors, the trip points, the cooling device, the OF cooling device, etc ... This organization does not help to migrate to a more sane code where there is a better self-encapsulation as all the components' internals can be directly accessed from a single file. For the sake of clarity, let's move the thermal trip points code in a dedicated thermal_trip.c file and add a function to browse all the trip points like we do with the thermal zones, the govenors and the cooling devices. The same can be done for the cooling devices and the governor code but that will come later as the current work in the thermal framework is to fix the trip point handling and use a generic trip point structure. No functional changes intended. Signed-off-by: Daniel Lezcano <[email protected]> Reviewed-by: Zhang Rui <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent b57d628 commit 5b8de18

File tree

5 files changed

+188
-151
lines changed

5 files changed

+188
-151
lines changed

drivers/thermal/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#
55

66
obj-$(CONFIG_THERMAL) += thermal_sys.o
7-
thermal_sys-y += thermal_core.o thermal_sysfs.o \
8-
thermal_helpers.o
7+
thermal_sys-y += thermal_core.o thermal_sysfs.o
8+
thermal_sys-y += thermal_trip.o thermal_helpers.o
99

1010
# netlink interface to manage the thermal framework
1111
thermal_sys-$(CONFIG_THERMAL_NETLINK) += thermal_netlink.o

drivers/thermal/thermal_core.c

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,12 +1156,6 @@ static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms
11561156
*delay_jiffies = round_jiffies(*delay_jiffies);
11571157
}
11581158

1159-
int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
1160-
{
1161-
return tz->num_trips;
1162-
}
1163-
EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
1164-
11651159
int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
11661160
{
11671161
int i, ret = -EINVAL;
@@ -1188,87 +1182,6 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
11881182
}
11891183
EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
11901184

1191-
int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
1192-
struct thermal_trip *trip)
1193-
{
1194-
int ret;
1195-
1196-
if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip)
1197-
return -EINVAL;
1198-
1199-
if (tz->trips) {
1200-
*trip = tz->trips[trip_id];
1201-
return 0;
1202-
}
1203-
1204-
if (tz->ops->get_trip_hyst) {
1205-
ret = tz->ops->get_trip_hyst(tz, trip_id, &trip->hysteresis);
1206-
if (ret)
1207-
return ret;
1208-
} else {
1209-
trip->hysteresis = 0;
1210-
}
1211-
1212-
ret = tz->ops->get_trip_temp(tz, trip_id, &trip->temperature);
1213-
if (ret)
1214-
return ret;
1215-
1216-
return tz->ops->get_trip_type(tz, trip_id, &trip->type);
1217-
}
1218-
EXPORT_SYMBOL_GPL(__thermal_zone_get_trip);
1219-
1220-
int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
1221-
struct thermal_trip *trip)
1222-
{
1223-
int ret;
1224-
1225-
mutex_lock(&tz->lock);
1226-
ret = __thermal_zone_get_trip(tz, trip_id, trip);
1227-
mutex_unlock(&tz->lock);
1228-
1229-
return ret;
1230-
}
1231-
EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
1232-
1233-
int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
1234-
const struct thermal_trip *trip)
1235-
{
1236-
struct thermal_trip t;
1237-
int ret;
1238-
1239-
if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips)
1240-
return -EINVAL;
1241-
1242-
ret = __thermal_zone_get_trip(tz, trip_id, &t);
1243-
if (ret)
1244-
return ret;
1245-
1246-
if (t.type != trip->type)
1247-
return -EINVAL;
1248-
1249-
if (t.temperature != trip->temperature && tz->ops->set_trip_temp) {
1250-
ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature);
1251-
if (ret)
1252-
return ret;
1253-
}
1254-
1255-
if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) {
1256-
ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis);
1257-
if (ret)
1258-
return ret;
1259-
}
1260-
1261-
if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis))
1262-
tz->trips[trip_id] = *trip;
1263-
1264-
thermal_notify_tz_trip_change(tz->id, trip_id, trip->type,
1265-
trip->temperature, trip->hysteresis);
1266-
1267-
__thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
1268-
1269-
return 0;
1270-
}
1271-
12721185
/**
12731186
* thermal_zone_device_register_with_trips() - register a new thermal zone device
12741187
* @type: the thermal zone device type

drivers/thermal/thermal_core.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
5252
int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
5353
void *thermal_governor);
5454

55+
int __for_each_thermal_trip(struct thermal_zone_device *,
56+
int (*cb)(struct thermal_trip *, void *),
57+
void *);
58+
5559
struct thermal_zone_device *thermal_zone_get_by_id(int id);
5660

5761
struct thermal_attr {

drivers/thermal/thermal_helpers.c

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -146,68 +146,6 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
146146
}
147147
EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
148148

149-
/**
150-
* __thermal_zone_set_trips - Computes the next trip points for the driver
151-
* @tz: a pointer to a thermal zone device structure
152-
*
153-
* The function computes the next temperature boundaries by browsing
154-
* the trip points. The result is the closer low and high trip points
155-
* to the current temperature. These values are passed to the backend
156-
* driver to let it set its own notification mechanism (usually an
157-
* interrupt).
158-
*
159-
* This function must be called with tz->lock held. Both tz and tz->ops
160-
* must be valid pointers.
161-
*
162-
* It does not return a value
163-
*/
164-
void __thermal_zone_set_trips(struct thermal_zone_device *tz)
165-
{
166-
struct thermal_trip trip;
167-
int low = -INT_MAX, high = INT_MAX;
168-
int i, ret;
169-
170-
lockdep_assert_held(&tz->lock);
171-
172-
if (!tz->ops->set_trips)
173-
return;
174-
175-
for (i = 0; i < tz->num_trips; i++) {
176-
int trip_low;
177-
178-
ret = __thermal_zone_get_trip(tz, i , &trip);
179-
if (ret)
180-
return;
181-
182-
trip_low = trip.temperature - trip.hysteresis;
183-
184-
if (trip_low < tz->temperature && trip_low > low)
185-
low = trip_low;
186-
187-
if (trip.temperature > tz->temperature &&
188-
trip.temperature < high)
189-
high = trip.temperature;
190-
}
191-
192-
/* No need to change trip points */
193-
if (tz->prev_low_trip == low && tz->prev_high_trip == high)
194-
return;
195-
196-
tz->prev_low_trip = low;
197-
tz->prev_high_trip = high;
198-
199-
dev_dbg(&tz->device,
200-
"new temperature boundaries: %d < x < %d\n", low, high);
201-
202-
/*
203-
* Set a temperature window. When this window is left the driver
204-
* must inform the thermal core via thermal_zone_device_update.
205-
*/
206-
ret = tz->ops->set_trips(tz, low, high);
207-
if (ret)
208-
dev_err(&tz->device, "Failed to set trips: %d\n", ret);
209-
}
210-
211149
static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
212150
int target)
213151
{

drivers/thermal/thermal_trip.c

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

0 commit comments

Comments
 (0)