blob: 5fd03865e396e373d20e3c8e2dd54c79a26955ee [file] [log] [blame]
Eduardo Valentin445eaf82012-07-12 19:02:30 +03001/*
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>
Eduardo Valentin5035d482012-11-13 14:10:02 -040032#include <linux/cpumask.h>
Eduardo Valentin445eaf82012-07-12 19:02:30 +030033#include <linux/cpu_cooling.h>
Eduardo Valentin26d9cc62013-07-04 16:43:54 -040034#include <linux/of.h>
Eduardo Valentin445eaf82012-07-12 19:02:30 +030035
Eduardo Valentin7372add2013-03-19 10:54:19 -040036#include "ti-thermal.h"
37#include "ti-bandgap.h"
Eduardo Valentin445eaf82012-07-12 19:02:30 +030038
39/* common data structures */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040040struct ti_thermal_data {
41 struct thermal_zone_device *ti_thermal;
Eduardo Valentin359836e2013-05-29 15:07:41 +000042 struct thermal_zone_device *pcb_tz;
Eduardo Valentin445eaf82012-07-12 19:02:30 +030043 struct thermal_cooling_device *cool_dev;
Eduardo Valentin03e859d2013-03-19 10:54:21 -040044 struct ti_bandgap *bgp;
Eduardo Valentin445eaf82012-07-12 19:02:30 +030045 enum thermal_device_mode mode;
46 struct work_struct thermal_wq;
47 int sensor_id;
Eduardo Valentin26d9cc62013-07-04 16:43:54 -040048 bool our_zone;
Eduardo Valentin445eaf82012-07-12 19:02:30 +030049};
50
Eduardo Valentin03e859d2013-03-19 10:54:21 -040051static void ti_thermal_work(struct work_struct *work)
Eduardo Valentin445eaf82012-07-12 19:02:30 +030052{
Eduardo Valentin03e859d2013-03-19 10:54:21 -040053 struct ti_thermal_data *data = container_of(work,
54 struct ti_thermal_data, thermal_wq);
Eduardo Valentin445eaf82012-07-12 19:02:30 +030055
Eduardo Valentin03e859d2013-03-19 10:54:21 -040056 thermal_zone_device_update(data->ti_thermal);
Eduardo Valentin445eaf82012-07-12 19:02:30 +030057
Eduardo Valentin03e859d2013-03-19 10:54:21 -040058 dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
59 data->ti_thermal->type);
Eduardo Valentin445eaf82012-07-12 19:02:30 +030060}
61
62/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040063 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
Eduardo Valentin445eaf82012-07-12 19:02:30 +030064 * @t: omap sensor temperature
65 * @s: omap sensor slope value
66 * @c: omap sensor const value
67 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040068static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
Eduardo Valentin445eaf82012-07-12 19:02:30 +030069{
70 int delta = t * s / 1000 + c;
71
72 if (delta < 0)
73 delta = 0;
74
75 return t + delta;
76}
77
78/* thermal zone ops */
79/* Get temperature callback function for thermal zone*/
Eduardo Valentin26d9cc62013-07-04 16:43:54 -040080static inline int __ti_thermal_get_temp(void *devdata, long *temp)
Eduardo Valentin445eaf82012-07-12 19:02:30 +030081{
Eduardo Valentin359836e2013-05-29 15:07:41 +000082 struct thermal_zone_device *pcb_tz = NULL;
Eduardo Valentin26d9cc62013-07-04 16:43:54 -040083 struct ti_thermal_data *data = devdata;
Eduardo Valentin03e859d2013-03-19 10:54:21 -040084 struct ti_bandgap *bgp;
Eduardo Valentin9879b2c2013-03-19 10:54:23 -040085 const struct ti_temp_sensor *s;
Eduardo Valentin359836e2013-05-29 15:07:41 +000086 int ret, tmp, slope, constant;
87 unsigned long pcb_temp;
Eduardo Valentin445eaf82012-07-12 19:02:30 +030088
Eduardo Valentin04a4d102012-09-11 19:06:55 +030089 if (!data)
90 return 0;
91
Eduardo Valentind7f080e2013-03-19 10:54:18 -040092 bgp = data->bgp;
93 s = &bgp->conf->sensors[data->sensor_id];
Eduardo Valentin04a4d102012-09-11 19:06:55 +030094
Eduardo Valentin03e859d2013-03-19 10:54:21 -040095 ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
Eduardo Valentin445eaf82012-07-12 19:02:30 +030096 if (ret)
97 return ret;
98
Eduardo Valentin359836e2013-05-29 15:07:41 +000099 /* Default constants */
100 slope = s->slope;
101 constant = s->constant;
102
103 pcb_tz = data->pcb_tz;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300104 /* In case pcb zone is available, use the extrapolation rule with it */
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000105 if (!IS_ERR(pcb_tz)) {
Eduardo Valentin359836e2013-05-29 15:07:41 +0000106 ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
107 if (!ret) {
108 tmp -= pcb_temp; /* got a valid PCB temp */
109 slope = s->slope_pcb;
110 constant = s->constant_pcb;
111 } else {
112 dev_err(bgp->dev,
113 "Failed to read PCB state. Using defaults\n");
Eduardo Valentindf8f1342013-09-15 15:21:51 -0400114 ret = 0;
Eduardo Valentin359836e2013-05-29 15:07:41 +0000115 }
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300116 }
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400117 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300118
119 return ret;
120}
121
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400122static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,
123 unsigned long *temp)
124{
125 struct ti_thermal_data *data = thermal->devdata;
126
127 return __ti_thermal_get_temp(data, temp);
128}
129
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300130/* Bind callback functions for thermal zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400131static int ti_thermal_bind(struct thermal_zone_device *thermal,
132 struct thermal_cooling_device *cdev)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300133{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400134 struct ti_thermal_data *data = thermal->devdata;
Eduardo Valentin5035d482012-11-13 14:10:02 -0400135 int id;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300136
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000137 if (!data || IS_ERR(data))
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300138 return -ENODEV;
139
140 /* check if this is the cooling device we registered */
141 if (data->cool_dev != cdev)
142 return 0;
143
144 id = data->sensor_id;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300145
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300146 /* Simple thing, two trips, one passive another critical */
Eduardo Valentina7a3b8c2012-08-23 08:37:59 +0800147 return thermal_zone_bind_cooling_device(thermal, 0, cdev,
Eduardo Valentin67b4c472013-04-08 08:19:08 -0400148 /* bind with min and max states defined by cpu_cooling */
Eduardo Valentina7a3b8c2012-08-23 08:37:59 +0800149 THERMAL_NO_LIMIT,
150 THERMAL_NO_LIMIT);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300151}
152
153/* Unbind callback functions for thermal zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400154static int ti_thermal_unbind(struct thermal_zone_device *thermal,
155 struct thermal_cooling_device *cdev)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300156{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400157 struct ti_thermal_data *data = thermal->devdata;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300158
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000159 if (!data || IS_ERR(data))
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300160 return -ENODEV;
161
162 /* check if this is the cooling device we registered */
163 if (data->cool_dev != cdev)
164 return 0;
165
166 /* Simple thing, two trips, one passive another critical */
167 return thermal_zone_unbind_cooling_device(thermal, 0, cdev);
168}
169
170/* Get mode callback functions for thermal zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400171static int ti_thermal_get_mode(struct thermal_zone_device *thermal,
172 enum thermal_device_mode *mode)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300173{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400174 struct ti_thermal_data *data = thermal->devdata;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300175
176 if (data)
177 *mode = data->mode;
178
179 return 0;
180}
181
182/* Set mode callback functions for thermal zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400183static int ti_thermal_set_mode(struct thermal_zone_device *thermal,
184 enum thermal_device_mode mode)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300185{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400186 struct ti_thermal_data *data = thermal->devdata;
Ranganath Krishnan10ccff12013-08-23 11:08:22 -0500187 struct ti_bandgap *bgp;
188
189 bgp = data->bgp;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300190
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400191 if (!data->ti_thermal) {
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300192 dev_notice(&thermal->device, "thermal zone not registered\n");
193 return 0;
194 }
195
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400196 mutex_lock(&data->ti_thermal->lock);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300197
198 if (mode == THERMAL_DEVICE_ENABLED)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400199 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300200 else
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400201 data->ti_thermal->polling_delay = 0;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300202
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400203 mutex_unlock(&data->ti_thermal->lock);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300204
205 data->mode = mode;
Ranganath Krishnan10ccff12013-08-23 11:08:22 -0500206 ti_bandgap_write_update_interval(bgp, data->sensor_id,
207 data->ti_thermal->polling_delay);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400208 thermal_zone_device_update(data->ti_thermal);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300209 dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n",
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400210 data->ti_thermal->polling_delay);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300211
212 return 0;
213}
214
215/* Get trip type callback functions for thermal zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400216static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal,
217 int trip, enum thermal_trip_type *type)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300218{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400219 if (!ti_thermal_is_valid_trip(trip))
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300220 return -EINVAL;
221
222 if (trip + 1 == OMAP_TRIP_NUMBER)
223 *type = THERMAL_TRIP_CRITICAL;
224 else
225 *type = THERMAL_TRIP_PASSIVE;
226
227 return 0;
228}
229
230/* Get trip temperature callback functions for thermal zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400231static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal,
232 int trip, unsigned long *temp)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300233{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400234 if (!ti_thermal_is_valid_trip(trip))
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300235 return -EINVAL;
236
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400237 *temp = ti_thermal_get_trip_value(trip);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300238
239 return 0;
240}
241
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400242static int __ti_thermal_get_trend(void *p, long *trend)
J Keerthy03b7f672013-04-01 12:04:46 -0400243{
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400244 struct ti_thermal_data *data = p;
J Keerthy03b7f672013-04-01 12:04:46 -0400245 struct ti_bandgap *bgp;
246 int id, tr, ret = 0;
247
248 bgp = data->bgp;
249 id = data->sensor_id;
250
251 ret = ti_bandgap_get_trend(bgp, id, &tr);
252 if (ret)
253 return ret;
254
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400255 *trend = tr;
256
257 return 0;
258}
259
260/* Get the temperature trend callback functions for thermal zone */
261static int ti_thermal_get_trend(struct thermal_zone_device *thermal,
262 int trip, enum thermal_trend *trend)
263{
264 int ret;
265 long tr;
266
267 ret = __ti_thermal_get_trend(thermal->devdata, &tr);
268 if (ret)
269 return ret;
270
J Keerthy03b7f672013-04-01 12:04:46 -0400271 if (tr > 0)
272 *trend = THERMAL_TREND_RAISING;
273 else if (tr < 0)
274 *trend = THERMAL_TREND_DROPPING;
275 else
276 *trend = THERMAL_TREND_STABLE;
277
278 return 0;
279}
280
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300281/* Get critical temperature callback functions for thermal zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400282static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal,
283 unsigned long *temp)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300284{
285 /* shutdown zone */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400286 return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300287}
288
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400289static const struct thermal_zone_of_device_ops ti_of_thermal_ops = {
290 .get_temp = __ti_thermal_get_temp,
291 .get_trend = __ti_thermal_get_trend,
292};
293
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400294static struct thermal_zone_device_ops ti_thermal_ops = {
295 .get_temp = ti_thermal_get_temp,
J Keerthy03b7f672013-04-01 12:04:46 -0400296 .get_trend = ti_thermal_get_trend,
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400297 .bind = ti_thermal_bind,
298 .unbind = ti_thermal_unbind,
299 .get_mode = ti_thermal_get_mode,
300 .set_mode = ti_thermal_set_mode,
301 .get_trip_type = ti_thermal_get_trip_type,
302 .get_trip_temp = ti_thermal_get_trip_temp,
303 .get_crit_temp = ti_thermal_get_crit_temp,
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300304};
305
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400306static struct ti_thermal_data
307*ti_thermal_build_data(struct ti_bandgap *bgp, int id)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300308{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400309 struct ti_thermal_data *data;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300310
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400311 data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300312 if (!data) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400313 dev_err(bgp->dev, "kzalloc fail\n");
Eduardo Valentin04a4d102012-09-11 19:06:55 +0300314 return NULL;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300315 }
316 data->sensor_id = id;
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400317 data->bgp = bgp;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300318 data->mode = THERMAL_DEVICE_ENABLED;
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000319 /* pcb_tz will be either valid or PTR_ERR() */
Eduardo Valentin359836e2013-05-29 15:07:41 +0000320 data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400321 INIT_WORK(&data->thermal_wq, ti_thermal_work);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300322
Eduardo Valentin04a4d102012-09-11 19:06:55 +0300323 return data;
324}
325
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400326int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
327 char *domain)
Eduardo Valentin04a4d102012-09-11 19:06:55 +0300328{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400329 struct ti_thermal_data *data;
Eduardo Valentin04a4d102012-09-11 19:06:55 +0300330
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400331 data = ti_bandgap_get_sensor_data(bgp, id);
Eduardo Valentin04a4d102012-09-11 19:06:55 +0300332
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000333 if (!data || IS_ERR(data))
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400334 data = ti_thermal_build_data(bgp, id);
Eduardo Valentin04a4d102012-09-11 19:06:55 +0300335
336 if (!data)
337 return -EINVAL;
338
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400339 /* in case this is specified by DT */
340 data->ti_thermal = thermal_zone_of_sensor_register(bgp->dev, id,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400341 data, &ti_of_thermal_ops);
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400342 if (IS_ERR(data->ti_thermal)) {
343 /* Create thermal zone */
344 data->ti_thermal = thermal_zone_device_register(domain,
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400345 OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,
Durgadoss R50125a92012-09-18 11:04:56 +0530346 NULL, FAST_TEMP_MONITORING_RATE,
Eduardo Valentin765a1932012-09-11 19:06:54 +0300347 FAST_TEMP_MONITORING_RATE);
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400348 if (IS_ERR(data->ti_thermal)) {
349 dev_err(bgp->dev, "thermal zone device is NULL\n");
350 return PTR_ERR(data->ti_thermal);
351 }
352 data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;
353 data->our_zone = true;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300354 }
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400355 ti_bandgap_set_sensor_data(bgp, id, data);
Ranganath Krishnan10ccff12013-08-23 11:08:22 -0500356 ti_bandgap_write_update_interval(bgp, data->sensor_id,
357 data->ti_thermal->polling_delay);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300358
359 return 0;
360}
361
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400362int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300363{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400364 struct ti_thermal_data *data;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300365
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400366 data = ti_bandgap_get_sensor_data(bgp, id);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300367
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400368 if (data && data->ti_thermal) {
369 if (data->our_zone)
370 thermal_zone_device_unregister(data->ti_thermal);
371 else
372 thermal_zone_of_sensor_unregister(bgp->dev,
373 data->ti_thermal);
374 }
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300375
376 return 0;
377}
378
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400379int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300380{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400381 struct ti_thermal_data *data;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300382
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400383 data = ti_bandgap_get_sensor_data(bgp, id);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300384
385 schedule_work(&data->thermal_wq);
386
387 return 0;
388}
389
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400390int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300391{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400392 struct ti_thermal_data *data;
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400393 struct device_node *np = bgp->dev->of_node;
394
395 /*
396 * We are assuming here that if one deploys the zone
397 * using DT, then it must be aware that the cooling device
398 * loading has to happen via cpufreq driver.
399 */
400 if (of_find_property(np, "#thermal-sensor-cells", NULL))
401 return 0;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300402
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400403 data = ti_bandgap_get_sensor_data(bgp, id);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000404 if (!data || IS_ERR(data))
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400405 data = ti_thermal_build_data(bgp, id);
Eduardo Valentin04a4d102012-09-11 19:06:55 +0300406
407 if (!data)
408 return -EINVAL;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300409
Eduardo Valentinf1553332013-04-08 08:19:13 -0400410 if (!cpufreq_get_current_driver()) {
411 dev_dbg(bgp->dev, "no cpufreq driver yet\n");
412 return -EPROBE_DEFER;
413 }
414
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300415 /* Register cooling device */
Eduardo Valentin5035d482012-11-13 14:10:02 -0400416 data->cool_dev = cpufreq_cooling_register(cpu_present_mask);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000417 if (IS_ERR(data->cool_dev)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400418 dev_err(bgp->dev,
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300419 "Failed to register cpufreq cooling device\n");
420 return PTR_ERR(data->cool_dev);
421 }
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400422 ti_bandgap_set_sensor_data(bgp, id, data);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300423
424 return 0;
425}
426
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400427int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300428{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400429 struct ti_thermal_data *data;
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300430
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400431 data = ti_bandgap_get_sensor_data(bgp, id);
Eduardo Valentin26d9cc62013-07-04 16:43:54 -0400432
433 if (data && data->cool_dev)
434 cpufreq_cooling_unregister(data->cool_dev);
Eduardo Valentin445eaf82012-07-12 19:02:30 +0300435
436 return 0;
437}