1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * db8500_thermal.c - DB8500 Thermal Management Implementation
5 * Copyright (C) 2012 ST-Ericsson
6 * Copyright (C) 2012 Linaro Ltd.
8 * Author: Hongbo Zhang <hongbo.zhang@linaro.com>
11 #include <linux/cpu_cooling.h>
12 #include <linux/interrupt.h>
13 #include <linux/mfd/dbx500-prcmu.h>
14 #include <linux/module.h>
16 #include <linux/platform_data/db8500_thermal.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/thermal.h>
21 #define PRCMU_DEFAULT_MEASURE_TIME 0xFFF
22 #define PRCMU_DEFAULT_LOW_TEMP 0
24 struct db8500_thermal_zone {
25 struct thermal_zone_device *therm_dev;
27 struct work_struct therm_work;
28 struct db8500_thsens_platform_data *trip_tab;
29 enum thermal_device_mode mode;
30 enum thermal_trend trend;
31 unsigned long cur_temp_pseudo;
32 unsigned int cur_index;
35 /* Local function to check if thermal zone matches cooling devices */
36 static int db8500_thermal_match_cdev(struct thermal_cooling_device *cdev,
37 struct db8500_trip_point *trip_point)
41 if (!strlen(cdev->type))
44 for (i = 0; i < COOLING_DEV_MAX; i++) {
45 if (!strcmp(trip_point->cdev_name[i], cdev->type))
52 /* Callback to bind cooling device to thermal zone */
53 static int db8500_cdev_bind(struct thermal_zone_device *thermal,
54 struct thermal_cooling_device *cdev)
56 struct db8500_thermal_zone *pzone = thermal->devdata;
57 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
58 unsigned long max_state, upper, lower;
61 cdev->ops->get_max_state(cdev, &max_state);
63 for (i = 0; i < ptrips->num_trips; i++) {
64 if (db8500_thermal_match_cdev(cdev, &ptrips->trip_points[i]))
67 upper = lower = i > max_state ? max_state : i;
69 ret = thermal_zone_bind_cooling_device(thermal, i, cdev,
70 upper, lower, THERMAL_WEIGHT_DEFAULT);
72 dev_info(&cdev->device, "%s bind to %d: %d-%s\n", cdev->type,
73 i, ret, ret ? "fail" : "succeed");
79 /* Callback to unbind cooling device from thermal zone */
80 static int db8500_cdev_unbind(struct thermal_zone_device *thermal,
81 struct thermal_cooling_device *cdev)
83 struct db8500_thermal_zone *pzone = thermal->devdata;
84 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
87 for (i = 0; i < ptrips->num_trips; i++) {
88 if (db8500_thermal_match_cdev(cdev, &ptrips->trip_points[i]))
91 ret = thermal_zone_unbind_cooling_device(thermal, i, cdev);
93 dev_info(&cdev->device, "%s unbind from %d: %s\n", cdev->type,
94 i, ret ? "fail" : "succeed");
100 /* Callback to get current temperature */
101 static int db8500_sys_get_temp(struct thermal_zone_device *thermal, int *temp)
103 struct db8500_thermal_zone *pzone = thermal->devdata;
106 * TODO: There is no PRCMU interface to get temperature data currently,
107 * so a pseudo temperature is returned , it works for thermal framework
108 * and this will be fixed when the PRCMU interface is available.
110 *temp = pzone->cur_temp_pseudo;
115 /* Callback to get temperature changing trend */
116 static int db8500_sys_get_trend(struct thermal_zone_device *thermal,
117 int trip, enum thermal_trend *trend)
119 struct db8500_thermal_zone *pzone = thermal->devdata;
121 *trend = pzone->trend;
126 /* Callback to get thermal zone mode */
127 static int db8500_sys_get_mode(struct thermal_zone_device *thermal,
128 enum thermal_device_mode *mode)
130 struct db8500_thermal_zone *pzone = thermal->devdata;
132 mutex_lock(&pzone->th_lock);
134 mutex_unlock(&pzone->th_lock);
139 /* Callback to set thermal zone mode */
140 static int db8500_sys_set_mode(struct thermal_zone_device *thermal,
141 enum thermal_device_mode mode)
143 struct db8500_thermal_zone *pzone = thermal->devdata;
145 mutex_lock(&pzone->th_lock);
148 if (mode == THERMAL_DEVICE_ENABLED)
149 schedule_work(&pzone->therm_work);
151 mutex_unlock(&pzone->th_lock);
156 /* Callback to get trip point type */
157 static int db8500_sys_get_trip_type(struct thermal_zone_device *thermal,
158 int trip, enum thermal_trip_type *type)
160 struct db8500_thermal_zone *pzone = thermal->devdata;
161 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
163 if (trip >= ptrips->num_trips)
166 *type = ptrips->trip_points[trip].type;
171 /* Callback to get trip point temperature */
172 static int db8500_sys_get_trip_temp(struct thermal_zone_device *thermal,
175 struct db8500_thermal_zone *pzone = thermal->devdata;
176 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
178 if (trip >= ptrips->num_trips)
181 *temp = ptrips->trip_points[trip].temp;
186 /* Callback to get critical trip point temperature */
187 static int db8500_sys_get_crit_temp(struct thermal_zone_device *thermal,
190 struct db8500_thermal_zone *pzone = thermal->devdata;
191 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
194 for (i = ptrips->num_trips - 1; i > 0; i--) {
195 if (ptrips->trip_points[i].type == THERMAL_TRIP_CRITICAL) {
196 *temp = ptrips->trip_points[i].temp;
204 static struct thermal_zone_device_ops thdev_ops = {
205 .bind = db8500_cdev_bind,
206 .unbind = db8500_cdev_unbind,
207 .get_temp = db8500_sys_get_temp,
208 .get_trend = db8500_sys_get_trend,
209 .get_mode = db8500_sys_get_mode,
210 .set_mode = db8500_sys_set_mode,
211 .get_trip_type = db8500_sys_get_trip_type,
212 .get_trip_temp = db8500_sys_get_trip_temp,
213 .get_crit_temp = db8500_sys_get_crit_temp,
216 static void db8500_thermal_update_config(struct db8500_thermal_zone *pzone,
217 unsigned int idx, enum thermal_trend trend,
218 unsigned long next_low, unsigned long next_high)
220 prcmu_stop_temp_sense();
222 pzone->cur_index = idx;
223 pzone->cur_temp_pseudo = (next_low + next_high)/2;
224 pzone->trend = trend;
226 prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000));
227 prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME);
230 static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data)
232 struct db8500_thermal_zone *pzone = irq_data;
233 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
234 unsigned int idx = pzone->cur_index;
235 unsigned long next_low, next_high;
237 if (unlikely(idx == 0))
238 /* Meaningless for thermal management, ignoring it */
242 next_high = ptrips->trip_points[0].temp;
243 next_low = PRCMU_DEFAULT_LOW_TEMP;
245 next_high = ptrips->trip_points[idx-1].temp;
246 next_low = ptrips->trip_points[idx-2].temp;
250 db8500_thermal_update_config(pzone, idx, THERMAL_TREND_DROPPING,
251 next_low, next_high);
253 dev_dbg(&pzone->therm_dev->device,
254 "PRCMU set max %ld, min %ld\n", next_high, next_low);
256 schedule_work(&pzone->therm_work);
261 static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
263 struct db8500_thermal_zone *pzone = irq_data;
264 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
265 unsigned int idx = pzone->cur_index;
266 unsigned long next_low, next_high;
268 if (idx < ptrips->num_trips - 1) {
269 next_high = ptrips->trip_points[idx+1].temp;
270 next_low = ptrips->trip_points[idx].temp;
273 db8500_thermal_update_config(pzone, idx, THERMAL_TREND_RAISING,
274 next_low, next_high);
276 dev_dbg(&pzone->therm_dev->device,
277 "PRCMU set max %ld, min %ld\n", next_high, next_low);
278 } else if (idx == ptrips->num_trips - 1)
279 pzone->cur_temp_pseudo = ptrips->trip_points[idx].temp + 1;
281 schedule_work(&pzone->therm_work);
286 static void db8500_thermal_work(struct work_struct *work)
288 enum thermal_device_mode cur_mode;
289 struct db8500_thermal_zone *pzone;
291 pzone = container_of(work, struct db8500_thermal_zone, therm_work);
293 mutex_lock(&pzone->th_lock);
294 cur_mode = pzone->mode;
295 mutex_unlock(&pzone->th_lock);
297 if (cur_mode == THERMAL_DEVICE_DISABLED)
300 thermal_zone_device_update(pzone->therm_dev, THERMAL_EVENT_UNSPECIFIED);
301 dev_dbg(&pzone->therm_dev->device, "thermal work finished.\n");
305 static struct db8500_thsens_platform_data*
306 db8500_thermal_parse_dt(struct platform_device *pdev)
308 struct db8500_thsens_platform_data *ptrips;
309 struct device_node *np = pdev->dev.of_node;
315 ptrips = devm_kzalloc(&pdev->dev, sizeof(*ptrips), GFP_KERNEL);
319 if (of_property_read_u32(np, "num-trips", &tmp_data))
322 if (tmp_data > THERMAL_MAX_TRIPS)
325 ptrips->num_trips = tmp_data;
327 for (i = 0; i < ptrips->num_trips; i++) {
328 sprintf(prop_name, "trip%d-temp", i);
329 if (of_property_read_u32(np, prop_name, &tmp_data))
332 ptrips->trip_points[i].temp = tmp_data;
334 sprintf(prop_name, "trip%d-type", i);
335 if (of_property_read_string(np, prop_name, &tmp_str))
338 if (!strcmp(tmp_str, "active"))
339 ptrips->trip_points[i].type = THERMAL_TRIP_ACTIVE;
340 else if (!strcmp(tmp_str, "passive"))
341 ptrips->trip_points[i].type = THERMAL_TRIP_PASSIVE;
342 else if (!strcmp(tmp_str, "hot"))
343 ptrips->trip_points[i].type = THERMAL_TRIP_HOT;
344 else if (!strcmp(tmp_str, "critical"))
345 ptrips->trip_points[i].type = THERMAL_TRIP_CRITICAL;
349 sprintf(prop_name, "trip%d-cdev-num", i);
350 if (of_property_read_u32(np, prop_name, &tmp_data))
353 if (tmp_data > COOLING_DEV_MAX)
356 for (j = 0; j < tmp_data; j++) {
357 sprintf(prop_name, "trip%d-cdev-name%d", i, j);
358 if (of_property_read_string(np, prop_name, &tmp_str))
361 if (strlen(tmp_str) >= THERMAL_NAME_LENGTH)
364 strcpy(ptrips->trip_points[i].cdev_name[j], tmp_str);
370 dev_err(&pdev->dev, "Parsing device tree data error.\n");
374 static inline struct db8500_thsens_platform_data*
375 db8500_thermal_parse_dt(struct platform_device *pdev)
381 static int db8500_thermal_probe(struct platform_device *pdev)
383 struct db8500_thermal_zone *pzone = NULL;
384 struct db8500_thsens_platform_data *ptrips = NULL;
385 struct device_node *np = pdev->dev.of_node;
386 int low_irq, high_irq, ret = 0;
387 unsigned long dft_low, dft_high;
390 ptrips = db8500_thermal_parse_dt(pdev);
392 ptrips = dev_get_platdata(&pdev->dev);
397 pzone = devm_kzalloc(&pdev->dev, sizeof(*pzone), GFP_KERNEL);
401 mutex_init(&pzone->th_lock);
402 mutex_lock(&pzone->th_lock);
404 pzone->mode = THERMAL_DEVICE_DISABLED;
405 pzone->trip_tab = ptrips;
407 INIT_WORK(&pzone->therm_work, db8500_thermal_work);
409 low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
411 dev_err(&pdev->dev, "Get IRQ_HOTMON_LOW failed.\n");
416 ret = devm_request_threaded_irq(&pdev->dev, low_irq, NULL,
417 prcmu_low_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
418 "dbx500_temp_low", pzone);
420 dev_err(&pdev->dev, "Failed to allocate temp low irq.\n");
424 high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH");
426 dev_err(&pdev->dev, "Get IRQ_HOTMON_HIGH failed.\n");
431 ret = devm_request_threaded_irq(&pdev->dev, high_irq, NULL,
432 prcmu_high_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
433 "dbx500_temp_high", pzone);
435 dev_err(&pdev->dev, "Failed to allocate temp high irq.\n");
439 pzone->therm_dev = thermal_zone_device_register("db8500_thermal_zone",
440 ptrips->num_trips, 0, pzone, &thdev_ops, NULL, 0, 0);
442 if (IS_ERR(pzone->therm_dev)) {
443 dev_err(&pdev->dev, "Register thermal zone device failed.\n");
444 ret = PTR_ERR(pzone->therm_dev);
447 dev_info(&pdev->dev, "Thermal zone device registered.\n");
449 dft_low = PRCMU_DEFAULT_LOW_TEMP;
450 dft_high = ptrips->trip_points[0].temp;
452 db8500_thermal_update_config(pzone, 0, THERMAL_TREND_STABLE,
455 platform_set_drvdata(pdev, pzone);
456 pzone->mode = THERMAL_DEVICE_ENABLED;
459 mutex_unlock(&pzone->th_lock);
464 static int db8500_thermal_remove(struct platform_device *pdev)
466 struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
468 thermal_zone_device_unregister(pzone->therm_dev);
469 cancel_work_sync(&pzone->therm_work);
470 mutex_destroy(&pzone->th_lock);
475 static int db8500_thermal_suspend(struct platform_device *pdev,
478 struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
480 flush_work(&pzone->therm_work);
481 prcmu_stop_temp_sense();
486 static int db8500_thermal_resume(struct platform_device *pdev)
488 struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
489 struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
490 unsigned long dft_low, dft_high;
492 dft_low = PRCMU_DEFAULT_LOW_TEMP;
493 dft_high = ptrips->trip_points[0].temp;
495 db8500_thermal_update_config(pzone, 0, THERMAL_TREND_STABLE,
502 static const struct of_device_id db8500_thermal_match[] = {
503 { .compatible = "stericsson,db8500-thermal" },
506 MODULE_DEVICE_TABLE(of, db8500_thermal_match);
509 static struct platform_driver db8500_thermal_driver = {
511 .name = "db8500-thermal",
512 .of_match_table = of_match_ptr(db8500_thermal_match),
514 .probe = db8500_thermal_probe,
515 .suspend = db8500_thermal_suspend,
516 .resume = db8500_thermal_resume,
517 .remove = db8500_thermal_remove,
520 module_platform_driver(db8500_thermal_driver);
522 MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
523 MODULE_DESCRIPTION("DB8500 thermal driver");
524 MODULE_LICENSE("GPL");