Skip to content

Commit 338b5d7

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
Thermal: Support step_wise governor
Signed-off-by: wangjianyu3 <[email protected]>
1 parent d1b87bd commit 338b5d7

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

drivers/thermal/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ config THERMAL_DEFAULT_GOVERNOR
1717
---help---
1818
Default governor name.
1919

20+
config THERMAL_GOVERNOR_STEP_WISE
21+
bool "Enable step wise governor"
22+
default y
23+
---help---
24+
Enable step wise governor.
25+
2026
endif # THERMAL

drivers/thermal/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ ifeq ($(CONFIG_THERMAL),y)
2424

2525
CSRCS += thermal_core.c
2626

27+
ifeq ($(CONFIG_THERMAL_GOVERNOR_STEP_WISE),y)
28+
CSRCS += thermal_step_wise.c
29+
endif
30+
2731
DEPPATH += --dep-path thermal
2832
VPATH += thermal
2933

drivers/thermal/thermal_core.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,5 +839,14 @@ int thermal_init(void)
839839
{
840840
int ret = OK;
841841

842+
#ifdef CONFIG_THERMAL_GOVERNOR_STEP_WISE
843+
ret = thermal_register_step_wise_governor();
844+
if (ret < 0)
845+
{
846+
therr("Register step wise governor failed!\n");
847+
return ret;
848+
}
849+
#endif
850+
842851
return ret;
843852
}

drivers/thermal/thermal_core.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,8 @@ int thermal_zone_get_trip_hyst(FAR struct thermal_zone_device_s *zdev,
7373
int trip,
7474
FAR int *hyst);
7575

76+
/* Governor */
77+
78+
int thermal_register_step_wise_governor(void);
79+
7680
#endif /* __DRIVERS_THERMAL_THERMAL_CORE_H */

drivers/thermal/thermal_step_wise.c

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/****************************************************************************
2+
* drivers/thermal/thermal_step_wise.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <debug.h>
26+
27+
#include "thermal_core.h"
28+
29+
/****************************************************************************
30+
* Private Function Prototypes
31+
****************************************************************************/
32+
33+
static unsigned int get_target_state(FAR struct thermal_instance_s *instance,
34+
enum thermal_trend_e trend,
35+
bool throttle);
36+
static int step_wise_throttle(FAR struct thermal_zone_device_s *zdev,
37+
int trip);
38+
39+
/****************************************************************************
40+
* Private Data
41+
****************************************************************************/
42+
43+
static struct thermal_governor_s g_step_wise_governor =
44+
{
45+
.name = "step_wise",
46+
.throttle = step_wise_throttle,
47+
};
48+
49+
/****************************************************************************
50+
* Private Functions
51+
****************************************************************************/
52+
53+
static inline unsigned int
54+
validate_state(FAR struct thermal_instance_s *instance, bool throttle,
55+
unsigned int state, int value)
56+
{
57+
if ((state == THERMAL_TARGET_MIN && value == -1) ||
58+
(state == THERMAL_TARGET_MAX && value == 1))
59+
{
60+
value = 0;
61+
}
62+
63+
if (state == THERMAL_NO_TARGET)
64+
{
65+
state = 0;
66+
}
67+
68+
state += value;
69+
70+
if (state > instance->upper)
71+
{
72+
state = instance->upper;
73+
}
74+
else if(state < instance->lower && throttle)
75+
{
76+
state = instance->lower;
77+
}
78+
79+
return state;
80+
}
81+
82+
static unsigned int get_target_state(FAR struct thermal_instance_s *instance,
83+
enum thermal_trend_e trend,
84+
bool throttle)
85+
{
86+
FAR struct thermal_cooling_device_s *cdev = instance->cdev;
87+
unsigned int next_state = THERMAL_NO_TARGET;
88+
unsigned int cur_state = instance->target;
89+
90+
if (!cdev->ops || !cdev->ops->get_state)
91+
{
92+
return next_state;
93+
}
94+
95+
if (cur_state == THERMAL_NO_TARGET)
96+
{
97+
if (throttle)
98+
{
99+
next_state = validate_state(instance, throttle, cur_state, 1);
100+
}
101+
102+
return next_state;
103+
}
104+
105+
/* Update Cooling State */
106+
107+
switch (trend)
108+
{
109+
case THERMAL_TREND_RAISING:
110+
if (throttle)
111+
{
112+
next_state = validate_state(instance, throttle, cur_state, 1);
113+
}
114+
break;
115+
116+
case THERMAL_TREND_DROPPING:
117+
if (!throttle)
118+
{
119+
next_state = validate_state(instance, throttle, cur_state, -1);
120+
}
121+
break;
122+
123+
case THERMAL_TREND_STABLE:
124+
break;
125+
126+
default:
127+
break;
128+
}
129+
130+
return next_state;
131+
}
132+
133+
/* step_wise */
134+
135+
static int step_wise_throttle(FAR struct thermal_zone_device_s *zdev,
136+
int trip)
137+
{
138+
FAR struct thermal_instance_s *instance;
139+
enum thermal_trend_e trend;
140+
unsigned int next_state;
141+
bool throttle = false;
142+
int trip_temp;
143+
144+
thermal_zone_get_trip_temp(zdev, trip, &trip_temp);
145+
146+
trend = thermal_zone_get_trend(zdev);
147+
148+
if (zdev->temperature > trip_temp)
149+
{
150+
throttle = true;
151+
}
152+
153+
list_for_every_entry(&zdev->instance_list, instance,
154+
struct thermal_instance_s, zdev_node)
155+
{
156+
if (instance->trip != trip)
157+
{
158+
continue;
159+
}
160+
161+
next_state = get_target_state(instance, trend, throttle);
162+
163+
if (next_state == THERMAL_NO_TARGET || next_state == instance->target)
164+
{
165+
continue;
166+
}
167+
168+
instance->target = next_state;
169+
thermal_cooling_device_update(instance->cdev);
170+
}
171+
172+
return OK;
173+
}
174+
175+
/****************************************************************************
176+
* Public Functions
177+
****************************************************************************/
178+
179+
int thermal_register_step_wise_governor(void)
180+
{
181+
return thermal_register_governor(&g_step_wise_governor);
182+
}

0 commit comments

Comments
 (0)