]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/vendor-ti-am57x.git/blob - sgx_km/eurasia_km/services4/system/omap/sgxfreq_cool.c
sgx_km: Build SGX KM from source
[android/vendor-ti-am57x.git] / sgx_km / eurasia_km / services4 / system / omap / sgxfreq_cool.c
1 /*
2  * Copyright (C) 2012 Texas Instruments, Inc
3  *
4 @License        Dual MIT/GPLv2
6 The contents of this file are subject to the MIT license as set out below.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 Alternatively, the contents of this file may be used under the terms of
19 the GNU General Public License Version 2 ("GPL") in which case the provisions
20 of GPL are applicable instead of those above.
22 If you wish to allow use of your version of this file only under the terms of
23 GPL, and not to allow others to use your version of this file under the terms
24 of the MIT license, indicate your decision by deleting the provisions above
25 and replace them with the notice and other provisions required by GPL as set
26 out in the file called "GPL-COPYING" included in this distribution. If you do
27 not delete the provisions above, a recipient may use your version of this file
28 under the terms of either the MIT license or GPL.
30 This License is also included in this distribution in the file called
31 "MIT-COPYING".
33 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
34 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
35 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
37 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
38 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 */
42 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0))
44 #include <linux/thermal.h>
46 static struct cool_data {
47         int freq_cnt;
48         unsigned long *freq_list;
49         unsigned long state;
50         struct thermal_cooling_device *cdev;
51 } cd;
53 static int sgxfreq_get_max_state(struct thermal_cooling_device *cdev,
54                 unsigned long *state)
55 {
56         *state = cd.freq_cnt - 1;
57         return 0;
58 }
60 static int sgxfreq_get_cur_state(struct thermal_cooling_device *cdev,
61                 unsigned long *state)
62 {
63         *state = cd.state;
64         return 0;
65 }
67 static int sgxfreq_set_cur_state(struct thermal_cooling_device *cdev,
68                 unsigned long state)
69 {
70         int freq_max_index, freq_limit_index;
72         freq_max_index = cd.freq_cnt - 1;
74         freq_limit_index = freq_max_index - (unsigned int)state;
76         if (freq_limit_index < 0)
77                 freq_limit_index = 0;
79         sgxfreq_set_freq_limit(cd.freq_list[freq_limit_index]);
81         cd.state = state;
82         return 0;
83 }
86 static const struct thermal_cooling_device_ops sgxfreq_cooling_ops = {
87         .get_max_state = sgxfreq_get_max_state,
88         .get_cur_state = sgxfreq_get_cur_state,
89         .set_cur_state = sgxfreq_set_cur_state,
90 };
92 int cool_init(void)
93 {
94         int ret;
95         struct thermal_zone_device *tz;
97         cd.freq_cnt = sgxfreq_get_freq_list(&cd.freq_list);
98         if (!cd.freq_cnt || !cd.freq_list)
99                 return -EINVAL;
101         cd.cdev = thermal_cooling_device_register("gpu", (void *)NULL, &sgxfreq_cooling_ops);
103         if(IS_ERR(cd.cdev)) {
104                 pr_err("sgxfreq: Error while regeistering cooling device: %ld\n", PTR_ERR(cd.cdev));
105                 return -1;
106         }
108         tz = thermal_zone_get_zone_by_name("gpu");
109         if(IS_ERR(tz)) {
110                 pr_err("sgxfreq: Error while trying to obtain zone device: %ld\n", PTR_ERR(tz));
111                 return -1;
112         }
114         ret = thermal_zone_bind_cooling_device(tz, 0, cd.cdev, THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
115         if (ret)
116         {
117                 pr_err("sgxfreq: Error binding cooling device: %d\n", ret);
118         }
120         return 0;
123 void cool_deinit(void)
125         thermal_cooling_device_unregister(cd.cdev);
127 #else //if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0))
128 #include <linux/thermal_framework.h>
130 static int cool_device(struct thermal_dev *dev, int cooling_level);
132 static struct cool_data {
133         int freq_cnt;
134         unsigned long *freq_list;
135 } cd;
137 static struct thermal_dev_ops cool_dev_ops = {
138         .cool_device = cool_device,
139 };
141 static struct thermal_dev cool_dev = {
142         .name = "gpu_cooling.0",
143         .domain_name = "gpu",
144         .dev_ops = &cool_dev_ops,
145 };
147 static struct thermal_dev case_cool_dev = {
148         .name = "gpu_cooling.1",
149         .domain_name = "case",
150         .dev_ops = &cool_dev_ops,
151 };
153 static unsigned int gpu_cooling_level;
154 #if defined(CONFIG_CASE_TEMP_GOVERNOR)
155 static unsigned int case_cooling_level;
156 #endif
158 int cool_init(void)
160         int ret;
161         cd.freq_cnt = sgxfreq_get_freq_list(&cd.freq_list);
162         if (!cd.freq_cnt || !cd.freq_list)
163                 return -EINVAL;
165         ret = thermal_cooling_dev_register(&cool_dev);
166         if (ret)
167                 return ret;
169         return thermal_cooling_dev_register(&case_cool_dev);
172 void cool_deinit(void)
174         thermal_cooling_dev_unregister(&cool_dev);
175         thermal_cooling_dev_unregister(&case_cool_dev);
178 static int cool_device(struct thermal_dev *dev, int cooling_level)
180         int freq_max_index, freq_limit_index;
182 #if defined(CONFIG_CASE_TEMP_GOVERNOR)
183         if (!strcmp(dev->domain_name, "case"))
184         {
185                 int tmp = 0;
186                 tmp = cooling_level - case_subzone_number;
187                 if (tmp < 0)
188                         tmp = 0;
189                 case_cooling_level = tmp;
190         }
191         else
192 #endif
193         {
194                gpu_cooling_level = cooling_level;
195         }
197         freq_max_index = cd.freq_cnt - 1;
198 #if defined(CONFIG_CASE_TEMP_GOVERNOR)
199         if (case_cooling_level > gpu_cooling_level)
200         {
201                 freq_limit_index = freq_max_index - case_cooling_level;
202         }
203         else
204 #endif
205         {
206                 freq_limit_index = freq_max_index - gpu_cooling_level;
207         }
209         if (freq_limit_index < 0)
210                 freq_limit_index = 0;
212         sgxfreq_set_freq_limit(cd.freq_list[freq_limit_index]);
214         return 0;
216 #endif //if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0))