2 * exynos_thermal_common.c - Samsung EXYNOS common thermal file
4 * Copyright (C) 2013 Samsung Electronics
5 * Amit Daniel Kachhap <amit.daniel@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/cpu_cooling.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/thermal.h>
28 #include "exynos_thermal_common.h"
30 struct exynos_thermal_zone
{
31 enum thermal_device_mode mode
;
32 struct thermal_zone_device
*therm_dev
;
33 struct thermal_cooling_device
*cool_dev
[MAX_COOLING_DEVICE
];
34 unsigned int cool_dev_size
;
35 struct platform_device
*exynos4_dev
;
36 struct thermal_sensor_conf
*sensor_conf
;
40 /* Get mode callback functions for thermal zone */
41 static int exynos_get_mode(struct thermal_zone_device
*thermal
,
42 enum thermal_device_mode
*mode
)
44 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
46 *mode
= th_zone
->mode
;
50 /* Set mode callback functions for thermal zone */
51 static int exynos_set_mode(struct thermal_zone_device
*thermal
,
52 enum thermal_device_mode mode
)
54 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
56 dev_err(&thermal
->device
,
57 "thermal zone not registered\n");
61 mutex_lock(&thermal
->lock
);
63 if (mode
== THERMAL_DEVICE_ENABLED
&&
64 !th_zone
->sensor_conf
->trip_data
.trigger_falling
)
65 thermal
->polling_delay
= IDLE_INTERVAL
;
67 thermal
->polling_delay
= 0;
69 mutex_unlock(&thermal
->lock
);
72 thermal_zone_device_update(thermal
);
73 dev_dbg(th_zone
->sensor_conf
->dev
,
74 "thermal polling set for duration=%d msec\n",
75 thermal
->polling_delay
);
80 /* Get trip type callback functions for thermal zone */
81 static int exynos_get_trip_type(struct thermal_zone_device
*thermal
, int trip
,
82 enum thermal_trip_type
*type
)
84 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
85 int max_trip
= th_zone
->sensor_conf
->trip_data
.trip_count
;
88 if (trip
< 0 || trip
>= max_trip
)
91 trip_type
= th_zone
->sensor_conf
->trip_data
.trip_type
[trip
];
93 if (trip_type
== SW_TRIP
)
94 *type
= THERMAL_TRIP_CRITICAL
;
95 else if (trip_type
== THROTTLE_ACTIVE
)
96 *type
= THERMAL_TRIP_ACTIVE
;
97 else if (trip_type
== THROTTLE_PASSIVE
)
98 *type
= THERMAL_TRIP_PASSIVE
;
105 /* Get trip temperature callback functions for thermal zone */
106 static int exynos_get_trip_temp(struct thermal_zone_device
*thermal
, int trip
,
109 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
110 int max_trip
= th_zone
->sensor_conf
->trip_data
.trip_count
;
112 if (trip
< 0 || trip
>= max_trip
)
115 *temp
= th_zone
->sensor_conf
->trip_data
.trip_val
[trip
];
116 /* convert the temperature into millicelsius */
117 *temp
= *temp
* MCELSIUS
;
122 /* Get critical temperature callback functions for thermal zone */
123 static int exynos_get_crit_temp(struct thermal_zone_device
*thermal
,
126 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
127 int max_trip
= th_zone
->sensor_conf
->trip_data
.trip_count
;
128 /* Get the temp of highest trip*/
129 return exynos_get_trip_temp(thermal
, max_trip
- 1, temp
);
132 /* Bind callback functions for thermal zone */
133 static int exynos_bind(struct thermal_zone_device
*thermal
,
134 struct thermal_cooling_device
*cdev
)
136 int ret
= 0, i
, tab_size
, level
;
137 struct freq_clip_table
*tab_ptr
, *clip_data
;
138 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
139 struct thermal_sensor_conf
*data
= th_zone
->sensor_conf
;
141 tab_ptr
= (struct freq_clip_table
*)data
->cooling_data
.freq_data
;
142 tab_size
= data
->cooling_data
.freq_clip_count
;
144 if (tab_ptr
== NULL
|| tab_size
== 0)
147 /* find the cooling device registered*/
148 for (i
= 0; i
< th_zone
->cool_dev_size
; i
++)
149 if (cdev
== th_zone
->cool_dev
[i
])
152 /* No matching cooling device */
153 if (i
== th_zone
->cool_dev_size
)
156 /* Bind the thermal zone to the cpufreq cooling device */
157 for (i
= 0; i
< tab_size
; i
++) {
158 clip_data
= (struct freq_clip_table
*)&(tab_ptr
[i
]);
159 level
= cpufreq_cooling_get_level(0, clip_data
->freq_clip_max
);
160 if (level
== THERMAL_CSTATE_INVALID
)
162 switch (GET_ZONE(i
)) {
165 if (thermal_zone_bind_cooling_device(thermal
, i
, cdev
,
168 "error unbinding cdev inst=%d\n", i
);
171 th_zone
->bind
= true;
181 /* Unbind callback functions for thermal zone */
182 static int exynos_unbind(struct thermal_zone_device
*thermal
,
183 struct thermal_cooling_device
*cdev
)
185 int ret
= 0, i
, tab_size
;
186 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
187 struct thermal_sensor_conf
*data
= th_zone
->sensor_conf
;
189 if (th_zone
->bind
== false)
192 tab_size
= data
->cooling_data
.freq_clip_count
;
197 /* find the cooling device registered*/
198 for (i
= 0; i
< th_zone
->cool_dev_size
; i
++)
199 if (cdev
== th_zone
->cool_dev
[i
])
202 /* No matching cooling device */
203 if (i
== th_zone
->cool_dev_size
)
206 /* Bind the thermal zone to the cpufreq cooling device */
207 for (i
= 0; i
< tab_size
; i
++) {
208 switch (GET_ZONE(i
)) {
211 if (thermal_zone_unbind_cooling_device(thermal
, i
,
214 "error unbinding cdev inst=%d\n", i
);
217 th_zone
->bind
= false;
226 /* Get temperature callback functions for thermal zone */
227 static int exynos_get_temp(struct thermal_zone_device
*thermal
,
230 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
233 if (!th_zone
->sensor_conf
) {
234 dev_err(&thermal
->device
,
235 "Temperature sensor not initialised\n");
238 data
= th_zone
->sensor_conf
->driver_data
;
239 *temp
= th_zone
->sensor_conf
->read_temperature(data
);
240 /* convert the temperature into millicelsius */
241 *temp
= *temp
* MCELSIUS
;
245 /* Get temperature callback functions for thermal zone */
246 static int exynos_set_emul_temp(struct thermal_zone_device
*thermal
,
251 struct exynos_thermal_zone
*th_zone
= thermal
->devdata
;
253 if (!th_zone
->sensor_conf
) {
254 dev_err(&thermal
->device
,
255 "Temperature sensor not initialised\n");
258 data
= th_zone
->sensor_conf
->driver_data
;
259 if (th_zone
->sensor_conf
->write_emul_temp
)
260 ret
= th_zone
->sensor_conf
->write_emul_temp(data
, temp
);
264 /* Get the temperature trend */
265 static int exynos_get_trend(struct thermal_zone_device
*thermal
,
266 int trip
, enum thermal_trend
*trend
)
269 unsigned long trip_temp
;
271 ret
= exynos_get_trip_temp(thermal
, trip
, &trip_temp
);
275 if (thermal
->temperature
>= trip_temp
)
276 *trend
= THERMAL_TREND_RAISE_FULL
;
278 *trend
= THERMAL_TREND_DROP_FULL
;
282 /* Operation callback functions for thermal zone */
283 static struct thermal_zone_device_ops exynos_dev_ops
= {
285 .unbind
= exynos_unbind
,
286 .get_temp
= exynos_get_temp
,
287 .set_emul_temp
= exynos_set_emul_temp
,
288 .get_trend
= exynos_get_trend
,
289 .get_mode
= exynos_get_mode
,
290 .set_mode
= exynos_set_mode
,
291 .get_trip_type
= exynos_get_trip_type
,
292 .get_trip_temp
= exynos_get_trip_temp
,
293 .get_crit_temp
= exynos_get_crit_temp
,
297 * This function may be called from interrupt based temperature sensor
298 * when threshold is changed.
300 void exynos_report_trigger(struct thermal_sensor_conf
*conf
)
304 char *envp
[] = { data
, NULL
};
305 struct exynos_thermal_zone
*th_zone
;
307 if (!conf
|| !conf
->pzone_data
) {
308 pr_err("Invalid temperature sensor configuration data\n");
312 th_zone
= conf
->pzone_data
;
314 if (th_zone
->bind
== false) {
315 for (i
= 0; i
< th_zone
->cool_dev_size
; i
++) {
316 if (!th_zone
->cool_dev
[i
])
318 exynos_bind(th_zone
->therm_dev
,
319 th_zone
->cool_dev
[i
]);
323 thermal_zone_device_update(th_zone
->therm_dev
);
325 mutex_lock(&th_zone
->therm_dev
->lock
);
326 /* Find the level for which trip happened */
327 for (i
= 0; i
< th_zone
->sensor_conf
->trip_data
.trip_count
; i
++) {
328 if (th_zone
->therm_dev
->last_temperature
<
329 th_zone
->sensor_conf
->trip_data
.trip_val
[i
] * MCELSIUS
)
333 if (th_zone
->mode
== THERMAL_DEVICE_ENABLED
&&
334 !th_zone
->sensor_conf
->trip_data
.trigger_falling
) {
336 th_zone
->therm_dev
->polling_delay
= ACTIVE_INTERVAL
;
338 th_zone
->therm_dev
->polling_delay
= IDLE_INTERVAL
;
341 snprintf(data
, sizeof(data
), "%u", i
);
342 kobject_uevent_env(&th_zone
->therm_dev
->device
.kobj
, KOBJ_CHANGE
, envp
);
343 mutex_unlock(&th_zone
->therm_dev
->lock
);
346 /* Register with the in-kernel thermal management */
347 int exynos_register_thermal(struct thermal_sensor_conf
*sensor_conf
)
350 struct cpumask mask_val
;
351 struct exynos_thermal_zone
*th_zone
;
353 if (!sensor_conf
|| !sensor_conf
->read_temperature
) {
354 pr_err("Temperature sensor not initialised\n");
358 th_zone
= devm_kzalloc(sensor_conf
->dev
,
359 sizeof(struct exynos_thermal_zone
), GFP_KERNEL
);
363 th_zone
->sensor_conf
= sensor_conf
;
365 * TODO: 1) Handle multiple cooling devices in a thermal zone
366 * 2) Add a flag/name in cooling info to map to specific
369 if (sensor_conf
->cooling_data
.freq_clip_count
> 0) {
370 cpumask_set_cpu(0, &mask_val
);
371 th_zone
->cool_dev
[th_zone
->cool_dev_size
] =
372 cpufreq_cooling_register(&mask_val
);
373 if (IS_ERR(th_zone
->cool_dev
[th_zone
->cool_dev_size
])) {
374 dev_err(sensor_conf
->dev
,
375 "Failed to register cpufreq cooling device\n");
379 th_zone
->cool_dev_size
++;
382 th_zone
->therm_dev
= thermal_zone_device_register(
383 sensor_conf
->name
, sensor_conf
->trip_data
.trip_count
,
384 0, th_zone
, &exynos_dev_ops
, NULL
, 0,
385 sensor_conf
->trip_data
.trigger_falling
? 0 :
388 if (IS_ERR(th_zone
->therm_dev
)) {
389 dev_err(sensor_conf
->dev
,
390 "Failed to register thermal zone device\n");
391 ret
= PTR_ERR(th_zone
->therm_dev
);
394 th_zone
->mode
= THERMAL_DEVICE_ENABLED
;
395 sensor_conf
->pzone_data
= th_zone
;
397 dev_info(sensor_conf
->dev
,
398 "Exynos: Thermal zone(%s) registered\n", sensor_conf
->name
);
403 exynos_unregister_thermal(sensor_conf
);
407 /* Un-Register with the in-kernel thermal management */
408 void exynos_unregister_thermal(struct thermal_sensor_conf
*sensor_conf
)
411 struct exynos_thermal_zone
*th_zone
;
413 if (!sensor_conf
|| !sensor_conf
->pzone_data
) {
414 pr_err("Invalid temperature sensor configuration data\n");
418 th_zone
= sensor_conf
->pzone_data
;
420 if (th_zone
->therm_dev
)
421 thermal_zone_device_unregister(th_zone
->therm_dev
);
423 for (i
= 0; i
< th_zone
->cool_dev_size
; i
++) {
424 if (th_zone
->cool_dev
[i
])
425 cpufreq_cooling_unregister(th_zone
->cool_dev
[i
]);
428 dev_info(sensor_conf
->dev
,
429 "Exynos: Kernel Thermal management unregistered\n");