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