2 * OMAP thermal driver interface
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
6 * Eduardo Valentin <eduardo.valentin@ti.com>
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.
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.
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
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>
36 #include "ti-thermal.h"
37 #include "ti-bandgap.h"
39 /* common data structures */
40 struct ti_thermal_data
{
41 struct thermal_zone_device
*ti_thermal
;
42 struct thermal_zone_device
*pcb_tz
;
43 struct thermal_cooling_device
*cool_dev
;
44 struct ti_bandgap
*bgp
;
45 enum thermal_device_mode mode
;
46 struct work_struct thermal_wq
;
51 static void ti_thermal_work(struct work_struct
*work
)
53 struct ti_thermal_data
*data
= container_of(work
,
54 struct ti_thermal_data
, thermal_wq
);
56 thermal_zone_device_update(data
->ti_thermal
);
58 dev_dbg(&data
->ti_thermal
->device
, "updated thermal zone %s\n",
59 data
->ti_thermal
->type
);
63 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
64 * @t: omap sensor temperature
65 * @s: omap sensor slope value
66 * @c: omap sensor const value
68 static inline int ti_thermal_hotspot_temperature(int t
, int s
, int c
)
70 int delta
= t
* s
/ 1000 + c
;
78 /* thermal zone ops */
79 /* Get temperature callback function for thermal zone*/
80 static inline int __ti_thermal_get_temp(void *devdata
, long *temp
)
82 struct thermal_zone_device
*pcb_tz
= NULL
;
83 struct ti_thermal_data
*data
= devdata
;
84 struct ti_bandgap
*bgp
;
85 const struct ti_temp_sensor
*s
;
86 int ret
, tmp
, slope
, constant
;
87 unsigned long pcb_temp
;
93 s
= &bgp
->conf
->sensors
[data
->sensor_id
];
95 ret
= ti_bandgap_read_temperature(bgp
, data
->sensor_id
, &tmp
);
99 /* Default constants */
101 constant
= s
->constant
;
103 pcb_tz
= data
->pcb_tz
;
104 /* In case pcb zone is available, use the extrapolation rule with it */
105 if (!IS_ERR(pcb_tz
)) {
106 ret
= thermal_zone_get_temp(pcb_tz
, &pcb_temp
);
108 tmp
-= pcb_temp
; /* got a valid PCB temp */
109 slope
= s
->slope_pcb
;
110 constant
= s
->constant_pcb
;
113 "Failed to read PCB state. Using defaults\n");
117 *temp
= ti_thermal_hotspot_temperature(tmp
, slope
, constant
);
122 static inline int ti_thermal_get_temp(struct thermal_zone_device
*thermal
,
125 struct ti_thermal_data
*data
= thermal
->devdata
;
127 return __ti_thermal_get_temp(data
, temp
);
130 /* Bind callback functions for thermal zone */
131 static int ti_thermal_bind(struct thermal_zone_device
*thermal
,
132 struct thermal_cooling_device
*cdev
)
134 struct ti_thermal_data
*data
= thermal
->devdata
;
137 if (!data
|| IS_ERR(data
))
140 /* check if this is the cooling device we registered */
141 if (data
->cool_dev
!= cdev
)
144 id
= data
->sensor_id
;
146 /* Simple thing, two trips, one passive another critical */
147 return thermal_zone_bind_cooling_device(thermal
, 0, cdev
,
148 /* bind with min and max states defined by cpu_cooling */
153 /* Unbind callback functions for thermal zone */
154 static int ti_thermal_unbind(struct thermal_zone_device
*thermal
,
155 struct thermal_cooling_device
*cdev
)
157 struct ti_thermal_data
*data
= thermal
->devdata
;
159 if (!data
|| IS_ERR(data
))
162 /* check if this is the cooling device we registered */
163 if (data
->cool_dev
!= cdev
)
166 /* Simple thing, two trips, one passive another critical */
167 return thermal_zone_unbind_cooling_device(thermal
, 0, cdev
);
170 /* Get mode callback functions for thermal zone */
171 static int ti_thermal_get_mode(struct thermal_zone_device
*thermal
,
172 enum thermal_device_mode
*mode
)
174 struct ti_thermal_data
*data
= thermal
->devdata
;
182 /* Set mode callback functions for thermal zone */
183 static int ti_thermal_set_mode(struct thermal_zone_device
*thermal
,
184 enum thermal_device_mode mode
)
186 struct ti_thermal_data
*data
= thermal
->devdata
;
187 struct ti_bandgap
*bgp
;
191 if (!data
->ti_thermal
) {
192 dev_notice(&thermal
->device
, "thermal zone not registered\n");
196 mutex_lock(&data
->ti_thermal
->lock
);
198 if (mode
== THERMAL_DEVICE_ENABLED
)
199 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
201 data
->ti_thermal
->polling_delay
= 0;
203 mutex_unlock(&data
->ti_thermal
->lock
);
206 ti_bandgap_write_update_interval(bgp
, data
->sensor_id
,
207 data
->ti_thermal
->polling_delay
);
208 thermal_zone_device_update(data
->ti_thermal
);
209 dev_dbg(&thermal
->device
, "thermal polling set for duration=%d msec\n",
210 data
->ti_thermal
->polling_delay
);
215 /* Get trip type callback functions for thermal zone */
216 static int ti_thermal_get_trip_type(struct thermal_zone_device
*thermal
,
217 int trip
, enum thermal_trip_type
*type
)
219 if (!ti_thermal_is_valid_trip(trip
))
222 if (trip
+ 1 == OMAP_TRIP_NUMBER
)
223 *type
= THERMAL_TRIP_CRITICAL
;
225 *type
= THERMAL_TRIP_PASSIVE
;
230 /* Get trip temperature callback functions for thermal zone */
231 static int ti_thermal_get_trip_temp(struct thermal_zone_device
*thermal
,
232 int trip
, unsigned long *temp
)
234 if (!ti_thermal_is_valid_trip(trip
))
237 *temp
= ti_thermal_get_trip_value(trip
);
242 static int __ti_thermal_get_trend(void *p
, long *trend
)
244 struct ti_thermal_data
*data
= p
;
245 struct ti_bandgap
*bgp
;
249 id
= data
->sensor_id
;
251 ret
= ti_bandgap_get_trend(bgp
, id
, &tr
);
260 /* Get the temperature trend callback functions for thermal zone */
261 static int ti_thermal_get_trend(struct thermal_zone_device
*thermal
,
262 int trip
, enum thermal_trend
*trend
)
267 ret
= __ti_thermal_get_trend(thermal
->devdata
, &tr
);
272 *trend
= THERMAL_TREND_RAISING
;
274 *trend
= THERMAL_TREND_DROPPING
;
276 *trend
= THERMAL_TREND_STABLE
;
281 /* Get critical temperature callback functions for thermal zone */
282 static int ti_thermal_get_crit_temp(struct thermal_zone_device
*thermal
,
286 return ti_thermal_get_trip_temp(thermal
, OMAP_TRIP_NUMBER
- 1, temp
);
289 static struct thermal_zone_device_ops ti_thermal_ops
= {
290 .get_temp
= ti_thermal_get_temp
,
291 .get_trend
= ti_thermal_get_trend
,
292 .bind
= ti_thermal_bind
,
293 .unbind
= ti_thermal_unbind
,
294 .get_mode
= ti_thermal_get_mode
,
295 .set_mode
= ti_thermal_set_mode
,
296 .get_trip_type
= ti_thermal_get_trip_type
,
297 .get_trip_temp
= ti_thermal_get_trip_temp
,
298 .get_crit_temp
= ti_thermal_get_crit_temp
,
301 static struct ti_thermal_data
302 *ti_thermal_build_data(struct ti_bandgap
*bgp
, int id
)
304 struct ti_thermal_data
*data
;
306 data
= devm_kzalloc(bgp
->dev
, sizeof(*data
), GFP_KERNEL
);
308 dev_err(bgp
->dev
, "kzalloc fail\n");
311 data
->sensor_id
= id
;
313 data
->mode
= THERMAL_DEVICE_ENABLED
;
314 /* pcb_tz will be either valid or PTR_ERR() */
315 data
->pcb_tz
= thermal_zone_get_zone_by_name("pcb");
316 INIT_WORK(&data
->thermal_wq
, ti_thermal_work
);
321 int ti_thermal_expose_sensor(struct ti_bandgap
*bgp
, int id
,
324 struct ti_thermal_data
*data
;
326 data
= ti_bandgap_get_sensor_data(bgp
, id
);
328 if (!data
|| IS_ERR(data
))
329 data
= ti_thermal_build_data(bgp
, id
);
334 /* in case this is specified by DT */
335 data
->ti_thermal
= thermal_zone_of_sensor_register(bgp
->dev
, id
,
336 data
, __ti_thermal_get_temp
,
337 __ti_thermal_get_trend
);
338 if (IS_ERR(data
->ti_thermal
)) {
339 /* Create thermal zone */
340 data
->ti_thermal
= thermal_zone_device_register(domain
,
341 OMAP_TRIP_NUMBER
, 0, data
, &ti_thermal_ops
,
342 NULL
, FAST_TEMP_MONITORING_RATE
,
343 FAST_TEMP_MONITORING_RATE
);
344 if (IS_ERR(data
->ti_thermal
)) {
345 dev_err(bgp
->dev
, "thermal zone device is NULL\n");
346 return PTR_ERR(data
->ti_thermal
);
348 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
349 data
->our_zone
= true;
351 ti_bandgap_set_sensor_data(bgp
, id
, data
);
352 ti_bandgap_write_update_interval(bgp
, data
->sensor_id
,
353 data
->ti_thermal
->polling_delay
);
358 int ti_thermal_remove_sensor(struct ti_bandgap
*bgp
, int id
)
360 struct ti_thermal_data
*data
;
362 data
= ti_bandgap_get_sensor_data(bgp
, id
);
364 if (data
&& data
->ti_thermal
) {
366 thermal_zone_device_unregister(data
->ti_thermal
);
368 thermal_zone_of_sensor_unregister(bgp
->dev
,
375 int ti_thermal_report_sensor_temperature(struct ti_bandgap
*bgp
, int id
)
377 struct ti_thermal_data
*data
;
379 data
= ti_bandgap_get_sensor_data(bgp
, id
);
381 schedule_work(&data
->thermal_wq
);
386 int ti_thermal_register_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
388 struct ti_thermal_data
*data
;
389 struct device_node
*np
= bgp
->dev
->of_node
;
392 * We are assuming here that if one deploys the zone
393 * using DT, then it must be aware that the cooling device
394 * loading has to happen via cpufreq driver.
396 if (of_find_property(np
, "#thermal-sensor-cells", NULL
))
399 data
= ti_bandgap_get_sensor_data(bgp
, id
);
400 if (!data
|| IS_ERR(data
))
401 data
= ti_thermal_build_data(bgp
, id
);
406 if (!cpufreq_get_current_driver()) {
407 dev_dbg(bgp
->dev
, "no cpufreq driver yet\n");
408 return -EPROBE_DEFER
;
411 /* Register cooling device */
412 data
->cool_dev
= cpufreq_cooling_register(cpu_present_mask
);
413 if (IS_ERR(data
->cool_dev
)) {
415 "Failed to register cpufreq cooling device\n");
416 return PTR_ERR(data
->cool_dev
);
418 ti_bandgap_set_sensor_data(bgp
, id
, data
);
423 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
425 struct ti_thermal_data
*data
;
427 data
= ti_bandgap_get_sensor_data(bgp
, id
);
429 if (data
&& data
->cool_dev
)
430 cpufreq_cooling_unregister(data
->cool_dev
);