Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
aa1acb04 | 2 | /* |
3 | * db8500_thermal.c - DB8500 Thermal Management Implementation | |
4 | * | |
5 | * Copyright (C) 2012 ST-Ericsson | |
6c375ecc | 6 | * Copyright (C) 2012-2019 Linaro Ltd. |
aa1acb04 | 7 | * |
6c375ecc | 8 | * Authors: Hongbo Zhang, Linus Walleij |
aa1acb04 | 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> | |
aa1acb04 | 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 | |
cb063a83 | 22 | |
6c375ecc LW |
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, | |
cb063a83 | 52 | }; |
aa1acb04 | 53 | |
54 | struct db8500_thermal_zone { | |
6c375ecc | 55 | struct thermal_zone_device *tz; |
311526b7 | 56 | struct device *dev; |
6c375ecc | 57 | unsigned long interpolated_temp; |
aa1acb04 | 58 | unsigned int cur_index; |
59 | }; | |
60 | ||
aa1acb04 | 61 | /* Callback to get current temperature */ |
2320be60 | 62 | static int db8500_thermal_get_temp(struct thermal_zone_device *tz, int *temp) |
aa1acb04 | 63 | { |
5f68d078 | 64 | struct db8500_thermal_zone *th = thermal_zone_device_priv(tz); |
aa1acb04 | 65 | |
66 | /* | |
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. | |
70 | */ | |
6c375ecc | 71 | *temp = th->interpolated_temp; |
aa1acb04 | 72 | |
73 | return 0; | |
74 | } | |
75 | ||
2320be60 | 76 | static const struct thermal_zone_device_ops thdev_ops = { |
6c375ecc | 77 | .get_temp = db8500_thermal_get_temp, |
aa1acb04 | 78 | }; |
79 | ||
6c375ecc LW |
80 | static void db8500_thermal_update_config(struct db8500_thermal_zone *th, |
81 | unsigned int idx, | |
6c375ecc LW |
82 | unsigned long next_low, |
83 | unsigned long next_high) | |
aa1acb04 | 84 | { |
85 | prcmu_stop_temp_sense(); | |
86 | ||
6c375ecc LW |
87 | th->cur_index = idx; |
88 | th->interpolated_temp = (next_low + next_high)/2; | |
aa1acb04 | 89 | |
6c375ecc LW |
90 | /* |
91 | * The PRCMU accept absolute temperatures in celsius so divide | |
92 | * down the millicelsius with 1000 | |
93 | */ | |
aa1acb04 | 94 | prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000)); |
95 | prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME); | |
96 | } | |
97 | ||
98 | static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data) | |
99 | { | |
6c375ecc LW |
100 | struct db8500_thermal_zone *th = irq_data; |
101 | unsigned int idx = th->cur_index; | |
aa1acb04 | 102 | unsigned long next_low, next_high; |
103 | ||
6c375ecc | 104 | if (idx == 0) |
aa1acb04 | 105 | /* Meaningless for thermal management, ignoring it */ |
106 | return IRQ_HANDLED; | |
107 | ||
108 | if (idx == 1) { | |
6c375ecc | 109 | next_high = db8500_thermal_points[0]; |
aa1acb04 | 110 | next_low = PRCMU_DEFAULT_LOW_TEMP; |
111 | } else { | |
6c375ecc LW |
112 | next_high = db8500_thermal_points[idx - 1]; |
113 | next_low = db8500_thermal_points[idx - 2]; | |
aa1acb04 | 114 | } |
115 | idx -= 1; | |
116 | ||
66a0b101 | 117 | db8500_thermal_update_config(th, idx, next_low, next_high); |
311526b7 | 118 | dev_dbg(th->dev, |
aa1acb04 | 119 | "PRCMU set max %ld, min %ld\n", next_high, next_low); |
120 | ||
6c375ecc | 121 | thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED); |
aa1acb04 | 122 | |
123 | return IRQ_HANDLED; | |
124 | } | |
125 | ||
126 | static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data) | |
127 | { | |
6c375ecc LW |
128 | struct db8500_thermal_zone *th = irq_data; |
129 | unsigned int idx = th->cur_index; | |
aa1acb04 | 130 | unsigned long next_low, next_high; |
6c375ecc | 131 | int num_points = ARRAY_SIZE(db8500_thermal_points); |
aa1acb04 | 132 | |
6c375ecc LW |
133 | if (idx < num_points - 1) { |
134 | next_high = db8500_thermal_points[idx+1]; | |
135 | next_low = db8500_thermal_points[idx]; | |
aa1acb04 | 136 | idx += 1; |
137 | ||
66a0b101 | 138 | db8500_thermal_update_config(th, idx, next_low, next_high); |
aa1acb04 | 139 | |
311526b7 | 140 | dev_dbg(th->dev, |
c56dcfa3 | 141 | "PRCMU set max %ld, min %ld\n", next_high, next_low); |
6c375ecc LW |
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; | |
aa1acb04 | 145 | |
6c375ecc | 146 | thermal_zone_device_update(th->tz, THERMAL_EVENT_UNSPECIFIED); |
aa1acb04 | 147 | |
148 | return IRQ_HANDLED; | |
149 | } | |
150 | ||
aa1acb04 | 151 | static int db8500_thermal_probe(struct platform_device *pdev) |
152 | { | |
6c375ecc | 153 | struct db8500_thermal_zone *th = NULL; |
3de9e4df | 154 | struct device *dev = &pdev->dev; |
aa1acb04 | 155 | int low_irq, high_irq, ret = 0; |
aa1acb04 | 156 | |
6c375ecc LW |
157 | th = devm_kzalloc(dev, sizeof(*th), GFP_KERNEL); |
158 | if (!th) | |
aa1acb04 | 159 | return -ENOMEM; |
160 | ||
311526b7 DL |
161 | th->dev = dev; |
162 | ||
aa1acb04 | 163 | low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW"); |
8cf18eea | 164 | if (low_irq < 0) |
6c375ecc | 165 | return low_irq; |
aa1acb04 | 166 | |
3de9e4df | 167 | ret = devm_request_threaded_irq(dev, low_irq, NULL, |
aa1acb04 | 168 | prcmu_low_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT, |
6c375ecc | 169 | "dbx500_temp_low", th); |
aa1acb04 | 170 | if (ret < 0) { |
6c375ecc LW |
171 | dev_err(dev, "failed to allocate temp low irq\n"); |
172 | return ret; | |
aa1acb04 | 173 | } |
174 | ||
175 | high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH"); | |
8cf18eea | 176 | if (high_irq < 0) |
6c375ecc | 177 | return high_irq; |
aa1acb04 | 178 | |
3de9e4df | 179 | ret = devm_request_threaded_irq(dev, high_irq, NULL, |
aa1acb04 | 180 | prcmu_high_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT, |
6c375ecc | 181 | "dbx500_temp_high", th); |
aa1acb04 | 182 | if (ret < 0) { |
6c375ecc LW |
183 | dev_err(dev, "failed to allocate temp high irq\n"); |
184 | return ret; | |
aa1acb04 | 185 | } |
186 | ||
6c375ecc | 187 | /* register of thermal sensor and get info from DT */ |
2320be60 | 188 | th->tz = devm_thermal_of_zone_register(dev, 0, th, &thdev_ops); |
6c375ecc LW |
189 | if (IS_ERR(th->tz)) { |
190 | dev_err(dev, "register thermal zone sensor failed\n"); | |
191 | return PTR_ERR(th->tz); | |
aa1acb04 | 192 | } |
6c375ecc | 193 | dev_info(dev, "thermal zone sensor registered\n"); |
aa1acb04 | 194 | |
6c375ecc | 195 | /* Start measuring at the lowest point */ |
66a0b101 | 196 | db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP, |
6c375ecc | 197 | db8500_thermal_points[0]); |
aa1acb04 | 198 | |
6c375ecc | 199 | platform_set_drvdata(pdev, th); |
aa1acb04 | 200 | |
201 | return 0; | |
202 | } | |
203 | ||
204 | static int db8500_thermal_suspend(struct platform_device *pdev, | |
205 | pm_message_t state) | |
206 | { | |
aa1acb04 | 207 | prcmu_stop_temp_sense(); |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | static int db8500_thermal_resume(struct platform_device *pdev) | |
213 | { | |
6c375ecc | 214 | struct db8500_thermal_zone *th = platform_get_drvdata(pdev); |
aa1acb04 | 215 | |
6c375ecc | 216 | /* Resume and start measuring at the lowest point */ |
66a0b101 | 217 | db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP, |
6c375ecc | 218 | db8500_thermal_points[0]); |
aa1acb04 | 219 | |
220 | return 0; | |
221 | } | |
222 | ||
aa1acb04 | 223 | static const struct of_device_id db8500_thermal_match[] = { |
224 | { .compatible = "stericsson,db8500-thermal" }, | |
225 | {}, | |
226 | }; | |
8093a116 | 227 | MODULE_DEVICE_TABLE(of, db8500_thermal_match); |
aa1acb04 | 228 | |
229 | static struct platform_driver db8500_thermal_driver = { | |
230 | .driver = { | |
aa1acb04 | 231 | .name = "db8500-thermal", |
c3136376 | 232 | .of_match_table = of_match_ptr(db8500_thermal_match), |
aa1acb04 | 233 | }, |
234 | .probe = db8500_thermal_probe, | |
235 | .suspend = db8500_thermal_suspend, | |
236 | .resume = db8500_thermal_resume, | |
aa1acb04 | 237 | }; |
238 | ||
239 | module_platform_driver(db8500_thermal_driver); | |
240 | ||
241 | MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>"); | |
242 | MODULE_DESCRIPTION("DB8500 thermal driver"); | |
243 | MODULE_LICENSE("GPL"); |