drm/gem: move drm_gem_object_handle_unreference_unlocked into drm_gem.c
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / thermal / step_wise.c
1 /*
2  *  step_wise.c - A step-by-step Thermal throttling governor
3  *
4  *  Copyright (C) 2012 Intel Corp
5  *  Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
6  *
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; version 2 of the License.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23  */
24
25 #include <linux/thermal.h>
26
27 #include "thermal_core.h"
28
29 /*
30  * If the temperature is higher than a trip point,
31  *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
32  *       state for this trip point
33  *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
34  *       state for this trip point
35  *    c. if the trend is THERMAL_TREND_RAISE_FULL, use upper limit
36  *       for this trip point
37  *    d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
38  *       for this trip point
39  * If the temperature is lower than a trip point,
40  *    a. if the trend is THERMAL_TREND_RAISING, do nothing
41  *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
42  *       state for this trip point, if the cooling state already
43  *       equals lower limit, deactivate the thermal instance
44  *    c. if the trend is THERMAL_TREND_RAISE_FULL, do nothing
45  *    d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit,
46  *       if the cooling state already equals lower limit,
47  *       deactive the thermal instance
48  */
49 #if 0
50 static unsigned long get_target_state(struct thermal_instance *instance,
51                                 enum thermal_trend trend, bool throttle)
52 {
53         struct thermal_cooling_device *cdev = instance->cdev;
54         unsigned long cur_state;
55
56         cdev->ops->get_cur_state(cdev, &cur_state);
57         dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
58
59         switch (trend) {
60         case THERMAL_TREND_RAISING:
61                 if (throttle) {
62                         cur_state = cur_state < instance->upper ?
63                                     (cur_state + 1) : instance->upper;
64                         if (cur_state < instance->lower)
65                                 cur_state = instance->lower;
66                 }
67                 break;
68         case THERMAL_TREND_RAISE_FULL:
69                 if (throttle)
70                         cur_state = instance->upper;
71                 break;
72         case THERMAL_TREND_DROPPING:
73                 if (cur_state == instance->lower) {
74                         if (!throttle)
75                                 cur_state = -1;
76                 } else {
77                         cur_state -= 1;
78                         if (cur_state > instance->upper)
79                                 cur_state = instance->upper;
80                 }
81                 break;
82         case THERMAL_TREND_DROP_FULL:
83                 if (cur_state == instance->lower) {
84                         if (!throttle)
85                                 cur_state = -1;
86                 } else
87                         cur_state = instance->lower;
88                 break;
89         default:
90                 break;
91         }
92
93         return cur_state;
94 }
95
96 static void update_passive_instance(struct thermal_zone_device *tz,
97                                 enum thermal_trip_type type, int value)
98 {
99         /*
100          * If value is +1, activate a passive instance.
101          * If value is -1, deactivate a passive instance.
102          */
103         if (type == THERMAL_TRIP_PASSIVE || type == THERMAL_TRIPS_NONE)
104                 tz->passive += value;
105 }
106
107 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
108 {
109         long trip_temp;
110         enum thermal_trip_type trip_type;
111         enum thermal_trend trend;
112         struct thermal_instance *instance;
113         bool throttle = false;
114         int old_target;
115
116         if (trip == THERMAL_TRIPS_NONE) {
117                 trip_temp = tz->forced_passive;
118                 trip_type = THERMAL_TRIPS_NONE;
119         } else {
120                 tz->ops->get_trip_temp(tz, trip, &trip_temp);
121                 tz->ops->get_trip_type(tz, trip, &trip_type);
122         }
123
124         trend = get_tz_trend(tz, trip);
125
126         if (tz->temperature >= trip_temp)
127                 throttle = true;
128
129         dev_dbg(&tz->device, "Trip%d[type=%d,temp=%ld]:trend=%d,throttle=%d\n",
130                                 trip, trip_type, trip_temp, trend, throttle);
131
132         mutex_lock(&tz->lock);
133
134         list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
135                 if (instance->trip != trip)
136                         continue;
137
138                 old_target = instance->target;
139                 instance->target = get_target_state(instance, trend, throttle);
140                 dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
141                                         old_target, (int)instance->target);
142
143                 /* Activate a passive thermal instance */
144                 if (old_target == THERMAL_NO_TARGET &&
145                         instance->target != THERMAL_NO_TARGET)
146                         update_passive_instance(tz, trip_type, 1);
147                 /* Deactivate a passive thermal instance */
148                 else if (old_target != THERMAL_NO_TARGET &&
149                         instance->target == THERMAL_NO_TARGET)
150                         update_passive_instance(tz, trip_type, -1);
151
152
153                 instance->cdev->updated = false; /* cdev needs update */
154         }
155
156         mutex_unlock(&tz->lock);
157 }
158 #else
159
160 static int get_hyst_target(struct thermal_zone_device *tz)
161 {
162         int count;
163         unsigned long trip_temp, trip_hyst, hyst_temp;
164
165         if (tz->trips == 0 || !tz->ops->get_trip_hyst){
166                 return 0;
167         }
168         for (count = 0; count < tz->trips; count++) {
169                 tz->ops->get_trip_temp(tz, count, &trip_temp);
170                 tz->ops->get_trip_hyst(tz, count, &trip_hyst);
171                 hyst_temp = trip_temp - trip_hyst;
172                 printk("trip:%d, temp:%d, hyst_temp:%d\n", count, tz->temperature, hyst_temp);
173                 if (tz->temperature < (long)hyst_temp  + TRIP_TEMP_OFFSET)
174                         break;
175         }
176         return count;
177 }
178
179 static int get_trip_target(struct thermal_zone_device *tz)
180 {
181         int count = 0;
182         unsigned long trip_temp;
183
184         if (tz->trips == 0 || !tz->ops->get_trip_temp)
185                 return 0;
186
187         for (count = 0; count < tz->trips; count++) {
188                 tz->ops->get_trip_temp(tz, count, &trip_temp);
189                 printk("trip:%d, temp:%d, trip_temp:%d\n", count, tz->temperature, trip_temp);
190                 if (tz->temperature < (long)trip_temp  - TRIP_TEMP_OFFSET)
191                         break;
192         }
193         return count;
194 }
195
196 static int get_step_wise_target(struct thermal_instance *instance,
197                 struct thermal_zone_device *tz, int trip)
198 {
199         struct thermal_cooling_device *cdev = instance->cdev;
200         enum thermal_trend trend;
201         unsigned long cur_target;
202         unsigned long new_target;
203
204         cur_target = instance->target;
205         printk("%s: %s trip:%d cur_target:%d\n", cdev->type, __func__, trip, cur_target);
206         if (cur_target == THERMAL_NO_TARGET){
207                 cur_target = instance->lower;
208         }
209         if (tz->ops->get_trend){
210                 tz->ops->get_trend(tz, trip, &trend);
211         }else{
212                 new_target = get_trip_target(tz);
213                 printk("%s: %s trip:%d new_target:%d\n", cdev->type, __func__, trip, new_target);
214                 if (new_target < cur_target){
215                         trend = THERMAL_TREND_DROPPING;
216                         if (!tz->ops->get_trip_hyst){
217                                 goto dropping;
218                         }
219                 }else if(new_target >= cur_target){
220                         goto raising;
221                 }
222         }
223         switch (trend){
224                 case THERMAL_TREND_RAISING:
225                         new_target = get_trip_target(tz);
226                         printk("%s: %s trend raising, target:%d\n", cdev->type, __func__, new_target);
227                         goto raising;
228                 case THERMAL_TREND_DROPPING:
229                         if (tz->ops->get_trip_hyst){
230                                 new_target = get_hyst_target(tz);
231                         }else{
232                                 new_target = get_trip_target(tz);
233                         }
234                         printk("%s: %s trend dropping, target:%d\n", cdev->type, __func__, new_target);
235                         goto dropping;
236                 case THERMAL_TREND_STABLE:
237                 default:
238                         printk("%s: %s trend stable\n", cdev->type, __func__);
239                         return instance->lower;
240         }
241 raising:
242         if (new_target > cur_target){
243                 ++cur_target;
244         }
245         return cur_target;
246 dropping:
247         if (new_target < cur_target){
248                 --cur_target;
249         }
250         return cur_target;
251
252 }
253
254 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
255 {
256         struct thermal_instance *instance;
257         unsigned long target;
258
259         mutex_lock(&tz->lock);
260         list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
261                 if (instance->trip != trip)
262                         continue;
263                 target = get_step_wise_target(instance, tz, trip);
264                 printk("%s: get target_state: %d\n", instance->cdev->type, target);
265                 if (target == THERMAL_NO_TARGET){
266                         continue;
267                 }
268                 if (target < instance->lower){
269                         target = instance->lower;
270                 }
271                 if (target > instance->upper){
272                         target = instance->upper;
273                 }
274                 instance->target  = target;
275                 instance->cdev->updated = false; /* cdev needs update */
276         }
277         mutex_unlock(&tz->lock);
278
279         return;
280 }
281 #endif
282
283 /**
284  * step_wise_throttle - throttles devices asscciated with the given zone
285  * @tz - thermal_zone_device
286  * @trip - the trip point
287  * @trip_type - type of the trip point
288  *
289  * Throttling Logic: This uses the trend of the thermal zone to throttle.
290  * If the thermal zone is 'heating up' this throttles all the cooling
291  * devices associated with the zone and its particular trip point, by one
292  * step. If the zone is 'cooling down' it brings back the performance of
293  * the devices by one step.
294  */
295 static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
296 {
297         struct thermal_instance *instance;
298
299         thermal_zone_trip_update(tz, trip);
300
301         if (tz->forced_passive)
302                 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE);
303
304         mutex_lock(&tz->lock);
305
306         list_for_each_entry(instance, &tz->thermal_instances, tz_node)
307                 thermal_cdev_update(instance->cdev);
308
309         mutex_unlock(&tz->lock);
310
311         return 0;
312 }
313
314 static struct thermal_governor thermal_gov_step_wise = {
315         .name           = "step_wise",
316         .throttle       = step_wise_throttle,
317 };
318
319 int thermal_gov_step_wise_register(void)
320 {
321         return thermal_register_governor(&thermal_gov_step_wise);
322 }
323
324 void thermal_gov_step_wise_unregister(void)
325 {
326         thermal_unregister_governor(&thermal_gov_step_wise);
327 }