|
| 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