1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * db8500_thermal.c - DB8500 Thermal Management Implementation
4  *
5  * Copyright (C) 2012 ST-Ericsson
6  * Copyright (C) 2012-2019 Linaro Ltd.
7  *
8  * Authors: Hongbo Zhang, Linus Walleij
9  */
10 
11 #include <linux/cpu_cooling.h>
12 #include <linux/interrupt.h>
13 #include <linux/mfd/dbx500-prcmu.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/thermal.h>
19 
20 #define PRCMU_DEFAULT_MEASURE_TIME	0xFFF
21 #define PRCMU_DEFAULT_LOW_TEMP		0
22 
23 /**
24  * db8500_thermal_points - the interpolation points that trigger
25  * interrupts
26  */
27 static const unsigned long db8500_thermal_points[] = {
28 	15000,
29 	20000,
30 	25000,
31 	30000,
32 	35000,
33 	40000,
34 	45000,
35 	50000,
36 	55000,
37 	60000,
38 	65000,
39 	70000,
40 	75000,
41 	80000,
42 	/*
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
46 	 * this point.
47 	 */
48 	85000,
49 	90000,
50 	95000,
51 	100000,
52 };
53 
54 struct db8500_thermal_zone {
55 	struct thermal_zone_device *tz;
56 	unsigned long interpolated_temp;
57 	unsigned int cur_index;
58 };
59 
60 /* Callback to get current temperature */
61 static int db8500_thermal_get_temp(void *data, int *temp)
62 {
63 	struct db8500_thermal_zone *th = data;
64 
65 	/*
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.
69 	 */
70 	*temp = th->interpolated_temp;
71 
72 	return 0;
73 }
74 
75 static struct thermal_zone_of_device_ops thdev_ops = {
76 	.get_temp = db8500_thermal_get_temp,
77 };
78 
79 static void db8500_thermal_update_config(struct db8500_thermal_zone *th,
80 					 unsigned int idx,
81 					 unsigned long next_low,
82 					 unsigned long next_high)
83 {
84 	prcmu_stop_temp_sense();
85 
86 	th->cur_index = idx;
87 	th->interpolated_temp = (next_low + next_high)/2;
88 
89 	/*
90 	 * The PRCMU accept absolute temperatures in celsius so divide
91 	 * down the millicelsius with 1000
92 	 */
93 	prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000));
94 	prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME);
95 }
96 
97 static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data)
98 {
99 	struct db8500_thermal_zone *th = irq_data;
100 	unsigned int idx = th->cur_index;
101 	unsigned long next_low, next_high;
102 
103 	if (idx == 0)
104 		/* Meaningless for thermal management, ignoring it */
105 		return IRQ_HANDLED;
106 
107 	if (idx == 1) {
108 		next_high = db8500_thermal_points[0];
109 		next_low = PRCMU_DEFAULT_LOW_TEMP;
110 	} else {
111 		next_high = db8500_thermal_points[idx - 1];
112 		next_low = db8500_thermal_points[idx - 2];
113 	}
114 	idx -= 1;
115 
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);
119 
120 	thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
121 
122 	return IRQ_HANDLED;
123 }
124 
125 static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
126 {
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);
131 
132 	if (idx < num_points - 1) {
133 		next_high = db8500_thermal_points[idx+1];
134 		next_low = db8500_thermal_points[idx];
135 		idx += 1;
136 
137 		db8500_thermal_update_config(th, idx, next_low, next_high);
138 
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;
144 
145 	thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED);
146 
147 	return IRQ_HANDLED;
148 }
149 
150 static int db8500_thermal_probe(struct platform_device *pdev)
151 {
152 	struct db8500_thermal_zone *th = NULL;
153 	struct device *dev = &pdev->dev;
154 	int low_irq, high_irq, ret = 0;
155 
156 	th = devm_kzalloc(dev, sizeof(*th), GFP_KERNEL);
157 	if (!th)
158 		return -ENOMEM;
159 
160 	low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
161 	if (low_irq < 0)
162 		return low_irq;
163 
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);
167 	if (ret < 0) {
168 		dev_err(dev, "failed to allocate temp low irq\n");
169 		return ret;
170 	}
171 
172 	high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH");
173 	if (high_irq < 0)
174 		return high_irq;
175 
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);
179 	if (ret < 0) {
180 		dev_err(dev, "failed to allocate temp high irq\n");
181 		return ret;
182 	}
183 
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);
189 	}
190 	dev_info(dev, "thermal zone sensor registered\n");
191 
192 	/* Start measuring at the lowest point */
193 	db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP,
194 				     db8500_thermal_points[0]);
195 
196 	platform_set_drvdata(pdev, th);
197 
198 	return 0;
199 }
200 
201 static int db8500_thermal_suspend(struct platform_device *pdev,
202 		pm_message_t state)
203 {
204 	prcmu_stop_temp_sense();
205 
206 	return 0;
207 }
208 
209 static int db8500_thermal_resume(struct platform_device *pdev)
210 {
211 	struct db8500_thermal_zone *th = platform_get_drvdata(pdev);
212 
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]);
216 
217 	return 0;
218 }
219 
220 static const struct of_device_id db8500_thermal_match[] = {
221 	{ .compatible = "stericsson,db8500-thermal" },
222 	{},
223 };
224 MODULE_DEVICE_TABLE(of, db8500_thermal_match);
225 
226 static struct platform_driver db8500_thermal_driver = {
227 	.driver = {
228 		.name = "db8500-thermal",
229 		.of_match_table = of_match_ptr(db8500_thermal_match),
230 	},
231 	.probe = db8500_thermal_probe,
232 	.suspend = db8500_thermal_suspend,
233 	.resume = db8500_thermal_resume,
234 };
235 
236 module_platform_driver(db8500_thermal_driver);
237 
238 MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
239 MODULE_DESCRIPTION("DB8500 thermal driver");
240 MODULE_LICENSE("GPL");
241