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>
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
;
49 static void ti_thermal_work(struct work_struct
*work
)
51 struct ti_thermal_data
*data
= container_of(work
,
52 struct ti_thermal_data
, thermal_wq
);
54 thermal_zone_device_update(data
->ti_thermal
);
56 dev_dbg(&data
->ti_thermal
->device
, "updated thermal zone %s\n",
57 data
->ti_thermal
->type
);
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
66 static inline int ti_thermal_hotspot_temperature(int t
, int s
, int c
)
68 int delta
= t
* s
/ 1000 + c
;
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
,
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
;
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");
115 *temp
= ti_thermal_hotspot_temperature(tmp
, slope
, constant
);
120 /* Bind callback functions for thermal zone */
121 static int ti_thermal_bind(struct thermal_zone_device
*thermal
,
122 struct thermal_cooling_device
*cdev
)
124 struct ti_thermal_data
*data
= thermal
->devdata
;
127 if (!data
|| IS_ERR(data
))
130 /* check if this is the cooling device we registered */
131 if (data
->cool_dev
!= cdev
)
134 id
= data
->sensor_id
;
136 /* Simple thing, two trips, one passive another critical */
137 return thermal_zone_bind_cooling_device(thermal
, 0, cdev
,
138 /* bind with min and max states defined by cpu_cooling */
143 /* Unbind callback functions for thermal zone */
144 static int ti_thermal_unbind(struct thermal_zone_device
*thermal
,
145 struct thermal_cooling_device
*cdev
)
147 struct ti_thermal_data
*data
= thermal
->devdata
;
149 if (!data
|| IS_ERR(data
))
152 /* check if this is the cooling device we registered */
153 if (data
->cool_dev
!= cdev
)
156 /* Simple thing, two trips, one passive another critical */
157 return thermal_zone_unbind_cooling_device(thermal
, 0, cdev
);
160 /* Get mode callback functions for thermal zone */
161 static int ti_thermal_get_mode(struct thermal_zone_device
*thermal
,
162 enum thermal_device_mode
*mode
)
164 struct ti_thermal_data
*data
= thermal
->devdata
;
172 /* Set mode callback functions for thermal zone */
173 static int ti_thermal_set_mode(struct thermal_zone_device
*thermal
,
174 enum thermal_device_mode mode
)
176 struct ti_thermal_data
*data
= thermal
->devdata
;
177 struct ti_bandgap
*bgp
;
181 if (!data
->ti_thermal
) {
182 dev_notice(&thermal
->device
, "thermal zone not registered\n");
186 mutex_lock(&data
->ti_thermal
->lock
);
188 if (mode
== THERMAL_DEVICE_ENABLED
)
189 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
191 data
->ti_thermal
->polling_delay
= 0;
193 mutex_unlock(&data
->ti_thermal
->lock
);
196 ti_bandgap_write_update_interval(bgp
, data
->sensor_id
,
197 data
->ti_thermal
->polling_delay
);
198 thermal_zone_device_update(data
->ti_thermal
);
199 dev_dbg(&thermal
->device
, "thermal polling set for duration=%d msec\n",
200 data
->ti_thermal
->polling_delay
);
205 /* Get trip type callback functions for thermal zone */
206 static int ti_thermal_get_trip_type(struct thermal_zone_device
*thermal
,
207 int trip
, enum thermal_trip_type
*type
)
209 if (!ti_thermal_is_valid_trip(trip
))
212 if (trip
+ 1 == OMAP_TRIP_NUMBER
)
213 *type
= THERMAL_TRIP_CRITICAL
;
215 *type
= THERMAL_TRIP_PASSIVE
;
220 /* Get trip temperature callback functions for thermal zone */
221 static int ti_thermal_get_trip_temp(struct thermal_zone_device
*thermal
,
222 int trip
, unsigned long *temp
)
224 if (!ti_thermal_is_valid_trip(trip
))
227 *temp
= ti_thermal_get_trip_value(trip
);
232 /* Get the temperature trend callback functions for thermal zone */
233 static int ti_thermal_get_trend(struct thermal_zone_device
*thermal
,
234 int trip
, enum thermal_trend
*trend
)
236 struct ti_thermal_data
*data
= thermal
->devdata
;
237 struct ti_bandgap
*bgp
;
241 id
= data
->sensor_id
;
243 ret
= ti_bandgap_get_trend(bgp
, id
, &tr
);
248 *trend
= THERMAL_TREND_RAISING
;
250 *trend
= THERMAL_TREND_DROPPING
;
252 *trend
= THERMAL_TREND_STABLE
;
257 /* Get critical temperature callback functions for thermal zone */
258 static int ti_thermal_get_crit_temp(struct thermal_zone_device
*thermal
,
262 return ti_thermal_get_trip_temp(thermal
, OMAP_TRIP_NUMBER
- 1, temp
);
265 static struct thermal_zone_device_ops ti_thermal_ops
= {
266 .get_temp
= ti_thermal_get_temp
,
267 .get_trend
= ti_thermal_get_trend
,
268 .bind
= ti_thermal_bind
,
269 .unbind
= ti_thermal_unbind
,
270 .get_mode
= ti_thermal_get_mode
,
271 .set_mode
= ti_thermal_set_mode
,
272 .get_trip_type
= ti_thermal_get_trip_type
,
273 .get_trip_temp
= ti_thermal_get_trip_temp
,
274 .get_crit_temp
= ti_thermal_get_crit_temp
,
277 static struct ti_thermal_data
278 *ti_thermal_build_data(struct ti_bandgap
*bgp
, int id
)
280 struct ti_thermal_data
*data
;
282 data
= devm_kzalloc(bgp
->dev
, sizeof(*data
), GFP_KERNEL
);
284 dev_err(bgp
->dev
, "kzalloc fail\n");
287 data
->sensor_id
= id
;
289 data
->mode
= THERMAL_DEVICE_ENABLED
;
290 /* pcb_tz will be either valid or PTR_ERR() */
291 data
->pcb_tz
= thermal_zone_get_zone_by_name("pcb");
292 INIT_WORK(&data
->thermal_wq
, ti_thermal_work
);
297 int ti_thermal_expose_sensor(struct ti_bandgap
*bgp
, int id
,
300 struct ti_thermal_data
*data
;
302 data
= ti_bandgap_get_sensor_data(bgp
, id
);
304 if (!data
|| IS_ERR(data
))
305 data
= ti_thermal_build_data(bgp
, id
);
310 /* Create thermal zone */
311 data
->ti_thermal
= thermal_zone_device_register(domain
,
312 OMAP_TRIP_NUMBER
, 0, data
, &ti_thermal_ops
,
313 NULL
, FAST_TEMP_MONITORING_RATE
,
314 FAST_TEMP_MONITORING_RATE
);
315 if (IS_ERR(data
->ti_thermal
)) {
316 dev_err(bgp
->dev
, "thermal zone device is NULL\n");
317 return PTR_ERR(data
->ti_thermal
);
319 data
->ti_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
320 ti_bandgap_set_sensor_data(bgp
, id
, data
);
321 ti_bandgap_write_update_interval(bgp
, data
->sensor_id
,
322 data
->ti_thermal
->polling_delay
);
327 int ti_thermal_remove_sensor(struct ti_bandgap
*bgp
, int id
)
329 struct ti_thermal_data
*data
;
331 data
= ti_bandgap_get_sensor_data(bgp
, id
);
333 thermal_zone_device_unregister(data
->ti_thermal
);
338 int ti_thermal_report_sensor_temperature(struct ti_bandgap
*bgp
, int id
)
340 struct ti_thermal_data
*data
;
342 data
= ti_bandgap_get_sensor_data(bgp
, id
);
344 schedule_work(&data
->thermal_wq
);
349 int ti_thermal_register_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
351 struct ti_thermal_data
*data
;
353 data
= ti_bandgap_get_sensor_data(bgp
, id
);
354 if (!data
|| IS_ERR(data
))
355 data
= ti_thermal_build_data(bgp
, id
);
360 if (!cpufreq_get_current_driver()) {
361 dev_dbg(bgp
->dev
, "no cpufreq driver yet\n");
362 return -EPROBE_DEFER
;
365 /* Register cooling device */
366 data
->cool_dev
= cpufreq_cooling_register(cpu_present_mask
);
367 if (IS_ERR(data
->cool_dev
)) {
369 "Failed to register cpufreq cooling device\n");
370 return PTR_ERR(data
->cool_dev
);
372 ti_bandgap_set_sensor_data(bgp
, id
, data
);
377 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
379 struct ti_thermal_data
*data
;
381 data
= ti_bandgap_get_sensor_data(bgp
, id
);
382 cpufreq_cooling_unregister(data
->cool_dev
);