| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* gov_bang_bang.c - A simple thermal throttling governor using hysteresis |
| 4 |
* |
| 5 |
* Copyright (C) 2014 Peter Kaestle <[email protected]> |
| 6 |
* |
| 7 |
* Based on step_wise.c with following Copyrights: |
| 8 |
* Copyright (C) 2012 Intel Corp |
| 9 |
* Copyright (C) 2012 Durgadoss R <[email protected]> |
| 10 |
* |
| 11 |
* Regulation Logic: a two point regulation, deliver cooling state depending |
| 12 |
* on the previous state shown in this diagram: |
| 13 |
* |
| 14 |
* Fan: OFF ON |
| 15 |
* |
| 16 |
* | |
| 17 |
* | |
| 18 |
* trip_temp: +---->+ |
| 19 |
* | | ^ |
| 20 |
* | | | |
| 21 |
* | | Temperature |
| 22 |
* (trip_temp - hyst): +<----+ |
| 23 |
* | |
| 24 |
* | |
| 25 |
* | |
| 26 |
* |
| 27 |
* * If the fan is not running and temperature exceeds trip_temp, the fan |
| 28 |
* gets turned on. |
| 29 |
* * In case the fan is running, temperature must fall below |
| 30 |
* (trip_temp - hyst) so that the fan gets turned off again. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <linux/thermal.h> |
| 34 |
|
| 35 |
#include "thermal_core.h" |
| 36 |
|
| 37 |
static void bang_bang_set_instance_target(struct thermal_instance *instance, |
| 38 |
unsigned int target) |
| 39 |
{ |
| 40 |
if (instance->target != 0 && instance->target != 1 && |
| 41 |
instance->target != THERMAL_NO_TARGET) |
| 42 |
pr_debug("Unexpected state %ld of thermal instance %s in bang-bang\n", |
| 43 |
instance->target, instance->name); |
| 44 |
|
| 45 |
/* |
| 46 |
* Enable the fan when the trip is crossed on the way up and disable it |
| 47 |
* when the trip is crossed on the way down. |
| 48 |
*/ |
| 49 |
instance->target = target; |
| 50 |
instance->initialized = true; |
| 51 |
|
| 52 |
dev_dbg(&instance->cdev->device, "target=%ld\n", instance->target); |
| 53 |
|
| 54 |
thermal_cdev_update_nocheck(instance->cdev); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* bang_bang_trip_crossed - controls devices associated with the given zone |
| 59 |
* @tz: thermal_zone_device |
| 60 |
* @trip: the trip point |
| 61 |
* @upward: whether or not the trip has been crossed on the way up |
| 62 |
*/ |
| 63 |
static void bang_bang_trip_crossed(struct thermal_zone_device *tz, |
| 64 |
const struct thermal_trip *trip, |
| 65 |
bool upward) |
| 66 |
{ |
| 67 |
const struct thermal_trip_desc *td = trip_to_trip_desc(trip); |
| 68 |
struct thermal_instance *instance; |
| 69 |
|
| 70 |
lockdep_assert_held(&tz->lock); |
| 71 |
|
| 72 |
dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n", |
| 73 |
thermal_zone_trip_id(tz, trip), trip->temperature, |
| 74 |
tz->temperature, trip->hysteresis); |
| 75 |
|
| 76 |
list_for_each_entry(instance, &td->thermal_instances, trip_node) |
| 77 |
bang_bang_set_instance_target(instance, upward); |
| 78 |
} |
| 79 |
|
| 80 |
static void bang_bang_manage(struct thermal_zone_device *tz) |
| 81 |
{ |
| 82 |
const struct thermal_trip_desc *td; |
| 83 |
struct thermal_instance *instance; |
| 84 |
|
| 85 |
/* If the code below has run already, nothing needs to be done. */ |
| 86 |
if (tz->governor_data) |
| 87 |
return; |
| 88 |
|
| 89 |
for_each_trip_desc(tz, td) { |
| 90 |
const struct thermal_trip *trip = &td->trip; |
| 91 |
bool turn_on; |
| 92 |
|
| 93 |
if (trip->temperature == THERMAL_TEMP_INVALID || |
| 94 |
trip->type == THERMAL_TRIP_CRITICAL || |
| 95 |
trip->type == THERMAL_TRIP_HOT) |
| 96 |
continue; |
| 97 |
|
| 98 |
/* |
| 99 |
* Adjust the target states for uninitialized thermal instances |
| 100 |
* to the thermal zone temperature and the trip point threshold. |
| 101 |
*/ |
| 102 |
turn_on = tz->temperature >= td->threshold; |
| 103 |
list_for_each_entry(instance, &td->thermal_instances, trip_node) { |
| 104 |
if (!instance->initialized) |
| 105 |
bang_bang_set_instance_target(instance, turn_on); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
tz->governor_data = (void *)true; |
| 110 |
} |
| 111 |
|
| 112 |
static void bang_bang_update_tz(struct thermal_zone_device *tz, |
| 113 |
enum thermal_notify_event reason) |
| 114 |
{ |
| 115 |
/* |
| 116 |
* Let bang_bang_manage() know that it needs to walk trips after binding |
| 117 |
* a new cdev and after system resume. |
| 118 |
*/ |
| 119 |
if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME) |
| 120 |
tz->governor_data = NULL; |
| 121 |
} |
| 122 |
|
| 123 |
static struct thermal_governor thermal_gov_bang_bang = { |
| 124 |
.name = "bang_bang", |
| 125 |
.trip_crossed = bang_bang_trip_crossed, |
| 126 |
.manage = bang_bang_manage, |
| 127 |
.update_tz = bang_bang_update_tz, |
| 128 |
}; |
| 129 |
THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang); |