Skip to content

Commit 247d403

Browse files
zhang-ruisashalevin
authored andcommitted
Thermal: initialize thermal zone device correctly
[ Upstream commit bb431ba ] After thermal zone device registered, as we have not read any temperature before, thus tz->temperature should not be 0, which actually means 0C, and thermal trend is not available. In this case, we need specially handling for the first thermal_zone_device_update(). Both thermal core framework and step_wise governor is enhanced to handle this. And since the step_wise governor is the only one that uses trends, so it's the only thermal governor that needs to be updated. CC: <[email protected]> #3.18+ Tested-by: Manuel Krause <[email protected]> Tested-by: szegad <[email protected]> Tested-by: prash <[email protected]> Tested-by: amish <[email protected]> Tested-by: Matthias <[email protected]> Reviewed-by: Javi Merino <[email protected]> Signed-off-by: Zhang Rui <[email protected]> Signed-off-by: Chen Yu <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 40b7566 commit 247d403

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

drivers/thermal/step_wise.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ static unsigned long get_target_state(struct thermal_instance *instance,
6363
next_target = instance->target;
6464
dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
6565

66+
if (!instance->initialized) {
67+
if (throttle) {
68+
next_target = (cur_state + 1) >= instance->upper ?
69+
instance->upper :
70+
((cur_state + 1) < instance->lower ?
71+
instance->lower : (cur_state + 1));
72+
} else {
73+
next_target = THERMAL_NO_TARGET;
74+
}
75+
76+
return next_target;
77+
}
78+
6679
switch (trend) {
6780
case THERMAL_TREND_RAISING:
6881
if (throttle) {
@@ -149,7 +162,7 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
149162
dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
150163
old_target, (int)instance->target);
151164

152-
if (old_target == instance->target)
165+
if (instance->initialized && old_target == instance->target)
153166
continue;
154167

155168
/* Activate a passive thermal instance */
@@ -161,7 +174,7 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
161174
instance->target == THERMAL_NO_TARGET)
162175
update_passive_instance(tz, trip_type, -1);
163176

164-
177+
instance->initialized = true;
165178
instance->cdev->updated = false; /* cdev needs update */
166179
}
167180

drivers/thermal/thermal_core.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,22 @@ static void update_temperature(struct thermal_zone_device *tz)
471471
mutex_unlock(&tz->lock);
472472

473473
trace_thermal_temperature(tz);
474-
dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
475-
tz->last_temperature, tz->temperature);
474+
if (tz->last_temperature == THERMAL_TEMP_INVALID)
475+
dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
476+
tz->temperature);
477+
else
478+
dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
479+
tz->last_temperature, tz->temperature);
480+
}
481+
482+
static void thermal_zone_device_reset(struct thermal_zone_device *tz)
483+
{
484+
struct thermal_instance *pos;
485+
486+
tz->temperature = THERMAL_TEMP_INVALID;
487+
tz->passive = 0;
488+
list_for_each_entry(pos, &tz->thermal_instances, tz_node)
489+
pos->initialized = false;
476490
}
477491

478492
void thermal_zone_device_update(struct thermal_zone_device *tz)
@@ -1576,6 +1590,7 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
15761590
if (!tz->ops->get_temp)
15771591
thermal_zone_device_set_polling(tz, 0);
15781592

1593+
thermal_zone_device_reset(tz);
15791594
thermal_zone_device_update(tz);
15801595

15811596
return tz;

drivers/thermal/thermal_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct thermal_instance {
4141
struct thermal_zone_device *tz;
4242
struct thermal_cooling_device *cdev;
4343
int trip;
44+
bool initialized;
4445
unsigned long upper; /* Highest cooling state for this trip point */
4546
unsigned long lower; /* Lowest cooling state for this trip point */
4647
unsigned long target; /* expected cooling state */

include/linux/thermal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
/* No upper/lower limit requirement */
4141
#define THERMAL_NO_LIMIT ((u32)~0)
4242

43+
/* use value, which < 0K, to indicate an invalid/uninitialized temperature */
44+
#define THERMAL_TEMP_INVALID -274000
45+
4346
/* Unit conversion macros */
4447
#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732 >= 0) ? \
4548
((long)t-2732+5)/10 : ((long)t-2732-5)/10)

0 commit comments

Comments
 (0)