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 int thermal_zone_trip_update(struct thermal_zone_device *tz,
17 const struct thermal_trip *trip)
18{
19 int trip_index = thermal_zone_trip_id(tz, trip);
20 struct thermal_instance *instance;
21
22 if (!trip->hysteresis)
23 dev_info_once(&tz->device,
24 "Zero hysteresis value for thermal zone %s\n", tz->type);
25
26 dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
27 trip_index, trip->temperature, tz->temperature,
28 trip->hysteresis);
29
30 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
31 if (instance->trip != trip)
32 continue;
33
34 /* in case fan is in initial state, switch the fan off */
35 if (instance->target == THERMAL_NO_TARGET)
36 instance->target = 0;
37
38 /* in case fan is neither on nor off set the fan to active */
39 if (instance->target != 0 && instance->target != 1) {
40 pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
41 instance->name, instance->target);
42 instance->target = 1;
43 }
44
45 /*
46 * enable fan when temperature exceeds trip_temp and disable
47 * the fan in case it falls below trip_temp minus hysteresis
48 */
49 if (instance->target == 0 && tz->temperature >= trip->temperature)
50 instance->target = 1;
51 else if (instance->target == 1 &&
52 tz->temperature <= trip->temperature - trip->hysteresis)
53 instance->target = 0;
54
55 dev_dbg(&instance->cdev->device, "target=%d\n",
56 (int)instance->target);
57
58 mutex_lock(&instance->cdev->lock);
59 instance->cdev->updated = false; /* cdev needs update */
60 mutex_unlock(&instance->cdev->lock);
61 }
62
63 return 0;
64}
65
66/**
67 * bang_bang_control - controls devices associated with the given zone
68 * @tz: thermal_zone_device
69 * @trip: the trip point
70 *
71 * Regulation Logic: a two point regulation, deliver cooling state depending
72 * on the previous state shown in this diagram:
73 *
74 * Fan: OFF ON
75 *
76 * |
77 * |
78 * trip_temp: +---->+
79 * | | ^
80 * | | |
81 * | | Temperature
82 * (trip_temp - hyst): +<----+
83 * |
84 * |
85 * |
86 *
87 * * If the fan is not running and temperature exceeds trip_temp, the fan
88 * gets turned on.
89 * * In case the fan is running, temperature must fall below
90 * (trip_temp - hyst) so that the fan gets turned off again.
91 *
92 */
93static int bang_bang_control(struct thermal_zone_device *tz,
94 const struct thermal_trip *trip)
95{
96 struct thermal_instance *instance;
97 int ret;
98
99 lockdep_assert_held(&tz->lock);
100
101 ret = thermal_zone_trip_update(tz, trip);
102 if (ret)
103 return ret;
104
105 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
106 thermal_cdev_update(instance->cdev);
107
108 return 0;
109}
110
111static struct thermal_governor thermal_gov_bang_bang = {
112 .name = "bang_bang",
113 .throttle = bang_bang_control,
114};
115THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
1/*
2 * gov_bang_bang.c - A simple thermal throttling governor using hysteresis
3 *
4 * Copyright (C) 2014 Peter Feuerer <peter@piie.net>
5 *
6 * Based on step_wise.c with following Copyrights:
7 * Copyright (C) 2012 Intel Corp
8 * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, version 2.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU General Public License for more details.
19 *
20 */
21
22#include <linux/thermal.h>
23
24#include "thermal_core.h"
25
26static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
27{
28 int trip_temp, trip_hyst;
29 struct thermal_instance *instance;
30
31 tz->ops->get_trip_temp(tz, trip, &trip_temp);
32 tz->ops->get_trip_hyst(tz, trip, &trip_hyst);
33
34 dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
35 trip, trip_temp, tz->temperature,
36 trip_hyst);
37
38 mutex_lock(&tz->lock);
39
40 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
41 if (instance->trip != trip)
42 continue;
43
44 /* in case fan is in initial state, switch the fan off */
45 if (instance->target == THERMAL_NO_TARGET)
46 instance->target = 0;
47
48 /* in case fan is neither on nor off set the fan to active */
49 if (instance->target != 0 && instance->target != 1) {
50 pr_warn("Thermal instance %s controlled by bang-bang has unexpected state: %ld\n",
51 instance->name, instance->target);
52 instance->target = 1;
53 }
54
55 /*
56 * enable fan when temperature exceeds trip_temp and disable
57 * the fan in case it falls below trip_temp minus hysteresis
58 */
59 if (instance->target == 0 && tz->temperature >= trip_temp)
60 instance->target = 1;
61 else if (instance->target == 1 &&
62 tz->temperature < trip_temp - trip_hyst)
63 instance->target = 0;
64
65 dev_dbg(&instance->cdev->device, "target=%d\n",
66 (int)instance->target);
67
68 instance->cdev->updated = false; /* cdev needs update */
69 }
70
71 mutex_unlock(&tz->lock);
72}
73
74/**
75 * bang_bang_control - controls devices associated with the given zone
76 * @tz - thermal_zone_device
77 * @trip - the trip point
78 *
79 * Regulation Logic: a two point regulation, deliver cooling state depending
80 * on the previous state shown in this diagram:
81 *
82 * Fan: OFF ON
83 *
84 * |
85 * |
86 * trip_temp: +---->+
87 * | | ^
88 * | | |
89 * | | Temperature
90 * (trip_temp - hyst): +<----+
91 * |
92 * |
93 * |
94 *
95 * * If the fan is not running and temperature exceeds trip_temp, the fan
96 * gets turned on.
97 * * In case the fan is running, temperature must fall below
98 * (trip_temp - hyst) so that the fan gets turned off again.
99 *
100 */
101static int bang_bang_control(struct thermal_zone_device *tz, int trip)
102{
103 struct thermal_instance *instance;
104
105 thermal_zone_trip_update(tz, trip);
106
107 mutex_lock(&tz->lock);
108
109 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
110 thermal_cdev_update(instance->cdev);
111
112 mutex_unlock(&tz->lock);
113
114 return 0;
115}
116
117static struct thermal_governor thermal_gov_bang_bang = {
118 .name = "bang_bang",
119 .throttle = bang_bang_control,
120};
121
122int thermal_gov_bang_bang_register(void)
123{
124 return thermal_register_governor(&thermal_gov_bang_bang);
125}
126
127void thermal_gov_bang_bang_unregister(void)
128{
129 thermal_unregister_governor(&thermal_gov_bang_bang);
130}