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/cpumask.h>
32 #include <linux/cpu_cooling.h>
35 #include "ti-thermal.h"
36 #include "ti-bandgap.h"
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
;
50 static void ti_thermal_work(struct work_struct
*work
)
52 struct ti_thermal_data
*data
= container_of(work
,
53 struct ti_thermal_data
, thermal_wq
);
55 thermal_zone_device_update(data
->ti_thermal
);
57 dev_dbg(&data
->ti_thermal
->device
, "updated thermal zone %s\n",
58 data
->ti_thermal
->type
);
62 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
63 * @t: omap sensor temperature
64 * @s: omap sensor slope value
65 * @c: omap sensor const value
67 static inline int ti_thermal_hotspot_temperature(int t
, int s
, int c
)
69 int delta
= t
* s
/ 1000 + c
;
77 /* thermal zone ops */
78 /* Get temperature callback function for thermal zone*/
79 static inline int __ti_thermal_get_temp(void *devdata
, long *temp
)
81 struct thermal_zone_device
*pcb_tz
= NULL
;
82 struct ti_thermal_data
*data
= devdata
;
83 struct ti_bandgap
*bgp
;
84 const struct ti_temp_sensor
*s
;
85 int ret
, tmp
, slope
, constant
;
86 unsigned long pcb_temp
;
92 s
= &bgp
->conf
->sensors
[data
->sensor_id
];
94 ret
= ti_bandgap_read_temperature(bgp
, data
->sensor_id
, &tmp
);
98 /* Default constants */
100 constant
= s
->constant
;
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
);
107 tmp
-= pcb_temp
; /* got a valid PCB temp */
108 slope
= s
->slope_pcb
;
109 constant
= s
->constant_pcb
;
112 "Failed to read PCB state. Using defaults\n");
116 *temp
= ti_thermal_hotspot_temperature(tmp
, slope
, constant
);
121 static inline int ti_thermal_get_temp(struct thermal_zone_device
*thermal
,
124 struct ti_thermal_data
*data
= thermal
->devdata
;
126 return __ti_thermal_get_temp(data
, temp
);
129 /* Bind callback functions for thermal zone */
130 static int ti_thermal_bind(struct thermal_zone_device
*thermal
,
131 struct thermal_cooling_device
*cdev
)
133 struct ti_thermal_data
*data
= thermal
->devdata
;
136 if (!data
|| IS_ERR(data
))
139 /* check if this is the cooling device we registered */
140 if (data
->cool_dev
!= cdev
)
143 id
= data
->sensor_id
;
145 /* Simple thing, two trips, one passive another critical */
146 return thermal_zone_bind_cooling_device(thermal
, 0, cdev
,
147 /* bind with min and max states defined by cpu_cooling */
152 /* Unbind callback functions for thermal zone */
153 static int ti_thermal_unbind(struct thermal_zone_device
*thermal
,
154 struct thermal_cooling_device
*cdev
)
156 struct ti_thermal_data
*data
= thermal
->devdata
;
158 if (!data
|| IS_ERR(data
))
161 /* check if this is the cooling device we registered */
162 if (data
->cool_dev
!= cdev
)
165 /* Simple thing, two trips, one passive another critical */
166 return thermal_zone_unbind_cooling_device(thermal
, 0, cdev
);
169 /* Get mode callback functions for thermal zone */
170 static int ti_thermal_get_mode(struct thermal_zone_device
*thermal
,
171 enum thermal_device_mode
*mode
)
173 struct ti_thermal_data
*data
= thermal
->devdata
;
181 /* Set mode callback functions for thermal zone */
182 static int ti_thermal_set_mode(struct thermal_zone_device
*thermal
,
183 enum thermal_device_mode mode
)
185 struct ti_thermal_data
*data
= thermal
->devdata
;
186 struct ti_bandgap
*bgp
;
190 if (!data
->ti_thermal
) {
191 dev_notice(&thermal
->device
, "thermal zone not registered\n");
195 mutex_lock(&data
->ti_thermal
->lock
);
197 if (mode
== THERMAL_DEVICE_ENABLED
)
198 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
200 data
->ti_thermal
->polling_delay
= 0;
202 mutex_unlock(&data
->ti_thermal
->lock
);
205 ti_bandgap_write_update_interval(bgp
, data
->sensor_id
,
206 data
->ti_thermal
->polling_delay
);
207 thermal_zone_device_update(data
->ti_thermal
);
208 dev_dbg(&thermal
->device
, "thermal polling set for duration=%d msec\n",
209 data
->ti_thermal
->polling_delay
);
214 /* Get trip type callback functions for thermal zone */
215 static int ti_thermal_get_trip_type(struct thermal_zone_device
*thermal
,
216 int trip
, enum thermal_trip_type
*type
)
218 if (!ti_thermal_is_valid_trip(trip
))
221 if (trip
+ 1 == OMAP_TRIP_NUMBER
)
222 *type
= THERMAL_TRIP_CRITICAL
;
224 *type
= THERMAL_TRIP_PASSIVE
;
229 /* Get trip temperature callback functions for thermal zone */
230 static int ti_thermal_get_trip_temp(struct thermal_zone_device
*thermal
,
231 int trip
, unsigned long *temp
)
233 if (!ti_thermal_is_valid_trip(trip
))
236 *temp
= ti_thermal_get_trip_value(trip
);
241 static int __ti_thermal_get_trend(void *p
, long *trend
)
243 struct ti_thermal_data
*data
= p
;
244 struct ti_bandgap
*bgp
;
248 id
= data
->sensor_id
;
250 ret
= ti_bandgap_get_trend(bgp
, id
, &tr
);
259 /* Get the temperature trend callback functions for thermal zone */
260 static int ti_thermal_get_trend(struct thermal_zone_device
*thermal
,
261 int trip
, enum thermal_trend
*trend
)
266 ret
= __ti_thermal_get_trend(thermal
->devdata
, &tr
);
271 *trend
= THERMAL_TREND_RAISING
;
273 *trend
= THERMAL_TREND_DROPPING
;
275 *trend
= THERMAL_TREND_STABLE
;
280 /* Get critical temperature callback functions for thermal zone */
281 static int ti_thermal_get_crit_temp(struct thermal_zone_device
*thermal
,
285 return ti_thermal_get_trip_temp(thermal
, OMAP_TRIP_NUMBER
- 1, temp
);
288 static const struct thermal_zone_of_device_ops ti_of_thermal_ops
= {
289 .get_temp
= __ti_thermal_get_temp
,
290 .get_trend
= __ti_thermal_get_trend
,
293 static struct thermal_zone_device_ops ti_thermal_ops
= {
294 .get_temp
= ti_thermal_get_temp
,
295 .get_trend
= ti_thermal_get_trend
,
296 .bind
= ti_thermal_bind
,
297 .unbind
= ti_thermal_unbind
,
298 .get_mode
= ti_thermal_get_mode
,
299 .set_mode
= ti_thermal_set_mode
,
300 .get_trip_type
= ti_thermal_get_trip_type
,
301 .get_trip_temp
= ti_thermal_get_trip_temp
,
302 .get_crit_temp
= ti_thermal_get_crit_temp
,
305 static struct ti_thermal_data
306 *ti_thermal_build_data(struct ti_bandgap
*bgp
, int id
)
308 struct ti_thermal_data
*data
;
310 data
= devm_kzalloc(bgp
->dev
, sizeof(*data
), GFP_KERNEL
);
312 dev_err(bgp
->dev
, "kzalloc fail\n");
315 data
->sensor_id
= id
;
317 data
->mode
= THERMAL_DEVICE_ENABLED
;
318 /* pcb_tz will be either valid or PTR_ERR() */
319 data
->pcb_tz
= thermal_zone_get_zone_by_name("pcb");
320 INIT_WORK(&data
->thermal_wq
, ti_thermal_work
);
325 int ti_thermal_expose_sensor(struct ti_bandgap
*bgp
, int id
,
328 struct ti_thermal_data
*data
;
330 data
= ti_bandgap_get_sensor_data(bgp
, id
);
332 if (!data
|| IS_ERR(data
))
333 data
= ti_thermal_build_data(bgp
, id
);
338 /* in case this is specified by DT */
339 data
->ti_thermal
= thermal_zone_of_sensor_register(bgp
->dev
, id
,
340 data
, &ti_of_thermal_ops
);
341 if (IS_ERR(data
->ti_thermal
)) {
342 /* Create thermal zone */
343 data
->ti_thermal
= thermal_zone_device_register(domain
,
344 OMAP_TRIP_NUMBER
, 0, data
, &ti_thermal_ops
,
345 NULL
, FAST_TEMP_MONITORING_RATE
,
346 FAST_TEMP_MONITORING_RATE
);
347 if (IS_ERR(data
->ti_thermal
)) {
348 dev_err(bgp
->dev
, "thermal zone device is NULL\n");
349 return PTR_ERR(data
->ti_thermal
);
351 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
352 data
->our_zone
= true;
354 ti_bandgap_set_sensor_data(bgp
, id
, data
);
355 ti_bandgap_write_update_interval(bgp
, data
->sensor_id
,
356 data
->ti_thermal
->polling_delay
);
361 int ti_thermal_remove_sensor(struct ti_bandgap
*bgp
, int id
)
363 struct ti_thermal_data
*data
;
365 data
= ti_bandgap_get_sensor_data(bgp
, id
);
367 if (data
&& data
->ti_thermal
) {
369 thermal_zone_device_unregister(data
->ti_thermal
);
371 thermal_zone_of_sensor_unregister(bgp
->dev
,
378 int ti_thermal_report_sensor_temperature(struct ti_bandgap
*bgp
, int id
)
380 struct ti_thermal_data
*data
;
382 data
= ti_bandgap_get_sensor_data(bgp
, id
);
384 schedule_work(&data
->thermal_wq
);
389 int ti_thermal_register_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
391 struct ti_thermal_data
*data
;
392 struct device_node
*np
= bgp
->dev
->of_node
;
395 * We are assuming here that if one deploys the zone
396 * using DT, then it must be aware that the cooling device
397 * loading has to happen via cpufreq driver.
399 if (of_find_property(np
, "#thermal-sensor-cells", NULL
))
402 data
= ti_bandgap_get_sensor_data(bgp
, id
);
403 if (!data
|| IS_ERR(data
))
404 data
= ti_thermal_build_data(bgp
, id
);
409 /* Register cooling device */
410 data
->cool_dev
= cpufreq_cooling_register(cpu_present_mask
);
411 if (IS_ERR(data
->cool_dev
)) {
412 int ret
= PTR_ERR(data
->cool_dev
);
414 if (ret
!= -EPROBE_DEFER
)
416 "Failed to register cpu cooling device %d\n",
421 ti_bandgap_set_sensor_data(bgp
, id
, data
);
426 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
428 struct ti_thermal_data
*data
;
430 data
= ti_bandgap_get_sensor_data(bgp
, id
);
433 cpufreq_cooling_unregister(data
->cool_dev
);