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 
thermal_zone_trip_update(struct thermal_zone_device * tz,int trip_index)16 static int thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_index)
17 {
18 	const struct thermal_trip *trip = &tz->trips[trip_index];
19 	struct thermal_instance *instance;
20 
21 	if (!trip->hysteresis)
22 		dev_info_once(&tz->device,
23 			      "Zero hysteresis value for thermal zone %s\n", tz->type);
24 
25 	dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
26 				trip_index, trip->temperature, tz->temperature,
27 				trip->hysteresis);
28 
29 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
30 		if (instance->trip != trip)
31 			continue;
32 
33 		/* in case fan is in initial state, switch the fan off */
34 		if (instance->target == THERMAL_NO_TARGET)
35 			instance->target = 0;
36 
37 		/* in case fan is neither on nor off set the fan to active */
38 		if (instance->target != 0 && instance->target != 1) {
39 			pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
40 					instance->name, instance->target);
41 			instance->target = 1;
42 		}
43 
44 		/*
45 		 * enable fan when temperature exceeds trip_temp and disable
46 		 * the fan in case it falls below trip_temp minus hysteresis
47 		 */
48 		if (instance->target == 0 && tz->temperature >= trip->temperature)
49 			instance->target = 1;
50 		else if (instance->target == 1 &&
51 			 tz->temperature <= trip->temperature - trip->hysteresis)
52 			instance->target = 0;
53 
54 		dev_dbg(&instance->cdev->device, "target=%d\n",
55 					(int)instance->target);
56 
57 		mutex_lock(&instance->cdev->lock);
58 		instance->cdev->updated = false; /* cdev needs update */
59 		mutex_unlock(&instance->cdev->lock);
60 	}
61 
62 	return 0;
63 }
64 
65 /**
66  * bang_bang_control - controls devices associated with the given zone
67  * @tz: thermal_zone_device
68  * @trip: the trip point
69  *
70  * Regulation Logic: a two point regulation, deliver cooling state depending
71  * on the previous state shown in this diagram:
72  *
73  *                Fan:   OFF    ON
74  *
75  *                              |
76  *                              |
77  *          trip_temp:    +---->+
78  *                        |     |        ^
79  *                        |     |        |
80  *                        |     |   Temperature
81  * (trip_temp - hyst):    +<----+
82  *                        |
83  *                        |
84  *                        |
85  *
86  *   * If the fan is not running and temperature exceeds trip_temp, the fan
87  *     gets turned on.
88  *   * In case the fan is running, temperature must fall below
89  *     (trip_temp - hyst) so that the fan gets turned off again.
90  *
91  */
bang_bang_control(struct thermal_zone_device * tz,int trip)92 static int bang_bang_control(struct thermal_zone_device *tz, int trip)
93 {
94 	struct thermal_instance *instance;
95 	int ret;
96 
97 	lockdep_assert_held(&tz->lock);
98 
99 	ret = thermal_zone_trip_update(tz, trip);
100 	if (ret)
101 		return ret;
102 
103 	list_for_each_entry(instance, &tz->thermal_instances, tz_node)
104 		thermal_cdev_update(instance->cdev);
105 
106 	return 0;
107 }
108 
109 static struct thermal_governor thermal_gov_bang_bang = {
110 	.name		= "bang_bang",
111 	.throttle	= bang_bang_control,
112 };
113 THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
114