• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
bang_bang_set_instance_target(struct thermal_instance * instance,unsigned int target)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  */
bang_bang_control(struct thermal_zone_device * tz,const struct thermal_trip * trip,bool crossed_up)66 static void bang_bang_control(struct thermal_zone_device *tz,
67 			      const struct thermal_trip *trip,
68 			      bool crossed_up)
69 {
70 	const struct thermal_trip_desc *td = trip_to_trip_desc(trip);
71 	struct thermal_instance *instance;
72 
73 	lockdep_assert_held(&tz->lock);
74 
75 	dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
76 		thermal_zone_trip_id(tz, trip), trip->temperature,
77 		tz->temperature, trip->hysteresis);
78 
79 	list_for_each_entry(instance, &td->thermal_instances, trip_node)
80 		bang_bang_set_instance_target(instance, crossed_up);
81 }
82 
bang_bang_manage(struct thermal_zone_device * tz)83 static void bang_bang_manage(struct thermal_zone_device *tz)
84 {
85 	const struct thermal_trip_desc *td;
86 	struct thermal_instance *instance;
87 
88 	/* If the code below has run already, nothing needs to be done. */
89 	if (tz->governor_data)
90 		return;
91 
92 	for_each_trip_desc(tz, td) {
93 		const struct thermal_trip *trip = &td->trip;
94 		bool turn_on;
95 
96 		if (trip->temperature == THERMAL_TEMP_INVALID ||
97 		    trip->type == THERMAL_TRIP_CRITICAL ||
98 		    trip->type == THERMAL_TRIP_HOT)
99 			continue;
100 
101 		/*
102 		 * Adjust the target states for uninitialized thermal instances
103 		 * to the thermal zone temperature and the trip point threshold.
104 		 */
105 		turn_on = tz->temperature >= td->threshold;
106 		list_for_each_entry(instance, &td->thermal_instances, trip_node) {
107 			if (!instance->initialized)
108 				bang_bang_set_instance_target(instance, turn_on);
109 		}
110 	}
111 
112 	tz->governor_data = (void *)true;
113 }
114 
bang_bang_update_tz(struct thermal_zone_device * tz,enum thermal_notify_event reason)115 static void bang_bang_update_tz(struct thermal_zone_device *tz,
116 				enum thermal_notify_event reason)
117 {
118 	/*
119 	 * Let bang_bang_manage() know that it needs to walk trips after binding
120 	 * a new cdev and after system resume.
121 	 */
122 	if (reason == THERMAL_TZ_BIND_CDEV || reason == THERMAL_TZ_RESUME)
123 		tz->governor_data = NULL;
124 }
125 
126 static struct thermal_governor thermal_gov_bang_bang = {
127 	.name		= "bang_bang",
128 	.trip_crossed	= bang_bang_control,
129 	.manage		= bang_bang_manage,
130 	.update_tz	= bang_bang_update_tz,
131 };
132 THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
133