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/cpu_cooling.h>
34 #include "omap-thermal.h"
35 #include "omap-bandgap.h"
37 /* common data structures */
38 struct omap_thermal_data
{
39 struct thermal_zone_device
*omap_thermal
;
40 struct thermal_cooling_device
*cool_dev
;
41 struct omap_bandgap
*bg_ptr
;
42 enum thermal_device_mode mode
;
43 struct work_struct thermal_wq
;
47 static void omap_thermal_work(struct work_struct
*work
)
49 struct omap_thermal_data
*data
= container_of(work
,
50 struct omap_thermal_data
, thermal_wq
);
52 thermal_zone_device_update(data
->omap_thermal
);
54 dev_dbg(&data
->omap_thermal
->device
, "updated thermal zone %s\n",
55 data
->omap_thermal
->type
);
59 * omap_thermal_hotspot_temperature - returns sensor extrapolated temperature
60 * @t: omap sensor temperature
61 * @s: omap sensor slope value
62 * @c: omap sensor const value
64 static inline int omap_thermal_hotspot_temperature(int t
, int s
, int c
)
66 int delta
= t
* s
/ 1000 + c
;
74 /* thermal zone ops */
75 /* Get temperature callback function for thermal zone*/
76 static inline int omap_thermal_get_temp(struct thermal_zone_device
*thermal
,
79 struct omap_thermal_data
*data
= thermal
->devdata
;
80 struct omap_bandgap
*bg_ptr
;
81 struct omap_temp_sensor
*s
;
82 int ret
, tmp
, pcb_temp
, slope
, constant
;
87 bg_ptr
= data
->bg_ptr
;
88 s
= &bg_ptr
->conf
->sensors
[data
->sensor_id
];
90 ret
= omap_bandgap_read_temperature(bg_ptr
, data
->sensor_id
, &tmp
);
95 /* TODO: Introduce pcb temperature lookup */
96 /* In case pcb zone is available, use the extrapolation rule with it */
100 constant
= s
->constant_pcb
;
103 constant
= s
->constant
;
105 *temp
= omap_thermal_hotspot_temperature(tmp
, slope
, constant
);
110 /* Bind callback functions for thermal zone */
111 static int omap_thermal_bind(struct thermal_zone_device
*thermal
,
112 struct thermal_cooling_device
*cdev
)
114 struct omap_thermal_data
*data
= thermal
->devdata
;
117 if (IS_ERR_OR_NULL(data
))
120 /* check if this is the cooling device we registered */
121 if (data
->cool_dev
!= cdev
)
124 id
= data
->sensor_id
;
125 max
= data
->bg_ptr
->conf
->sensors
[id
].cooling_data
.freq_clip_count
;
127 /* TODO: bind with min and max states */
128 /* Simple thing, two trips, one passive another critical */
129 return thermal_zone_bind_cooling_device(thermal
, 0, cdev
,
134 /* Unbind callback functions for thermal zone */
135 static int omap_thermal_unbind(struct thermal_zone_device
*thermal
,
136 struct thermal_cooling_device
*cdev
)
138 struct omap_thermal_data
*data
= thermal
->devdata
;
140 if (IS_ERR_OR_NULL(data
))
143 /* check if this is the cooling device we registered */
144 if (data
->cool_dev
!= cdev
)
147 /* Simple thing, two trips, one passive another critical */
148 return thermal_zone_unbind_cooling_device(thermal
, 0, cdev
);
151 /* Get mode callback functions for thermal zone */
152 static int omap_thermal_get_mode(struct thermal_zone_device
*thermal
,
153 enum thermal_device_mode
*mode
)
155 struct omap_thermal_data
*data
= thermal
->devdata
;
163 /* Set mode callback functions for thermal zone */
164 static int omap_thermal_set_mode(struct thermal_zone_device
*thermal
,
165 enum thermal_device_mode mode
)
167 struct omap_thermal_data
*data
= thermal
->devdata
;
169 if (!data
->omap_thermal
) {
170 dev_notice(&thermal
->device
, "thermal zone not registered\n");
174 mutex_lock(&data
->omap_thermal
->lock
);
176 if (mode
== THERMAL_DEVICE_ENABLED
)
177 data
->omap_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
179 data
->omap_thermal
->polling_delay
= 0;
181 mutex_unlock(&data
->omap_thermal
->lock
);
184 thermal_zone_device_update(data
->omap_thermal
);
185 dev_dbg(&thermal
->device
, "thermal polling set for duration=%d msec\n",
186 data
->omap_thermal
->polling_delay
);
191 /* Get trip type callback functions for thermal zone */
192 static int omap_thermal_get_trip_type(struct thermal_zone_device
*thermal
,
193 int trip
, enum thermal_trip_type
*type
)
195 if (!omap_thermal_is_valid_trip(trip
))
198 if (trip
+ 1 == OMAP_TRIP_NUMBER
)
199 *type
= THERMAL_TRIP_CRITICAL
;
201 *type
= THERMAL_TRIP_PASSIVE
;
206 /* Get trip temperature callback functions for thermal zone */
207 static int omap_thermal_get_trip_temp(struct thermal_zone_device
*thermal
,
208 int trip
, unsigned long *temp
)
210 if (!omap_thermal_is_valid_trip(trip
))
213 *temp
= omap_thermal_get_trip_value(trip
);
218 /* Get critical temperature callback functions for thermal zone */
219 static int omap_thermal_get_crit_temp(struct thermal_zone_device
*thermal
,
223 return omap_thermal_get_trip_temp(thermal
, OMAP_TRIP_NUMBER
- 1, temp
);
226 static struct thermal_zone_device_ops omap_thermal_ops
= {
227 .get_temp
= omap_thermal_get_temp
,
228 /* TODO: add .get_trend */
229 .bind
= omap_thermal_bind
,
230 .unbind
= omap_thermal_unbind
,
231 .get_mode
= omap_thermal_get_mode
,
232 .set_mode
= omap_thermal_set_mode
,
233 .get_trip_type
= omap_thermal_get_trip_type
,
234 .get_trip_temp
= omap_thermal_get_trip_temp
,
235 .get_crit_temp
= omap_thermal_get_crit_temp
,
238 static struct omap_thermal_data
239 *omap_thermal_build_data(struct omap_bandgap
*bg_ptr
, int id
)
241 struct omap_thermal_data
*data
;
243 data
= devm_kzalloc(bg_ptr
->dev
, sizeof(*data
), GFP_KERNEL
);
245 dev_err(bg_ptr
->dev
, "kzalloc fail\n");
248 data
->sensor_id
= id
;
249 data
->bg_ptr
= bg_ptr
;
250 data
->mode
= THERMAL_DEVICE_ENABLED
;
251 INIT_WORK(&data
->thermal_wq
, omap_thermal_work
);
256 int omap_thermal_expose_sensor(struct omap_bandgap
*bg_ptr
, int id
,
259 struct omap_thermal_pdata pdata
;
261 data
= omap_bandgap_get_sensor_data(bg_ptr
, id
);
264 data
= omap_thermal_build_pdata(bg_ptr
, id
);
269 /* TODO: remove TC1 TC2 */
270 /* Create thermal zone */
271 data
->omap_thermal
= thermal_zone_device_register(domain
,
272 OMAP_TRIP_NUMBER
, 0, data
, &omap_thermal_ops
,
273 FAST_TEMP_MONITORING_RATE
,
274 FAST_TEMP_MONITORING_RATE
);
275 if (IS_ERR_OR_NULL(data
->omap_thermal
)) {
276 dev_err(bg_ptr
->dev
, "thermal zone device is NULL\n");
277 return PTR_ERR(data
->omap_thermal
);
279 data
->omap_thermal
->polling_delay
= FAST_TEMP_MONITORING_RATE
;
280 omap_bandgap_set_sensor_data(bg_ptr
, id
, data
);
285 int omap_thermal_remove_sensor(struct omap_bandgap
*bg_ptr
, int id
)
287 struct omap_thermal_data
*data
;
289 data
= omap_bandgap_get_sensor_data(bg_ptr
, id
);
291 thermal_zone_device_unregister(data
->omap_thermal
);
296 int omap_thermal_report_sensor_temperature(struct omap_bandgap
*bg_ptr
, int id
)
298 struct omap_thermal_data
*data
;
300 data
= omap_bandgap_get_sensor_data(bg_ptr
, id
);
302 schedule_work(&data
->thermal_wq
);
307 static int omap_thermal_build_cpufreq_clip(struct omap_bandgap
*bg_ptr
,
308 struct freq_clip_table
**tab_ptr
,
311 struct cpufreq_frequency_table
*freq_table
;
312 struct freq_clip_table
*tab
;
315 freq_table
= cpufreq_frequency_get_table(0);
316 if (IS_ERR_OR_NULL(freq_table
)) {
318 "%s: failed to get cpufreq table (%p)\n",
319 __func__
, freq_table
);
323 for (i
= 0; freq_table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
324 unsigned int freq
= freq_table
[i
].frequency
;
325 if (freq
== CPUFREQ_ENTRY_INVALID
)
330 tab
= devm_kzalloc(bg_ptr
->dev
, sizeof(*tab
) * count
, GFP_KERNEL
);
333 "%s: no memory available\n", __func__
);
337 for (i
= 0; freq_table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
338 unsigned int freq
= freq_table
[i
].frequency
;
340 if (freq
== CPUFREQ_ENTRY_INVALID
)
343 tab
[count
- i
- 1].freq_clip_max
= freq
;
344 tab
[count
- i
- 1].temp_level
= OMAP_TRIP_HOT
;
345 tab
[count
- i
- 1].mask_val
= cpumask_of(0);
354 int omap_thermal_register_cpu_cooling(struct omap_bandgap
*bg_ptr
, int id
)
356 struct omap_thermal_data
*data
;
357 struct freq_clip_table
*tab_ptr
;
360 data
= omap_bandgap_get_sensor_data(bg_ptr
, id
);
362 data
= omap_thermal_build_pdata(bg_ptr
, id
);
367 ret
= omap_thermal_build_cpufreq_clip(bg_ptr
, &tab_ptr
, &tab_size
);
370 "%s: failed to build cpufreq clip table\n", __func__
);
374 /* Register cooling device */
375 data
->cool_dev
= cpufreq_cooling_register(tab_ptr
, tab_size
);
376 if (IS_ERR_OR_NULL(data
->cool_dev
)) {
378 "Failed to register cpufreq cooling device\n");
379 return PTR_ERR(data
->cool_dev
);
381 bg_ptr
->conf
->sensors
[id
].cooling_data
.freq_clip_count
= tab_size
;
382 omap_bandgap_set_sensor_data(bg_ptr
, id
, data
);
387 int omap_thermal_unregister_cpu_cooling(struct omap_bandgap
*bg_ptr
, int id
)
389 struct omap_thermal_data
*data
;
391 data
= omap_bandgap_get_sensor_data(bg_ptr
, id
);
392 cpufreq_cooling_unregister(data
->cool_dev
);