blob: 944ba2f340c83cfed436c569c949d58dc6a60dfe [file] [log] [blame]
Durgadoss R4ccc57432012-09-18 11:05:01 +05301/*
2 * fair_share.c - A simple weight based Thermal 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
Durgadoss R4ccc57432012-09-18 11:05:01 +053025#include <linux/thermal.h>
26
27#include "thermal_core.h"
28
29/**
30 * get_trip_level: - obtains the current trip level for a zone
31 * @tz: thermal zone device
32 */
33static int get_trip_level(struct thermal_zone_device *tz)
34{
35 int count = 0;
36 unsigned long trip_temp;
37
38 if (tz->trips == 0 || !tz->ops->get_trip_temp)
39 return 0;
40
41 for (count = 0; count < tz->trips; count++) {
42 tz->ops->get_trip_temp(tz, count, &trip_temp);
43 if (tz->temperature < trip_temp)
44 break;
45 }
46 return count;
47}
48
49static long get_target_state(struct thermal_zone_device *tz,
50 struct thermal_cooling_device *cdev, int weight, int level)
51{
52 unsigned long max_state;
53
54 cdev->ops->get_max_state(cdev, &max_state);
55
56 return (long)(weight * level * max_state) / (100 * tz->trips);
57}
58
59/**
60 * fair_share_throttle - throttles devices asscciated with the given zone
61 * @tz - thermal_zone_device
62 *
63 * Throttling Logic: This uses three parameters to calculate the new
64 * throttle state of the cooling devices associated with the given zone.
65 *
66 * Parameters used for Throttling:
67 * P1. max_state: Maximum throttle state exposed by the cooling device.
68 * P2. weight[i]/100:
69 * How 'effective' the 'i'th device is, in cooling the given zone.
70 * P3. cur_trip_level/max_no_of_trips:
71 * This describes the extent to which the devices should be throttled.
72 * We do not want to throttle too much when we trip a lower temperature,
73 * whereas the throttling is at full swing if we trip critical levels.
74 * (Heavily assumes the trip points are in ascending order)
75 * new_state of cooling device = P3 * P2 * P1
76 */
Sachin Kamat62176932012-09-27 16:57:53 +053077static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
Durgadoss R4ccc57432012-09-18 11:05:01 +053078{
79 const struct thermal_zone_params *tzp;
80 struct thermal_cooling_device *cdev;
81 struct thermal_instance *instance;
82 int i;
83 int cur_trip_level = get_trip_level(tz);
84
85 if (!tz->tzp || !tz->tzp->tbp)
86 return -EINVAL;
87
88 tzp = tz->tzp;
89
90 for (i = 0; i < tzp->num_tbps; i++) {
91 if (!tzp->tbp[i].cdev)
92 continue;
93
94 cdev = tzp->tbp[i].cdev;
95 instance = get_thermal_instance(tz, cdev, trip);
96 if (!instance)
97 continue;
98
99 instance->target = get_target_state(tz, cdev,
100 tzp->tbp[i].weight, cur_trip_level);
101
102 instance->cdev->updated = false;
103 thermal_cdev_update(cdev);
104 }
105 return 0;
106}
107
Sachin Kamat62176932012-09-27 16:57:53 +0530108static struct thermal_governor thermal_gov_fair_share = {
Durgadoss R4ccc57432012-09-18 11:05:01 +0530109 .name = "fair_share",
110 .throttle = fair_share_throttle,
Durgadoss R4ccc57432012-09-18 11:05:01 +0530111};
112
Zhang Rui80a26a52013-03-26 16:38:29 +0800113int thermal_gov_fair_share_register(void)
Durgadoss R4ccc57432012-09-18 11:05:01 +0530114{
115 return thermal_register_governor(&thermal_gov_fair_share);
116}
117
Zhang Rui80a26a52013-03-26 16:38:29 +0800118void thermal_gov_fair_share_unregister(void)
Durgadoss R4ccc57432012-09-18 11:05:01 +0530119{
120 thermal_unregister_governor(&thermal_gov_fair_share);
121}
122