Commit | Line | Data |
---|---|---|
445eaf87 EV |
1 | /* |
2 | * OMAP thermal driver interface | |
3 | * | |
4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ | |
5 | * Contact: | |
6 | * Eduardo Valentin <eduardo.valentin@ti.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
20 | * 02110-1301 USA | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/device.h> | |
25 | #include <linux/err.h> | |
26 | #include <linux/mutex.h> | |
27 | #include <linux/gfp.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/workqueue.h> | |
30 | #include <linux/thermal.h> | |
4d753aa7 | 31 | #include <linux/cpufreq.h> |
5035d48d | 32 | #include <linux/cpumask.h> |
445eaf87 | 33 | #include <linux/cpu_cooling.h> |
26d9cc65 | 34 | #include <linux/of.h> |
445eaf87 | 35 | |
7372add4 EV |
36 | #include "ti-thermal.h" |
37 | #include "ti-bandgap.h" | |
445eaf87 EV |
38 | |
39 | /* common data structures */ | |
03e859d3 | 40 | struct ti_thermal_data { |
4d753aa7 | 41 | struct cpufreq_policy *policy; |
03e859d3 | 42 | struct thermal_zone_device *ti_thermal; |
359836e1 | 43 | struct thermal_zone_device *pcb_tz; |
445eaf87 | 44 | struct thermal_cooling_device *cool_dev; |
03e859d3 | 45 | struct ti_bandgap *bgp; |
445eaf87 EV |
46 | enum thermal_device_mode mode; |
47 | struct work_struct thermal_wq; | |
48 | int sensor_id; | |
26d9cc65 | 49 | bool our_zone; |
445eaf87 EV |
50 | }; |
51 | ||
03e859d3 | 52 | static void ti_thermal_work(struct work_struct *work) |
445eaf87 | 53 | { |
03e859d3 EV |
54 | struct ti_thermal_data *data = container_of(work, |
55 | struct ti_thermal_data, thermal_wq); | |
445eaf87 | 56 | |
0e70f466 | 57 | thermal_zone_device_update(data->ti_thermal, THERMAL_EVENT_UNSPECIFIED); |
445eaf87 | 58 | |
03e859d3 EV |
59 | dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n", |
60 | data->ti_thermal->type); | |
445eaf87 EV |
61 | } |
62 | ||
63 | /** | |
03e859d3 | 64 | * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature |
445eaf87 EV |
65 | * @t: omap sensor temperature |
66 | * @s: omap sensor slope value | |
67 | * @c: omap sensor const value | |
68 | */ | |
03e859d3 | 69 | static inline int ti_thermal_hotspot_temperature(int t, int s, int c) |
445eaf87 EV |
70 | { |
71 | int delta = t * s / 1000 + c; | |
72 | ||
73 | if (delta < 0) | |
74 | delta = 0; | |
75 | ||
76 | return t + delta; | |
77 | } | |
78 | ||
79 | /* thermal zone ops */ | |
e34238bf | 80 | /* Get temperature callback function for thermal zone */ |
17e8351a | 81 | static inline int __ti_thermal_get_temp(void *devdata, int *temp) |
445eaf87 | 82 | { |
359836e1 | 83 | struct thermal_zone_device *pcb_tz = NULL; |
26d9cc65 | 84 | struct ti_thermal_data *data = devdata; |
03e859d3 | 85 | struct ti_bandgap *bgp; |
9879b2c4 | 86 | const struct ti_temp_sensor *s; |
359836e1 | 87 | int ret, tmp, slope, constant; |
17e8351a | 88 | int pcb_temp; |
445eaf87 | 89 | |
04a4d10d EV |
90 | if (!data) |
91 | return 0; | |
92 | ||
d7f080e6 EV |
93 | bgp = data->bgp; |
94 | s = &bgp->conf->sensors[data->sensor_id]; | |
04a4d10d | 95 | |
03e859d3 | 96 | ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp); |
445eaf87 EV |
97 | if (ret) |
98 | return ret; | |
99 | ||
359836e1 | 100 | /* Default constants */ |
e7d22fd2 K |
101 | slope = thermal_zone_get_slope(data->ti_thermal); |
102 | constant = thermal_zone_get_offset(data->ti_thermal); | |
359836e1 EV |
103 | |
104 | pcb_tz = data->pcb_tz; | |
445eaf87 | 105 | /* In case pcb zone is available, use the extrapolation rule with it */ |
0c12b5ac | 106 | if (!IS_ERR(pcb_tz)) { |
359836e1 EV |
107 | ret = thermal_zone_get_temp(pcb_tz, &pcb_temp); |
108 | if (!ret) { | |
109 | tmp -= pcb_temp; /* got a valid PCB temp */ | |
110 | slope = s->slope_pcb; | |
111 | constant = s->constant_pcb; | |
112 | } else { | |
113 | dev_err(bgp->dev, | |
114 | "Failed to read PCB state. Using defaults\n"); | |
df8f1347 | 115 | ret = 0; |
359836e1 | 116 | } |
445eaf87 | 117 | } |
03e859d3 | 118 | *temp = ti_thermal_hotspot_temperature(tmp, slope, constant); |
445eaf87 EV |
119 | |
120 | return ret; | |
121 | } | |
122 | ||
26d9cc65 | 123 | static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal, |
17e8351a | 124 | int *temp) |
26d9cc65 EV |
125 | { |
126 | struct ti_thermal_data *data = thermal->devdata; | |
127 | ||
128 | return __ti_thermal_get_temp(data, temp); | |
129 | } | |
130 | ||
e78eaf45 | 131 | static int __ti_thermal_get_trend(void *p, int trip, enum thermal_trend *trend) |
03b7f67b | 132 | { |
26d9cc65 | 133 | struct ti_thermal_data *data = p; |
03b7f67b K |
134 | struct ti_bandgap *bgp; |
135 | int id, tr, ret = 0; | |
136 | ||
137 | bgp = data->bgp; | |
138 | id = data->sensor_id; | |
139 | ||
140 | ret = ti_bandgap_get_trend(bgp, id, &tr); | |
141 | if (ret) | |
142 | return ret; | |
143 | ||
144 | if (tr > 0) | |
145 | *trend = THERMAL_TREND_RAISING; | |
146 | else if (tr < 0) | |
147 | *trend = THERMAL_TREND_DROPPING; | |
148 | else | |
149 | *trend = THERMAL_TREND_STABLE; | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
2251aef6 EV |
154 | static const struct thermal_zone_of_device_ops ti_of_thermal_ops = { |
155 | .get_temp = __ti_thermal_get_temp, | |
156 | .get_trend = __ti_thermal_get_trend, | |
157 | }; | |
158 | ||
03e859d3 EV |
159 | static struct ti_thermal_data |
160 | *ti_thermal_build_data(struct ti_bandgap *bgp, int id) | |
445eaf87 | 161 | { |
03e859d3 | 162 | struct ti_thermal_data *data; |
445eaf87 | 163 | |
d7f080e6 | 164 | data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL); |
445eaf87 | 165 | if (!data) { |
d7f080e6 | 166 | dev_err(bgp->dev, "kzalloc fail\n"); |
04a4d10d | 167 | return NULL; |
445eaf87 EV |
168 | } |
169 | data->sensor_id = id; | |
d7f080e6 | 170 | data->bgp = bgp; |
445eaf87 | 171 | data->mode = THERMAL_DEVICE_ENABLED; |
0c12b5ac | 172 | /* pcb_tz will be either valid or PTR_ERR() */ |
359836e1 | 173 | data->pcb_tz = thermal_zone_get_zone_by_name("pcb"); |
03e859d3 | 174 | INIT_WORK(&data->thermal_wq, ti_thermal_work); |
445eaf87 | 175 | |
04a4d10d EV |
176 | return data; |
177 | } | |
178 | ||
03e859d3 EV |
179 | int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id, |
180 | char *domain) | |
04a4d10d | 181 | { |
03e859d3 | 182 | struct ti_thermal_data *data; |
04a4d10d | 183 | |
03e859d3 | 184 | data = ti_bandgap_get_sensor_data(bgp, id); |
04a4d10d | 185 | |
0c12b5ac | 186 | if (!data || IS_ERR(data)) |
03e859d3 | 187 | data = ti_thermal_build_data(bgp, id); |
04a4d10d EV |
188 | |
189 | if (!data) | |
190 | return -EINVAL; | |
191 | ||
26d9cc65 | 192 | /* in case this is specified by DT */ |
3982204c | 193 | data->ti_thermal = devm_thermal_zone_of_sensor_register(bgp->dev, id, |
2251aef6 | 194 | data, &ti_of_thermal_ops); |
26d9cc65 | 195 | if (IS_ERR(data->ti_thermal)) { |
b263b473 K |
196 | dev_err(bgp->dev, "thermal zone device is NULL\n"); |
197 | return PTR_ERR(data->ti_thermal); | |
445eaf87 | 198 | } |
b263b473 | 199 | |
03e859d3 | 200 | ti_bandgap_set_sensor_data(bgp, id, data); |
10ccff1b RK |
201 | ti_bandgap_write_update_interval(bgp, data->sensor_id, |
202 | data->ti_thermal->polling_delay); | |
445eaf87 EV |
203 | |
204 | return 0; | |
205 | } | |
206 | ||
03e859d3 | 207 | int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id) |
445eaf87 | 208 | { |
03e859d3 | 209 | struct ti_thermal_data *data; |
445eaf87 | 210 | |
03e859d3 | 211 | data = ti_bandgap_get_sensor_data(bgp, id); |
445eaf87 | 212 | |
26d9cc65 EV |
213 | if (data && data->ti_thermal) { |
214 | if (data->our_zone) | |
215 | thermal_zone_device_unregister(data->ti_thermal); | |
26d9cc65 | 216 | } |
445eaf87 EV |
217 | |
218 | return 0; | |
219 | } | |
220 | ||
03e859d3 | 221 | int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id) |
445eaf87 | 222 | { |
03e859d3 | 223 | struct ti_thermal_data *data; |
445eaf87 | 224 | |
03e859d3 | 225 | data = ti_bandgap_get_sensor_data(bgp, id); |
445eaf87 EV |
226 | |
227 | schedule_work(&data->thermal_wq); | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
03e859d3 | 232 | int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id) |
445eaf87 | 233 | { |
03e859d3 | 234 | struct ti_thermal_data *data; |
26d9cc65 EV |
235 | struct device_node *np = bgp->dev->of_node; |
236 | ||
237 | /* | |
238 | * We are assuming here that if one deploys the zone | |
239 | * using DT, then it must be aware that the cooling device | |
240 | * loading has to happen via cpufreq driver. | |
241 | */ | |
242 | if (of_find_property(np, "#thermal-sensor-cells", NULL)) | |
243 | return 0; | |
445eaf87 | 244 | |
03e859d3 | 245 | data = ti_bandgap_get_sensor_data(bgp, id); |
0c12b5ac | 246 | if (!data || IS_ERR(data)) |
03e859d3 | 247 | data = ti_thermal_build_data(bgp, id); |
04a4d10d EV |
248 | |
249 | if (!data) | |
250 | return -EINVAL; | |
445eaf87 | 251 | |
4d753aa7 VK |
252 | data->policy = cpufreq_cpu_get(0); |
253 | if (!data->policy) { | |
254 | pr_debug("%s: CPUFreq policy not found\n", __func__); | |
255 | return -EPROBE_DEFER; | |
256 | } | |
257 | ||
445eaf87 | 258 | /* Register cooling device */ |
4d753aa7 | 259 | data->cool_dev = cpufreq_cooling_register(data->policy); |
0c12b5ac | 260 | if (IS_ERR(data->cool_dev)) { |
cffafc32 | 261 | int ret = PTR_ERR(data->cool_dev); |
4d753aa7 VK |
262 | dev_err(bgp->dev, "Failed to register cpu cooling device %d\n", |
263 | ret); | |
264 | cpufreq_cpu_put(data->policy); | |
cffafc32 EV |
265 | |
266 | return ret; | |
445eaf87 | 267 | } |
03e859d3 | 268 | ti_bandgap_set_sensor_data(bgp, id, data); |
445eaf87 EV |
269 | |
270 | return 0; | |
271 | } | |
272 | ||
03e859d3 | 273 | int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id) |
445eaf87 | 274 | { |
03e859d3 | 275 | struct ti_thermal_data *data; |
445eaf87 | 276 | |
03e859d3 | 277 | data = ti_bandgap_get_sensor_data(bgp, id); |
26d9cc65 | 278 | |
4d753aa7 | 279 | if (data) { |
26d9cc65 | 280 | cpufreq_cooling_unregister(data->cool_dev); |
ba817a8c TL |
281 | if (data->policy) |
282 | cpufreq_cpu_put(data->policy); | |
4d753aa7 | 283 | } |
445eaf87 EV |
284 | |
285 | return 0; | |
286 | } |