xref: /linux/drivers/thermal/gov_bang_bang.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
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 
16 static 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 	mutex_lock(&instance->cdev->lock);
34 	__thermal_cdev_update(instance->cdev);
35 	mutex_unlock(&instance->cdev->lock);
36 }
37 
38 /**
39  * bang_bang_control - controls devices associated with the given zone
40  * @tz: thermal_zone_device
41  * @trip: the trip point
42  * @crossed_up: whether or not the trip has been crossed on the way up
43  *
44  * Regulation Logic: a two point regulation, deliver cooling state depending
45  * on the previous state shown in this diagram:
46  *
47  *                Fan:   OFF    ON
48  *
49  *                              |
50  *                              |
51  *          trip_temp:    +---->+
52  *                        |     |        ^
53  *                        |     |        |
54  *                        |     |   Temperature
55  * (trip_temp - hyst):    +<----+
56  *                        |
57  *                        |
58  *                        |
59  *
60  *   * If the fan is not running and temperature exceeds trip_temp, the fan
61  *     gets turned on.
62  *   * In case the fan is running, temperature must fall below
63  *     (trip_temp - hyst) so that the fan gets turned off again.
64  *
65  */
66 static void bang_bang_control(struct thermal_zone_device *tz,
67 			      const struct thermal_trip *trip,
68 			      bool crossed_up)
69 {
70 	struct thermal_instance *instance;
71 
72 	lockdep_assert_held(&tz->lock);
73 
74 	dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
75 		thermal_zone_trip_id(tz, trip), trip->temperature,
76 		tz->temperature, trip->hysteresis);
77 
78 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
79 		if (instance->trip == trip)
80 			bang_bang_set_instance_target(instance, crossed_up);
81 	}
82 }
83 
84 static void bang_bang_manage(struct thermal_zone_device *tz)
85 {
86 	const struct thermal_trip_desc *td;
87 	struct thermal_instance *instance;
88 
89 	/* If the code below has run already, nothing needs to be done. */
90 	if (tz->governor_data)
91 		return;
92 
93 	for_each_trip_desc(tz, td) {
94 		const struct thermal_trip *trip = &td->trip;
95 
96 		if (tz->temperature >= td->threshold ||
97 		    trip->temperature == THERMAL_TEMP_INVALID ||
98 		    trip->type == THERMAL_TRIP_CRITICAL ||
99 		    trip->type == THERMAL_TRIP_HOT)
100 			continue;
101 
102 		/*
103 		 * If the initial cooling device state is "on", but the zone
104 		 * temperature is not above the trip point, the core will not
105 		 * call bang_bang_control() until the zone temperature reaches
106 		 * the trip point temperature which may be never.  In those
107 		 * cases, set the initial state of the cooling device to 0.
108 		 */
109 		list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
110 			if (!instance->initialized && instance->trip == trip)
111 				bang_bang_set_instance_target(instance, 0);
112 		}
113 	}
114 
115 	tz->governor_data = (void *)true;
116 }
117 
118 static void bang_bang_update_tz(struct thermal_zone_device *tz,
119 				enum thermal_notify_event reason)
120 {
121 	/*
122 	 * Let bang_bang_manage() know that it needs to walk trips after binding
123 	 * a new cdev and after system resume.
124 	 */
125 	if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
126 		tz->governor_data = NULL;
127 }
128 
129 static struct thermal_governor thermal_gov_bang_bang = {
130 	.name		= "bang_bang",
131 	.trip_crossed	= bang_bang_control,
132 	.manage		= bang_bang_manage,
133 	.update_tz	= bang_bang_update_tz,
134 };
135 THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
136