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;
57 unsigned long interpolated_temp;
58 unsigned int cur_index;
61 /* Callback to get current temperature */
62 static int db8500_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
64 struct db8500_thermal_zone *th = thermal_zone_device_priv(tz);
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 static const struct thermal_zone_device_ops thdev_ops = {
77 .get_temp = db8500_thermal_get_temp,
80 static void db8500_thermal_update_config(struct db8500_thermal_zone *th,
82 unsigned long next_low,
83 unsigned long next_high)
85 prcmu_stop_temp_sense();
88 th->interpolated_temp = (next_low + next_high)/2;
91 * The PRCMU accept absolute temperatures in celsius so divide
92 * down the millicelsius with 1000
94 prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000));
95 prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME);
98 static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data)
100 struct db8500_thermal_zone *th = irq_data;
101 unsigned int idx = th->cur_index;
102 unsigned long next_low, next_high;
105 /* Meaningless for thermal management, ignoring it */
109 next_high = db8500_thermal_points[0];
110 next_low = PRCMU_DEFAULT_LOW_TEMP;
112 next_high = db8500_thermal_points[idx - 1];
113 next_low = db8500_thermal_points[idx - 2];
117 db8500_thermal_update_config(th, idx, next_low, next_high);
119 "PRCMU set max %ld, min %ld\n", next_high, next_low);
121 thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
126 static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
128 struct db8500_thermal_zone *th = irq_data;
129 unsigned int idx = th->cur_index;
130 unsigned long next_low, next_high;
131 int num_points = ARRAY_SIZE(db8500_thermal_points);
133 if (idx < num_points - 1) {
134 next_high = db8500_thermal_points[idx+1];
135 next_low = db8500_thermal_points[idx];
138 db8500_thermal_update_config(th, idx, next_low, next_high);
141 "PRCMU set max %ld, min %ld\n", next_high, next_low);
142 } else if (idx == num_points - 1)
143 /* So we roof out 1 degree over the max point */
144 th->interpolated_temp = db8500_thermal_points[idx] + 1;
146 thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
151 static int db8500_thermal_probe(struct platform_device *pdev)
153 struct db8500_thermal_zone *th = NULL;
154 struct device *dev = &pdev->dev;
155 int low_irq, high_irq, ret = 0;
157 th = devm_kzalloc(dev, sizeof(*th), GFP_KERNEL);
163 low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
167 ret = devm_request_threaded_irq(dev, low_irq, NULL,
168 prcmu_low_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
169 "dbx500_temp_low", th);
171 dev_err(dev, "failed to allocate temp low irq\n");
175 high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH");
179 ret = devm_request_threaded_irq(dev, high_irq, NULL,
180 prcmu_high_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
181 "dbx500_temp_high", th);
183 dev_err(dev, "failed to allocate temp high irq\n");
187 /* register of thermal sensor and get info from DT */
188 th->tz = devm_thermal_of_zone_register(dev, 0, th, &thdev_ops);
189 if (IS_ERR(th->tz)) {
190 dev_err(dev, "register thermal zone sensor failed\n");
191 return PTR_ERR(th->tz);
193 dev_info(dev, "thermal zone sensor registered\n");
195 /* Start measuring at the lowest point */
196 db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP,
197 db8500_thermal_points[0]);
199 platform_set_drvdata(pdev, th);
204 static int db8500_thermal_suspend(struct platform_device *pdev,
207 prcmu_stop_temp_sense();
212 static int db8500_thermal_resume(struct platform_device *pdev)
214 struct db8500_thermal_zone *th = platform_get_drvdata(pdev);
216 /* Resume and start measuring at the lowest point */
217 db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP,
218 db8500_thermal_points[0]);
223 static const struct of_device_id db8500_thermal_match[] = {
224 { .compatible = "stericsson,db8500-thermal" },
227 MODULE_DEVICE_TABLE(of, db8500_thermal_match);
229 static struct platform_driver db8500_thermal_driver = {
231 .name = "db8500-thermal",
232 .of_match_table = db8500_thermal_match,
234 .probe = db8500_thermal_probe,
235 .suspend = db8500_thermal_suspend,
236 .resume = db8500_thermal_resume,
239 module_platform_driver(db8500_thermal_driver);
241 MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
242 MODULE_DESCRIPTION("DB8500 thermal driver");
243 MODULE_LICENSE("GPL");