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-2019 Linaro Ltd.
8 * Authors: Hongbo Zhang, Linus Walleij
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_device.h>
17 #include <linux/slab.h>
18 #include <linux/thermal.h>
20 #define PRCMU_DEFAULT_MEASURE_TIME 0xFFF
21 #define PRCMU_DEFAULT_LOW_TEMP 0
24 * db8500_thermal_points - the interpolation points that trigger
27 static const unsigned long db8500_thermal_points
[] = {
43 * This is where things start to get really bad for the
44 * SoC and the thermal zones should be set up to trigger
45 * critical temperature at 85000 mC so we don't get above
54 struct db8500_thermal_zone
{
55 struct thermal_zone_device
*tz
;
56 unsigned long interpolated_temp
;
57 unsigned int cur_index
;
60 /* Callback to get current temperature */
61 static int db8500_thermal_get_temp(void *data
, int *temp
)
63 struct db8500_thermal_zone
*th
= data
;
66 * TODO: There is no PRCMU interface to get temperature data currently,
67 * so a pseudo temperature is returned , it works for thermal framework
68 * and this will be fixed when the PRCMU interface is available.
70 *temp
= th
->interpolated_temp
;
75 static struct thermal_zone_of_device_ops thdev_ops
= {
76 .get_temp
= db8500_thermal_get_temp
,
79 static void db8500_thermal_update_config(struct db8500_thermal_zone
*th
,
81 unsigned long next_low
,
82 unsigned long next_high
)
84 prcmu_stop_temp_sense();
87 th
->interpolated_temp
= (next_low
+ next_high
)/2;
90 * The PRCMU accept absolute temperatures in celsius so divide
91 * down the millicelsius with 1000
93 prcmu_config_hotmon((u8
)(next_low
/1000), (u8
)(next_high
/1000));
94 prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME
);
97 static irqreturn_t
prcmu_low_irq_handler(int irq
, void *irq_data
)
99 struct db8500_thermal_zone
*th
= irq_data
;
100 unsigned int idx
= th
->cur_index
;
101 unsigned long next_low
, next_high
;
104 /* Meaningless for thermal management, ignoring it */
108 next_high
= db8500_thermal_points
[0];
109 next_low
= PRCMU_DEFAULT_LOW_TEMP
;
111 next_high
= db8500_thermal_points
[idx
- 1];
112 next_low
= db8500_thermal_points
[idx
- 2];
116 db8500_thermal_update_config(th
, idx
, next_low
, next_high
);
117 dev_dbg(&th
->tz
->device
,
118 "PRCMU set max %ld, min %ld\n", next_high
, next_low
);
120 thermal_zone_device_update(th
->tz
, THERMAL_EVENT_UNSPECIFIED
);
125 static irqreturn_t
prcmu_high_irq_handler(int irq
, void *irq_data
)
127 struct db8500_thermal_zone
*th
= irq_data
;
128 unsigned int idx
= th
->cur_index
;
129 unsigned long next_low
, next_high
;
130 int num_points
= ARRAY_SIZE(db8500_thermal_points
);
132 if (idx
< num_points
- 1) {
133 next_high
= db8500_thermal_points
[idx
+1];
134 next_low
= db8500_thermal_points
[idx
];
137 db8500_thermal_update_config(th
, idx
, next_low
, next_high
);
139 dev_dbg(&th
->tz
->device
,
140 "PRCMU set max %ld, min %ld\n", next_high
, next_low
);
141 } else if (idx
== num_points
- 1)
142 /* So we roof out 1 degree over the max point */
143 th
->interpolated_temp
= db8500_thermal_points
[idx
] + 1;
145 thermal_zone_device_update(th
->tz
, THERMAL_EVENT_UNSPECIFIED
);
150 static int db8500_thermal_probe(struct platform_device
*pdev
)
152 struct db8500_thermal_zone
*th
= NULL
;
153 struct device
*dev
= &pdev
->dev
;
154 int low_irq
, high_irq
, ret
= 0;
156 th
= devm_kzalloc(dev
, sizeof(*th
), GFP_KERNEL
);
160 low_irq
= platform_get_irq_byname(pdev
, "IRQ_HOTMON_LOW");
164 ret
= devm_request_threaded_irq(dev
, low_irq
, NULL
,
165 prcmu_low_irq_handler
, IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
166 "dbx500_temp_low", th
);
168 dev_err(dev
, "failed to allocate temp low irq\n");
172 high_irq
= platform_get_irq_byname(pdev
, "IRQ_HOTMON_HIGH");
176 ret
= devm_request_threaded_irq(dev
, high_irq
, NULL
,
177 prcmu_high_irq_handler
, IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
178 "dbx500_temp_high", th
);
180 dev_err(dev
, "failed to allocate temp high irq\n");
184 /* register of thermal sensor and get info from DT */
185 th
->tz
= devm_thermal_zone_of_sensor_register(dev
, 0, th
, &thdev_ops
);
186 if (IS_ERR(th
->tz
)) {
187 dev_err(dev
, "register thermal zone sensor failed\n");
188 return PTR_ERR(th
->tz
);
190 dev_info(dev
, "thermal zone sensor registered\n");
192 /* Start measuring at the lowest point */
193 db8500_thermal_update_config(th
, 0, PRCMU_DEFAULT_LOW_TEMP
,
194 db8500_thermal_points
[0]);
196 platform_set_drvdata(pdev
, th
);
201 static int db8500_thermal_suspend(struct platform_device
*pdev
,
204 prcmu_stop_temp_sense();
209 static int db8500_thermal_resume(struct platform_device
*pdev
)
211 struct db8500_thermal_zone
*th
= platform_get_drvdata(pdev
);
213 /* Resume and start measuring at the lowest point */
214 db8500_thermal_update_config(th
, 0, PRCMU_DEFAULT_LOW_TEMP
,
215 db8500_thermal_points
[0]);
220 static const struct of_device_id db8500_thermal_match
[] = {
221 { .compatible
= "stericsson,db8500-thermal" },
224 MODULE_DEVICE_TABLE(of
, db8500_thermal_match
);
226 static struct platform_driver db8500_thermal_driver
= {
228 .name
= "db8500-thermal",
229 .of_match_table
= of_match_ptr(db8500_thermal_match
),
231 .probe
= db8500_thermal_probe
,
232 .suspend
= db8500_thermal_suspend
,
233 .resume
= db8500_thermal_resume
,
236 module_platform_driver(db8500_thermal_driver
);
238 MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
239 MODULE_DESCRIPTION("DB8500 thermal driver");
240 MODULE_LICENSE("GPL");