Commit | Line | Data |
---|---|---|
50acfb2b | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e4dbf98f PF |
2 | /* |
3 | * gov_bang_bang.c - A simple thermal throttling governor using hysteresis | |
4 | * | |
d3f5b736 | 5 | * Copyright (C) 2014 Peter Kaestle <peter@piie.net> |
e4dbf98f PF |
6 | * |
7 | * Based on step_wise.c with following Copyrights: | |
8 | * Copyright (C) 2012 Intel Corp | |
9 | * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com> | |
e4dbf98f PF |
10 | */ |
11 | ||
12 | #include <linux/thermal.h> | |
13 | ||
14 | #include "thermal_core.h" | |
15 | ||
7f725a23 | 16 | static int thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_id) |
e4dbf98f | 17 | { |
7f725a23 | 18 | struct thermal_trip trip; |
e4dbf98f | 19 | struct thermal_instance *instance; |
7f725a23 | 20 | int ret; |
e4dbf98f | 21 | |
7f725a23 DL |
22 | ret = __thermal_zone_get_trip(tz, trip_id, &trip); |
23 | if (ret) { | |
24 | pr_warn_once("Failed to retrieve trip point %d\n", trip_id); | |
25 | return ret; | |
26 | } | |
191b0754 | 27 | |
7f725a23 DL |
28 | if (!trip.hysteresis) |
29 | dev_info_once(&tz->device, | |
30 | "Zero hysteresis value for thermal zone %s\n", tz->type); | |
e4dbf98f | 31 | |
17e8351a | 32 | dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n", |
7f725a23 DL |
33 | trip_id, trip.temperature, tz->temperature, |
34 | trip.hysteresis); | |
e4dbf98f | 35 | |
e4dbf98f | 36 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
7f725a23 | 37 | if (instance->trip != trip_id) |
e4dbf98f PF |
38 | continue; |
39 | ||
40 | /* in case fan is in initial state, switch the fan off */ | |
41 | if (instance->target == THERMAL_NO_TARGET) | |
42 | instance->target = 0; | |
43 | ||
44 | /* in case fan is neither on nor off set the fan to active */ | |
45 | if (instance->target != 0 && instance->target != 1) { | |
46 | pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n", | |
47 | instance->name, instance->target); | |
48 | instance->target = 1; | |
49 | } | |
50 | ||
51 | /* | |
52 | * enable fan when temperature exceeds trip_temp and disable | |
53 | * the fan in case it falls below trip_temp minus hysteresis | |
54 | */ | |
7f725a23 | 55 | if (instance->target == 0 && tz->temperature >= trip.temperature) |
e4dbf98f PF |
56 | instance->target = 1; |
57 | else if (instance->target == 1 && | |
7f725a23 | 58 | tz->temperature <= trip.temperature - trip.hysteresis) |
e4dbf98f PF |
59 | instance->target = 0; |
60 | ||
61 | dev_dbg(&instance->cdev->device, "target=%d\n", | |
62 | (int)instance->target); | |
63 | ||
d0b7306d | 64 | mutex_lock(&instance->cdev->lock); |
e4dbf98f | 65 | instance->cdev->updated = false; /* cdev needs update */ |
d0b7306d | 66 | mutex_unlock(&instance->cdev->lock); |
e4dbf98f | 67 | } |
7f725a23 DL |
68 | |
69 | return 0; | |
e4dbf98f PF |
70 | } |
71 | ||
72 | /** | |
73 | * bang_bang_control - controls devices associated with the given zone | |
53d256e7 AK |
74 | * @tz: thermal_zone_device |
75 | * @trip: the trip point | |
e4dbf98f PF |
76 | * |
77 | * Regulation Logic: a two point regulation, deliver cooling state depending | |
78 | * on the previous state shown in this diagram: | |
79 | * | |
80 | * Fan: OFF ON | |
81 | * | |
82 | * | | |
83 | * | | |
84 | * trip_temp: +---->+ | |
85 | * | | ^ | |
86 | * | | | | |
87 | * | | Temperature | |
88 | * (trip_temp - hyst): +<----+ | |
89 | * | | |
90 | * | | |
91 | * | | |
92 | * | |
93 | * * If the fan is not running and temperature exceeds trip_temp, the fan | |
94 | * gets turned on. | |
95 | * * In case the fan is running, temperature must fall below | |
96 | * (trip_temp - hyst) so that the fan gets turned off again. | |
97 | * | |
98 | */ | |
99 | static int bang_bang_control(struct thermal_zone_device *tz, int trip) | |
100 | { | |
101 | struct thermal_instance *instance; | |
7f725a23 | 102 | int ret; |
e4dbf98f | 103 | |
670a5e35 | 104 | lockdep_assert_held(&tz->lock); |
e4dbf98f | 105 | |
7f725a23 DL |
106 | ret = thermal_zone_trip_update(tz, trip); |
107 | if (ret) | |
108 | return ret; | |
63561fe3 | 109 | |
e4dbf98f PF |
110 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) |
111 | thermal_cdev_update(instance->cdev); | |
112 | ||
e4dbf98f PF |
113 | return 0; |
114 | } | |
115 | ||
116 | static struct thermal_governor thermal_gov_bang_bang = { | |
117 | .name = "bang_bang", | |
118 | .throttle = bang_bang_control, | |
119 | }; | |
57c5b2ec | 120 | THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang); |