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 enum thermal_trend trend
;
57 unsigned long interpolated_temp
;
58 unsigned int cur_index
;
61 /* Callback to get current temperature */
62 static int db8500_thermal_get_temp(void *data
, int *temp
)
64 struct db8500_thermal_zone
*th
= data
;
67 * TODO: There is no PRCMU interface to get temperature data currently,
68 * so a pseudo temperature is returned , it works for thermal framework
69 * and this will be fixed when the PRCMU interface is available.
71 *temp
= th
->interpolated_temp
;
76 /* Callback to get temperature changing trend */
77 static int db8500_thermal_get_trend(void *data
, int trip
, enum thermal_trend
*trend
)
79 struct db8500_thermal_zone
*th
= data
;
86 static struct thermal_zone_of_device_ops thdev_ops
= {
87 .get_temp
= db8500_thermal_get_temp
,
88 .get_trend
= db8500_thermal_get_trend
,
91 static void db8500_thermal_update_config(struct db8500_thermal_zone
*th
,
93 enum thermal_trend trend
,
94 unsigned long next_low
,
95 unsigned long next_high
)
97 prcmu_stop_temp_sense();
100 th
->interpolated_temp
= (next_low
+ next_high
)/2;
104 * The PRCMU accept absolute temperatures in celsius so divide
105 * down the millicelsius with 1000
107 prcmu_config_hotmon((u8
)(next_low
/1000), (u8
)(next_high
/1000));
108 prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME
);
111 static irqreturn_t
prcmu_low_irq_handler(int irq
, void *irq_data
)
113 struct db8500_thermal_zone
*th
= irq_data
;
114 unsigned int idx
= th
->cur_index
;
115 unsigned long next_low
, next_high
;
118 /* Meaningless for thermal management, ignoring it */
122 next_high
= db8500_thermal_points
[0];
123 next_low
= PRCMU_DEFAULT_LOW_TEMP
;
125 next_high
= db8500_thermal_points
[idx
- 1];
126 next_low
= db8500_thermal_points
[idx
- 2];
130 db8500_thermal_update_config(th
, idx
, THERMAL_TREND_DROPPING
,
131 next_low
, next_high
);
132 dev_dbg(&th
->tz
->device
,
133 "PRCMU set max %ld, min %ld\n", next_high
, next_low
);
135 thermal_zone_device_update(th
->tz
, THERMAL_EVENT_UNSPECIFIED
);
140 static irqreturn_t
prcmu_high_irq_handler(int irq
, void *irq_data
)
142 struct db8500_thermal_zone
*th
= irq_data
;
143 unsigned int idx
= th
->cur_index
;
144 unsigned long next_low
, next_high
;
145 int num_points
= ARRAY_SIZE(db8500_thermal_points
);
147 if (idx
< num_points
- 1) {
148 next_high
= db8500_thermal_points
[idx
+1];
149 next_low
= db8500_thermal_points
[idx
];
152 db8500_thermal_update_config(th
, idx
, THERMAL_TREND_RAISING
,
153 next_low
, next_high
);
155 dev_dbg(&th
->tz
->device
,
156 "PRCMU set max %ld, min %ld\n", next_high
, next_low
);
157 } else if (idx
== num_points
- 1)
158 /* So we roof out 1 degree over the max point */
159 th
->interpolated_temp
= db8500_thermal_points
[idx
] + 1;
161 thermal_zone_device_update(th
->tz
, THERMAL_EVENT_UNSPECIFIED
);
166 static int db8500_thermal_probe(struct platform_device
*pdev
)
168 struct db8500_thermal_zone
*th
= NULL
;
169 struct device
*dev
= &pdev
->dev
;
170 int low_irq
, high_irq
, ret
= 0;
172 th
= devm_kzalloc(dev
, sizeof(*th
), GFP_KERNEL
);
176 low_irq
= platform_get_irq_byname(pdev
, "IRQ_HOTMON_LOW");
178 dev_err(dev
, "Get IRQ_HOTMON_LOW failed\n");
182 ret
= devm_request_threaded_irq(dev
, low_irq
, NULL
,
183 prcmu_low_irq_handler
, IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
184 "dbx500_temp_low", th
);
186 dev_err(dev
, "failed to allocate temp low irq\n");
190 high_irq
= platform_get_irq_byname(pdev
, "IRQ_HOTMON_HIGH");
192 dev_err(dev
, "Get IRQ_HOTMON_HIGH failed\n");
196 ret
= devm_request_threaded_irq(dev
, high_irq
, NULL
,
197 prcmu_high_irq_handler
, IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
198 "dbx500_temp_high", th
);
200 dev_err(dev
, "failed to allocate temp high irq\n");
204 /* register of thermal sensor and get info from DT */
205 th
->tz
= devm_thermal_zone_of_sensor_register(dev
, 0, th
, &thdev_ops
);
206 if (IS_ERR(th
->tz
)) {
207 dev_err(dev
, "register thermal zone sensor failed\n");
208 return PTR_ERR(th
->tz
);
210 dev_info(dev
, "thermal zone sensor registered\n");
212 /* Start measuring at the lowest point */
213 db8500_thermal_update_config(th
, 0, THERMAL_TREND_STABLE
,
214 PRCMU_DEFAULT_LOW_TEMP
,
215 db8500_thermal_points
[0]);
217 platform_set_drvdata(pdev
, th
);
222 static int db8500_thermal_suspend(struct platform_device
*pdev
,
225 prcmu_stop_temp_sense();
230 static int db8500_thermal_resume(struct platform_device
*pdev
)
232 struct db8500_thermal_zone
*th
= platform_get_drvdata(pdev
);
234 /* Resume and start measuring at the lowest point */
235 db8500_thermal_update_config(th
, 0, THERMAL_TREND_STABLE
,
236 PRCMU_DEFAULT_LOW_TEMP
,
237 db8500_thermal_points
[0]);
242 static const struct of_device_id db8500_thermal_match
[] = {
243 { .compatible
= "stericsson,db8500-thermal" },
246 MODULE_DEVICE_TABLE(of
, db8500_thermal_match
);
248 static struct platform_driver db8500_thermal_driver
= {
250 .name
= "db8500-thermal",
251 .of_match_table
= of_match_ptr(db8500_thermal_match
),
253 .probe
= db8500_thermal_probe
,
254 .suspend
= db8500_thermal_suspend
,
255 .resume
= db8500_thermal_resume
,
258 module_platform_driver(db8500_thermal_driver
);
260 MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
261 MODULE_DESCRIPTION("DB8500 thermal driver");
262 MODULE_LICENSE("GPL");