Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[platform/kernel/linux-starfive.git] / drivers / thermal / gov_bang_bang.c
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 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         list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
35                 if (instance->trip != trip)
36                         continue;
37
38                 /* in case fan is in initial state, switch the fan off */
39                 if (instance->target == THERMAL_NO_TARGET)
40                         instance->target = 0;
41
42                 /* in case fan is neither on nor off set the fan to active */
43                 if (instance->target != 0 && instance->target != 1) {
44                         pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
45                                         instance->name, instance->target);
46                         instance->target = 1;
47                 }
48
49                 /*
50                  * enable fan when temperature exceeds trip_temp and disable
51                  * the fan in case it falls below trip_temp minus hysteresis
52                  */
53                 if (instance->target == 0 && tz->temperature >= trip_temp)
54                         instance->target = 1;
55                 else if (instance->target == 1 &&
56                                 tz->temperature <= trip_temp - trip_hyst)
57                         instance->target = 0;
58
59                 dev_dbg(&instance->cdev->device, "target=%d\n",
60                                         (int)instance->target);
61
62                 mutex_lock(&instance->cdev->lock);
63                 instance->cdev->updated = false; /* cdev needs update */
64                 mutex_unlock(&instance->cdev->lock);
65         }
66 }
67
68 /**
69  * bang_bang_control - controls devices associated with the given zone
70  * @tz: thermal_zone_device
71  * @trip: the trip point
72  *
73  * Regulation Logic: a two point regulation, deliver cooling state depending
74  * on the previous state shown in this diagram:
75  *
76  *                Fan:   OFF    ON
77  *
78  *                              |
79  *                              |
80  *          trip_temp:    +---->+
81  *                        |     |        ^
82  *                        |     |        |
83  *                        |     |   Temperature
84  * (trip_temp - hyst):    +<----+
85  *                        |
86  *                        |
87  *                        |
88  *
89  *   * If the fan is not running and temperature exceeds trip_temp, the fan
90  *     gets turned on.
91  *   * In case the fan is running, temperature must fall below
92  *     (trip_temp - hyst) so that the fan gets turned off again.
93  *
94  */
95 static int bang_bang_control(struct thermal_zone_device *tz, int trip)
96 {
97         struct thermal_instance *instance;
98
99         lockdep_assert_held(&tz->lock);
100
101         thermal_zone_trip_update(tz, trip);
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);