]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/thermal/ti-soc-thermal/ti-thermal-common.c
Merge remote-tracking branch 'hid/for-next'
[karo-tx-linux.git] / drivers / thermal / ti-soc-thermal / ti-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/cpumask.h>
33 #include <linux/cpu_cooling.h>
34
35 #include "ti-thermal.h"
36 #include "ti-bandgap.h"
37
38 /* common data structures */
39 struct ti_thermal_data {
40         struct thermal_zone_device *ti_thermal;
41         struct thermal_zone_device *pcb_tz;
42         struct thermal_cooling_device *cool_dev;
43         struct ti_bandgap *bgp;
44         enum thermal_device_mode mode;
45         struct work_struct thermal_wq;
46         int sensor_id;
47 };
48
49 static void ti_thermal_work(struct work_struct *work)
50 {
51         struct ti_thermal_data *data = container_of(work,
52                                         struct ti_thermal_data, thermal_wq);
53
54         thermal_zone_device_update(data->ti_thermal);
55
56         dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
57                 data->ti_thermal->type);
58 }
59
60 /**
61  * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
62  * @t:  omap sensor temperature
63  * @s:  omap sensor slope value
64  * @c:  omap sensor const value
65  */
66 static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
67 {
68         int delta = t * s / 1000 + c;
69
70         if (delta < 0)
71                 delta = 0;
72
73         return t + delta;
74 }
75
76 /* thermal zone ops */
77 /* Get temperature callback function for thermal zone*/
78 static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
79                                       unsigned long *temp)
80 {
81         struct thermal_zone_device *pcb_tz = NULL;
82         struct ti_thermal_data *data = thermal->devdata;
83         struct ti_bandgap *bgp;
84         const struct ti_temp_sensor *s;
85         int ret, tmp, slope, constant;
86         unsigned long pcb_temp;
87
88         if (!data)
89                 return 0;
90
91         bgp = data->bgp;
92         s = &bgp->conf->sensors[data->sensor_id];
93
94         ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
95         if (ret)
96                 return ret;
97
98         /* Default constants */
99         slope = s->slope;
100         constant = s->constant;
101
102         pcb_tz = data->pcb_tz;
103         /* In case pcb zone is available, use the extrapolation rule with it */
104         if (!IS_ERR(pcb_tz)) {
105                 ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
106                 if (!ret) {
107                         tmp -= pcb_temp; /* got a valid PCB temp */
108                         slope = s->slope_pcb;
109                         constant = s->constant_pcb;
110                 } else {
111                         dev_err(bgp->dev,
112                                 "Failed to read PCB state. Using defaults\n");
113                         ret = 0;
114                 }
115         }
116         *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
117
118         return ret;
119 }
120
121 /* Bind callback functions for thermal zone */
122 static int ti_thermal_bind(struct thermal_zone_device *thermal,
123                            struct thermal_cooling_device *cdev)
124 {
125         struct ti_thermal_data *data = thermal->devdata;
126         int id;
127
128         if (!data || IS_ERR(data))
129                 return -ENODEV;
130
131         /* check if this is the cooling device we registered */
132         if (data->cool_dev != cdev)
133                 return 0;
134
135         id = data->sensor_id;
136
137         /* Simple thing, two trips, one passive another critical */
138         return thermal_zone_bind_cooling_device(thermal, 0, cdev,
139         /* bind with min and max states defined by cpu_cooling */
140                                                 THERMAL_NO_LIMIT,
141                                                 THERMAL_NO_LIMIT);
142 }
143
144 /* Unbind callback functions for thermal zone */
145 static int ti_thermal_unbind(struct thermal_zone_device *thermal,
146                              struct thermal_cooling_device *cdev)
147 {
148         struct ti_thermal_data *data = thermal->devdata;
149
150         if (!data || IS_ERR(data))
151                 return -ENODEV;
152
153         /* check if this is the cooling device we registered */
154         if (data->cool_dev != cdev)
155                 return 0;
156
157         /* Simple thing, two trips, one passive another critical */
158         return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
159 }
160
161 /* Get mode callback functions for thermal zone */
162 static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
163                                enum thermal_device_mode *mode)
164 {
165         struct ti_thermal_data *data = thermal->devdata;
166
167         if (data)
168                 *mode = data->mode;
169
170         return 0;
171 }
172
173 /* Set mode callback functions for thermal zone */
174 static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
175                                enum thermal_device_mode mode)
176 {
177         struct ti_thermal_data *data = thermal->devdata;
178         struct ti_bandgap *bgp;
179
180         bgp = data->bgp;
181
182         if (!data->ti_thermal) {
183                 dev_notice(&thermal->device, "thermal zone not registered\n");
184                 return 0;
185         }
186
187         mutex_lock(&data->ti_thermal->lock);
188
189         if (mode == THERMAL_DEVICE_ENABLED)
190                 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
191         else
192                 data->ti_thermal->polling_delay = 0;
193
194         mutex_unlock(&data->ti_thermal->lock);
195
196         data->mode = mode;
197         ti_bandgap_write_update_interval(bgp, data->sensor_id,
198                                         data->ti_thermal->polling_delay);
199         thermal_zone_device_update(data->ti_thermal);
200         dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
201                 data->ti_thermal->polling_delay);
202
203         return 0;
204 }
205
206 /* Get trip type callback functions for thermal zone */
207 static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
208                                     int trip, enum thermal_trip_type *type)
209 {
210         if (!ti_thermal_is_valid_trip(trip))
211                 return -EINVAL;
212
213         if (trip + 1 == OMAP_TRIP_NUMBER)
214                 *type = THERMAL_TRIP_CRITICAL;
215         else
216                 *type = THERMAL_TRIP_PASSIVE;
217
218         return 0;
219 }
220
221 /* Get trip temperature callback functions for thermal zone */
222 static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
223                                     int trip, unsigned long *temp)
224 {
225         if (!ti_thermal_is_valid_trip(trip))
226                 return -EINVAL;
227
228         *temp = ti_thermal_get_trip_value(trip);
229
230         return 0;
231 }
232
233 /* Get the temperature trend callback functions for thermal zone */
234 static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
235                                 int trip, enum thermal_trend *trend)
236 {
237         struct ti_thermal_data *data = thermal->devdata;
238         struct ti_bandgap *bgp;
239         int id, tr, ret = 0;
240
241         bgp = data->bgp;
242         id = data->sensor_id;
243
244         ret = ti_bandgap_get_trend(bgp, id, &tr);
245         if (ret)
246                 return ret;
247
248         if (tr > 0)
249                 *trend = THERMAL_TREND_RAISING;
250         else if (tr < 0)
251                 *trend = THERMAL_TREND_DROPPING;
252         else
253                 *trend = THERMAL_TREND_STABLE;
254
255         return 0;
256 }
257
258 /* Get critical temperature callback functions for thermal zone */
259 static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
260                                     unsigned long *temp)
261 {
262         /* shutdown zone */
263         return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
264 }
265
266 static struct thermal_zone_device_ops ti_thermal_ops = {
267         .get_temp = ti_thermal_get_temp,
268         .get_trend = ti_thermal_get_trend,
269         .bind = ti_thermal_bind,
270         .unbind = ti_thermal_unbind,
271         .get_mode = ti_thermal_get_mode,
272         .set_mode = ti_thermal_set_mode,
273         .get_trip_type = ti_thermal_get_trip_type,
274         .get_trip_temp = ti_thermal_get_trip_temp,
275         .get_crit_temp = ti_thermal_get_crit_temp,
276 };
277
278 static struct ti_thermal_data
279 *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
280 {
281         struct ti_thermal_data *data;
282
283         data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
284         if (!data) {
285                 dev_err(bgp->dev, "kzalloc fail\n");
286                 return NULL;
287         }
288         data->sensor_id = id;
289         data->bgp = bgp;
290         data->mode = THERMAL_DEVICE_ENABLED;
291         /* pcb_tz will be either valid or PTR_ERR() */
292         data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
293         INIT_WORK(&data->thermal_wq, ti_thermal_work);
294
295         return data;
296 }
297
298 int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
299                              char *domain)
300 {
301         struct ti_thermal_data *data;
302
303         data = ti_bandgap_get_sensor_data(bgp, id);
304
305         if (!data || IS_ERR(data))
306                 data = ti_thermal_build_data(bgp, id);
307
308         if (!data)
309                 return -EINVAL;
310
311         /* Create thermal zone */
312         data->ti_thermal = thermal_zone_device_register(domain,
313                                 OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
314                                 NULL, FAST_TEMP_MONITORING_RATE,
315                                 FAST_TEMP_MONITORING_RATE);
316         if (IS_ERR(data->ti_thermal)) {
317                 dev_err(bgp->dev, "thermal zone device is NULL\n");
318                 return PTR_ERR(data->ti_thermal);
319         }
320         data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
321         ti_bandgap_set_sensor_data(bgp, id, data);
322         ti_bandgap_write_update_interval(bgp, data->sensor_id,
323                                         data->ti_thermal->polling_delay);
324
325         return 0;
326 }
327
328 int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
329 {
330         struct ti_thermal_data *data;
331
332         data = ti_bandgap_get_sensor_data(bgp, id);
333
334         thermal_zone_device_unregister(data->ti_thermal);
335
336         return 0;
337 }
338
339 int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
340 {
341         struct ti_thermal_data *data;
342
343         data = ti_bandgap_get_sensor_data(bgp, id);
344
345         schedule_work(&data->thermal_wq);
346
347         return 0;
348 }
349
350 int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
351 {
352         struct ti_thermal_data *data;
353
354         data = ti_bandgap_get_sensor_data(bgp, id);
355         if (!data || IS_ERR(data))
356                 data = ti_thermal_build_data(bgp, id);
357
358         if (!data)
359                 return -EINVAL;
360
361         if (!cpufreq_get_current_driver()) {
362                 dev_dbg(bgp->dev, "no cpufreq driver yet\n");
363                 return -EPROBE_DEFER;
364         }
365
366         /* Register cooling device */
367         data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
368         if (IS_ERR(data->cool_dev)) {
369                 dev_err(bgp->dev,
370                         "Failed to register cpufreq cooling device\n");
371                 return PTR_ERR(data->cool_dev);
372         }
373         ti_bandgap_set_sensor_data(bgp, id, data);
374
375         return 0;
376 }
377
378 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
379 {
380         struct ti_thermal_data *data;
381
382         data = ti_bandgap_get_sensor_data(bgp, id);
383         cpufreq_cooling_unregister(data->cool_dev);
384
385         return 0;
386 }