]> Pileus Git - ~andy/linux/blob - drivers/staging/omap-thermal/omap-thermal-common.c
thermal: Exynos: Fix NULL pointer dereference in exynos_unregister_thermal()
[~andy/linux] / 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 = data->bg_ptr;
81         struct omap_temp_sensor *s = &bg_ptr->conf->sensors[data->sensor_id];
82         int ret, tmp, pcb_temp, slope, constant;
83
84         ret = omap_bandgap_read_temperature(bg_ptr, data->sensor_id, &tmp);
85         if (ret)
86                 return ret;
87
88         pcb_temp = 0;
89         /* TODO: Introduce pcb temperature lookup */
90         /* In case pcb zone is available, use the extrapolation rule with it */
91         if (pcb_temp) {
92                 tmp -= pcb_temp;
93                 slope = s->slope_pcb;
94                 constant = s->constant_pcb;
95         } else {
96                 slope = s->slope;
97                 constant = s->constant;
98         }
99         *temp = omap_thermal_hotspot_temperature(tmp, slope, constant);
100
101         return ret;
102 }
103
104 /* Bind callback functions for thermal zone */
105 static int omap_thermal_bind(struct thermal_zone_device *thermal,
106                               struct thermal_cooling_device *cdev)
107 {
108         struct omap_thermal_data *data = thermal->devdata;
109         int max, id;
110
111         if (IS_ERR_OR_NULL(data))
112                 return -ENODEV;
113
114         /* check if this is the cooling device we registered */
115         if (data->cool_dev != cdev)
116                 return 0;
117
118         id = data->sensor_id;
119         max = data->bg_ptr->conf->sensors[id].cooling_data.freq_clip_count;
120
121         /* TODO: bind with min and max states */
122         /* Simple thing, two trips, one passive another critical */
123         return thermal_zone_bind_cooling_device(thermal, 0, cdev,
124                                                 THERMAL_NO_LIMIT,
125                                                 THERMAL_NO_LIMIT);
126 }
127
128 /* Unbind callback functions for thermal zone */
129 static int omap_thermal_unbind(struct thermal_zone_device *thermal,
130                                 struct thermal_cooling_device *cdev)
131 {
132         struct omap_thermal_data *data = thermal->devdata;
133
134         if (IS_ERR_OR_NULL(data))
135                 return -ENODEV;
136
137         /* check if this is the cooling device we registered */
138         if (data->cool_dev != cdev)
139                 return 0;
140
141         /* Simple thing, two trips, one passive another critical */
142         return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
143 }
144
145 /* Get mode callback functions for thermal zone */
146 static int omap_thermal_get_mode(struct thermal_zone_device *thermal,
147                                   enum thermal_device_mode *mode)
148 {
149         struct omap_thermal_data *data = thermal->devdata;
150
151         if (data)
152                 *mode = data->mode;
153
154         return 0;
155 }
156
157 /* Set mode callback functions for thermal zone */
158 static int omap_thermal_set_mode(struct thermal_zone_device *thermal,
159                                   enum thermal_device_mode mode)
160 {
161         struct omap_thermal_data *data = thermal->devdata;
162
163         if (!data->omap_thermal) {
164                 dev_notice(&thermal->device, "thermal zone not registered\n");
165                 return 0;
166         }
167
168         mutex_lock(&data->omap_thermal->lock);
169
170         if (mode == THERMAL_DEVICE_ENABLED)
171                 data->omap_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
172         else
173                 data->omap_thermal->polling_delay = 0;
174
175         mutex_unlock(&data->omap_thermal->lock);
176
177         data->mode = mode;
178         thermal_zone_device_update(data->omap_thermal);
179         dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
180                 data->omap_thermal->polling_delay);
181
182         return 0;
183 }
184
185 /* Get trip type callback functions for thermal zone */
186 static int omap_thermal_get_trip_type(struct thermal_zone_device *thermal,
187                                        int trip, enum thermal_trip_type *type)
188 {
189         if (!omap_thermal_is_valid_trip(trip))
190                 return -EINVAL;
191
192         if (trip + 1 == OMAP_TRIP_NUMBER)
193                 *type = THERMAL_TRIP_CRITICAL;
194         else
195                 *type = THERMAL_TRIP_PASSIVE;
196
197         return 0;
198 }
199
200 /* Get trip temperature callback functions for thermal zone */
201 static int omap_thermal_get_trip_temp(struct thermal_zone_device *thermal,
202                                        int trip, unsigned long *temp)
203 {
204         if (!omap_thermal_is_valid_trip(trip))
205                 return -EINVAL;
206
207         *temp = omap_thermal_get_trip_value(trip);
208
209         return 0;
210 }
211
212 /* Get critical temperature callback functions for thermal zone */
213 static int omap_thermal_get_crit_temp(struct thermal_zone_device *thermal,
214                                        unsigned long *temp)
215 {
216         /* shutdown zone */
217         return omap_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
218 }
219
220 static struct thermal_zone_device_ops omap_thermal_ops = {
221         .get_temp = omap_thermal_get_temp,
222         /* TODO: add .get_trend */
223         .bind = omap_thermal_bind,
224         .unbind = omap_thermal_unbind,
225         .get_mode = omap_thermal_get_mode,
226         .set_mode = omap_thermal_set_mode,
227         .get_trip_type = omap_thermal_get_trip_type,
228         .get_trip_temp = omap_thermal_get_trip_temp,
229         .get_crit_temp = omap_thermal_get_crit_temp,
230 };
231
232 int omap_thermal_expose_sensor(struct omap_bandgap *bg_ptr, int id,
233                                char *domain)
234 {
235         struct omap_thermal_data *data;
236
237         data = devm_kzalloc(bg_ptr->dev, sizeof(*data), GFP_KERNEL);
238         if (!data) {
239                 dev_err(bg_ptr->dev, "kzalloc fail\n");
240                 return -ENOMEM;
241         }
242         data->sensor_id = id;
243         data->bg_ptr = bg_ptr;
244         data->mode = THERMAL_DEVICE_ENABLED;
245         INIT_WORK(&data->thermal_wq, omap_thermal_work);
246
247         /* TODO: remove TC1 TC2 */
248         /* Create thermal zone */
249         data->omap_thermal = thermal_zone_device_register(domain,
250                                 OMAP_TRIP_NUMBER, 0, data, &omap_thermal_ops,
251                                 0, FAST_TEMP_MONITORING_RATE);
252         if (IS_ERR_OR_NULL(data->omap_thermal)) {
253                 dev_err(bg_ptr->dev, "thermal zone device is NULL\n");
254                 return PTR_ERR(data->omap_thermal);
255         }
256         data->omap_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
257         omap_bandgap_set_sensor_data(bg_ptr, id, data);
258
259         return 0;
260 }
261
262 int omap_thermal_remove_sensor(struct omap_bandgap *bg_ptr, int id)
263 {
264         struct omap_thermal_data *data;
265
266         data = omap_bandgap_get_sensor_data(bg_ptr, id);
267
268         thermal_zone_device_unregister(data->omap_thermal);
269
270         return 0;
271 }
272
273 int omap_thermal_report_sensor_temperature(struct omap_bandgap *bg_ptr, int id)
274 {
275         struct omap_thermal_data *data;
276
277         data = omap_bandgap_get_sensor_data(bg_ptr, id);
278
279         schedule_work(&data->thermal_wq);
280
281         return 0;
282 }
283
284 static int omap_thermal_build_cpufreq_clip(struct omap_bandgap *bg_ptr,
285                                            struct freq_clip_table **tab_ptr,
286                                            int *tab_size)
287 {
288         struct cpufreq_frequency_table *freq_table;
289         struct freq_clip_table *tab;
290         int i, count = 0;
291
292         freq_table = cpufreq_frequency_get_table(0);
293         if (IS_ERR_OR_NULL(freq_table)) {
294                 dev_err(bg_ptr->dev,
295                         "%s: failed to get cpufreq table (%p)\n",
296                         __func__, freq_table);
297                 return -EINVAL;
298         }
299
300         for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
301                 unsigned int freq = freq_table[i].frequency;
302                 if (freq == CPUFREQ_ENTRY_INVALID)
303                         continue;
304                 count++;
305         }
306
307         tab = devm_kzalloc(bg_ptr->dev, sizeof(*tab) * count, GFP_KERNEL);
308         if (!tab) {
309                 dev_err(bg_ptr->dev,
310                         "%s: no memory available\n", __func__);
311                 return -ENOMEM;
312         }
313
314         for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
315                 unsigned int freq = freq_table[i].frequency;
316
317                 if (freq == CPUFREQ_ENTRY_INVALID)
318                         continue;
319
320                 tab[count - i - 1].freq_clip_max = freq;
321                 tab[count - i - 1].temp_level = OMAP_TRIP_HOT;
322                 tab[count - i - 1].mask_val = cpumask_of(0);
323         }
324
325         *tab_ptr = tab;
326         *tab_size = count;
327
328         return 0;
329 }
330
331 int omap_thermal_register_cpu_cooling(struct omap_bandgap *bg_ptr, int id)
332 {
333         struct omap_thermal_data *data;
334         struct freq_clip_table *tab_ptr;
335         int tab_size, ret;
336
337         data = omap_bandgap_get_sensor_data(bg_ptr, id);
338
339         ret = omap_thermal_build_cpufreq_clip(bg_ptr, &tab_ptr, &tab_size);
340         if (ret < 0) {
341                 dev_err(bg_ptr->dev,
342                         "%s: failed to build cpufreq clip table\n", __func__);
343                 return ret;
344         }
345
346         /* Register cooling device */
347         data->cool_dev = cpufreq_cooling_register(tab_ptr, tab_size);
348         if (IS_ERR_OR_NULL(data->cool_dev)) {
349                 dev_err(bg_ptr->dev,
350                         "Failed to register cpufreq cooling device\n");
351                 return PTR_ERR(data->cool_dev);
352         }
353         bg_ptr->conf->sensors[id].cooling_data.freq_clip_count = tab_size;
354
355         return 0;
356 }
357
358 int omap_thermal_unregister_cpu_cooling(struct omap_bandgap *bg_ptr, int id)
359 {
360         struct omap_thermal_data *data;
361
362         data = omap_bandgap_get_sensor_data(bg_ptr, id);
363         cpufreq_cooling_unregister(data->cool_dev);
364
365         return 0;
366 }