Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / omap-thermal / omap-thermal-common.c
1 /*
2 * OMAP thermal driver interface
3 *
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
5 * Contact:
6 * Eduardo Valentin <eduardo.valentin@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/mutex.h>
27 #include <linux/gfp.h>
28 #include <linux/kernel.h>
29 #include <linux/workqueue.h>
30 #include <linux/thermal.h>
31 #include <linux/cpufreq.h>
32 #include <linux/cpu_cooling.h>
33
34 #include "omap-thermal.h"
35 #include "omap-bandgap.h"
36
37 /* common data structures */
38 struct omap_thermal_data {
39 struct thermal_zone_device *omap_thermal;
40 struct thermal_cooling_device *cool_dev;
41 struct omap_bandgap *bg_ptr;
42 enum thermal_device_mode mode;
43 struct work_struct thermal_wq;
44 int sensor_id;
45 };
46
47 static void omap_thermal_work(struct work_struct *work)
48 {
49 struct omap_thermal_data *data = container_of(work,
50 struct omap_thermal_data, thermal_wq);
51
52 thermal_zone_device_update(data->omap_thermal);
53
54 dev_dbg(&data->omap_thermal->device, "updated thermal zone %s\n",
55 data->omap_thermal->type);
56 }
57
58 /**
59 * omap_thermal_hotspot_temperature - returns sensor extrapolated temperature
60 * @t: omap sensor temperature
61 * @s: omap sensor slope value
62 * @c: omap sensor const value
63 */
64 static inline int omap_thermal_hotspot_temperature(int t, int s, int c)
65 {
66 int delta = t * s / 1000 + c;
67
68 if (delta < 0)
69 delta = 0;
70
71 return t + delta;
72 }
73
74 /* thermal zone ops */
75 /* Get temperature callback function for thermal zone*/
76 static inline int omap_thermal_get_temp(struct thermal_zone_device *thermal,
77 unsigned long *temp)
78 {
79 struct omap_thermal_data *data = thermal->devdata;
80 struct omap_bandgap *bg_ptr;
81 struct omap_temp_sensor *s;
82 int ret, tmp, pcb_temp, slope, constant;
83
84 if (!data)
85 return 0;
86
87 bg_ptr = data->bg_ptr;
88 s = &bg_ptr->conf->sensors[data->sensor_id];
89
90 ret = omap_bandgap_read_temperature(bg_ptr, data->sensor_id, &tmp);
91 if (ret)
92 return ret;
93
94 pcb_temp = 0;
95 /* TODO: Introduce pcb temperature lookup */
96 /* In case pcb zone is available, use the extrapolation rule with it */
97 if (pcb_temp) {
98 tmp -= pcb_temp;
99 slope = s->slope_pcb;
100 constant = s->constant_pcb;
101 } else {
102 slope = s->slope;
103 constant = s->constant;
104 }
105 *temp = omap_thermal_hotspot_temperature(tmp, slope, constant);
106
107 return ret;
108 }
109
110 /* Bind callback functions for thermal zone */
111 static int omap_thermal_bind(struct thermal_zone_device *thermal,
112 struct thermal_cooling_device *cdev)
113 {
114 struct omap_thermal_data *data = thermal->devdata;
115 int max, id;
116
117 if (IS_ERR_OR_NULL(data))
118 return -ENODEV;
119
120 /* check if this is the cooling device we registered */
121 if (data->cool_dev != cdev)
122 return 0;
123
124 id = data->sensor_id;
125 max = data->bg_ptr->conf->sensors[id].cooling_data.freq_clip_count;
126
127 /* TODO: bind with min and max states */
128 /* Simple thing, two trips, one passive another critical */
129 return thermal_zone_bind_cooling_device(thermal, 0, cdev);
130 }
131
132 /* Unbind callback functions for thermal zone */
133 static int omap_thermal_unbind(struct thermal_zone_device *thermal,
134 struct thermal_cooling_device *cdev)
135 {
136 struct omap_thermal_data *data = thermal->devdata;
137
138 if (IS_ERR_OR_NULL(data))
139 return -ENODEV;
140
141 /* check if this is the cooling device we registered */
142 if (data->cool_dev != cdev)
143 return 0;
144
145 /* Simple thing, two trips, one passive another critical */
146 return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
147 }
148
149 /* Get mode callback functions for thermal zone */
150 static int omap_thermal_get_mode(struct thermal_zone_device *thermal,
151 enum thermal_device_mode *mode)
152 {
153 struct omap_thermal_data *data = thermal->devdata;
154
155 if (data)
156 *mode = data->mode;
157
158 return 0;
159 }
160
161 /* Set mode callback functions for thermal zone */
162 static int omap_thermal_set_mode(struct thermal_zone_device *thermal,
163 enum thermal_device_mode mode)
164 {
165 struct omap_thermal_data *data = thermal->devdata;
166
167 if (!data->omap_thermal) {
168 dev_notice(&thermal->device, "thermal zone not registered\n");
169 return 0;
170 }
171
172 mutex_lock(&data->omap_thermal->lock);
173
174 if (mode == THERMAL_DEVICE_ENABLED)
175 data->omap_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
176 else
177 data->omap_thermal->polling_delay = 0;
178
179 mutex_unlock(&data->omap_thermal->lock);
180
181 data->mode = mode;
182 thermal_zone_device_update(data->omap_thermal);
183 dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
184 data->omap_thermal->polling_delay);
185
186 return 0;
187 }
188
189 /* Get trip type callback functions for thermal zone */
190 static int omap_thermal_get_trip_type(struct thermal_zone_device *thermal,
191 int trip, enum thermal_trip_type *type)
192 {
193 if (!omap_thermal_is_valid_trip(trip))
194 return -EINVAL;
195
196 if (trip + 1 == OMAP_TRIP_NUMBER)
197 *type = THERMAL_TRIP_CRITICAL;
198 else
199 *type = THERMAL_TRIP_PASSIVE;
200
201 return 0;
202 }
203
204 /* Get trip temperature callback functions for thermal zone */
205 static int omap_thermal_get_trip_temp(struct thermal_zone_device *thermal,
206 int trip, unsigned long *temp)
207 {
208 if (!omap_thermal_is_valid_trip(trip))
209 return -EINVAL;
210
211 *temp = omap_thermal_get_trip_value(trip);
212
213 return 0;
214 }
215
216 /* Get critical temperature callback functions for thermal zone */
217 static int omap_thermal_get_crit_temp(struct thermal_zone_device *thermal,
218 unsigned long *temp)
219 {
220 /* shutdown zone */
221 return omap_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
222 }
223
224 static struct thermal_zone_device_ops omap_thermal_ops = {
225 .get_temp = omap_thermal_get_temp,
226 /* TODO: add .get_trend */
227 .bind = omap_thermal_bind,
228 .unbind = omap_thermal_unbind,
229 .get_mode = omap_thermal_get_mode,
230 .set_mode = omap_thermal_set_mode,
231 .get_trip_type = omap_thermal_get_trip_type,
232 .get_trip_temp = omap_thermal_get_trip_temp,
233 .get_crit_temp = omap_thermal_get_crit_temp,
234 };
235
236 static struct omap_thermal_data
237 *omap_thermal_build_data(struct omap_bandgap *bg_ptr, int id)
238 {
239 struct omap_thermal_data *data;
240
241 data = devm_kzalloc(bg_ptr->dev, sizeof(*data), GFP_KERNEL);
242 if (!data) {
243 dev_err(bg_ptr->dev, "kzalloc fail\n");
244 return NULL;
245 }
246 data->sensor_id = id;
247 data->bg_ptr = bg_ptr;
248 data->mode = THERMAL_DEVICE_ENABLED;
249 INIT_WORK(&data->thermal_wq, omap_thermal_work);
250
251 return data;
252 }
253
254 int omap_thermal_expose_sensor(struct omap_bandgap *bg_ptr, int id,
255 char *domain)
256 {
257 struct omap_thermal_pdata pdata;
258
259 data = omap_bandgap_get_sensor_data(bg_ptr, id);
260
261 if (!data)
262 data = omap_thermal_build_pdata(bg_ptr, id);
263
264 if (!data)
265 return -EINVAL;
266
267 /* TODO: remove TC1 TC2 */
268 /* Create thermal zone */
269 data->omap_thermal = thermal_zone_device_register(domain,
270 OMAP_TRIP_NUMBER, 0, data, &omap_thermal_ops,
271 1, 2, /*TODO: remove this when FW allows */
272 FAST_TEMP_MONITORING_RATE,
273 FAST_TEMP_MONITORING_RATE);
274 if (IS_ERR_OR_NULL(data->omap_thermal)) {
275 dev_err(bg_ptr->dev, "thermal zone device is NULL\n");
276 return PTR_ERR(data->omap_thermal);
277 }
278 data->omap_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
279 omap_bandgap_set_sensor_data(bg_ptr, id, data);
280
281 return 0;
282 }
283
284 int omap_thermal_remove_sensor(struct omap_bandgap *bg_ptr, int id)
285 {
286 struct omap_thermal_data *data;
287
288 data = omap_bandgap_get_sensor_data(bg_ptr, id);
289
290 thermal_zone_device_unregister(data->omap_thermal);
291
292 return 0;
293 }
294
295 int omap_thermal_report_sensor_temperature(struct omap_bandgap *bg_ptr, int id)
296 {
297 struct omap_thermal_data *data;
298
299 data = omap_bandgap_get_sensor_data(bg_ptr, id);
300
301 schedule_work(&data->thermal_wq);
302
303 return 0;
304 }
305
306 static int omap_thermal_build_cpufreq_clip(struct omap_bandgap *bg_ptr,
307 struct freq_clip_table **tab_ptr,
308 int *tab_size)
309 {
310 struct cpufreq_frequency_table *freq_table;
311 struct freq_clip_table *tab;
312 int i, count = 0;
313
314 freq_table = cpufreq_frequency_get_table(0);
315 if (IS_ERR_OR_NULL(freq_table)) {
316 dev_err(bg_ptr->dev,
317 "%s: failed to get cpufreq table (%p)\n",
318 __func__, freq_table);
319 return -EINVAL;
320 }
321
322 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
323 unsigned int freq = freq_table[i].frequency;
324 if (freq == CPUFREQ_ENTRY_INVALID)
325 continue;
326 count++;
327 }
328
329 tab = devm_kzalloc(bg_ptr->dev, sizeof(*tab) * count, GFP_KERNEL);
330 if (!tab) {
331 dev_err(bg_ptr->dev,
332 "%s: no memory available\n", __func__);
333 return -ENOMEM;
334 }
335
336 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
337 unsigned int freq = freq_table[i].frequency;
338
339 if (freq == CPUFREQ_ENTRY_INVALID)
340 continue;
341
342 tab[count - i - 1].freq_clip_max = freq;
343 tab[count - i - 1].temp_level = OMAP_TRIP_HOT;
344 tab[count - i - 1].mask_val = cpumask_of(0);
345 }
346
347 *tab_ptr = tab;
348 *tab_size = count;
349
350 return 0;
351 }
352
353 int omap_thermal_register_cpu_cooling(struct omap_bandgap *bg_ptr, int id)
354 {
355 struct omap_thermal_data *data;
356 struct freq_clip_table *tab_ptr;
357 int tab_size, ret;
358
359 data = omap_bandgap_get_sensor_data(bg_ptr, id);
360 if (!data)
361 data = omap_thermal_build_pdata(bg_ptr, id);
362
363 if (!data)
364 return -EINVAL;
365
366 ret = omap_thermal_build_cpufreq_clip(bg_ptr, &tab_ptr, &tab_size);
367 if (ret < 0) {
368 dev_err(bg_ptr->dev,
369 "%s: failed to build cpufreq clip table\n", __func__);
370 return ret;
371 }
372
373 /* Register cooling device */
374 data->cool_dev = cpufreq_cooling_register(tab_ptr, tab_size);
375 if (IS_ERR_OR_NULL(data->cool_dev)) {
376 dev_err(bg_ptr->dev,
377 "Failed to register cpufreq cooling device\n");
378 return PTR_ERR(data->cool_dev);
379 }
380 bg_ptr->conf->sensors[id].cooling_data.freq_clip_count = tab_size;
381 omap_bandgap_set_sensor_data(bg_ptr, id, data);
382
383 return 0;
384 }
385
386 int omap_thermal_unregister_cpu_cooling(struct omap_bandgap *bg_ptr, int id)
387 {
388 struct omap_thermal_data *data;
389
390 data = omap_bandgap_get_sensor_data(bg_ptr, id);
391 cpufreq_cooling_unregister(data->cool_dev);
392
393 return 0;
394 }