1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * int340x_thermal_zone.c
4 * Copyright (c) 2015, Intel Corporation.
5 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/acpi.h>
10 #include <linux/thermal.h>
11 #include <linux/units.h>
12 #include "int340x_thermal_zone.h"
13
int340x_thermal_get_zone_temp(struct thermal_zone_device *zone, int *temp)14 static int int340x_thermal_get_zone_temp(struct thermal_zone_device *zone,
15 int *temp)
16 {
17 struct int34x_thermal_zone *d = zone->devdata;
18 unsigned long long tmp;
19 acpi_status status;
20
21 if (d->override_ops && d->override_ops->get_temp)
22 return d->override_ops->get_temp(zone, temp);
23
24 status = acpi_evaluate_integer(d->adev->handle, "_TMP", NULL, &tmp);
25 if (ACPI_FAILURE(status))
26 return -EIO;
27
28 if (d->lpat_table) {
29 int conv_temp;
30
31 conv_temp = acpi_lpat_raw_to_temp(d->lpat_table, (int)tmp);
32 if (conv_temp < 0)
33 return conv_temp;
34
35 *temp = (unsigned long)conv_temp * 10;
36 } else
37 /* _TMP returns the temperature in tenths of degrees Kelvin */
38 *temp = deci_kelvin_to_millicelsius(tmp);
39
40 return 0;
41 }
42
int340x_thermal_get_trip_temp(struct thermal_zone_device *zone, int trip, int *temp)43 static int int340x_thermal_get_trip_temp(struct thermal_zone_device *zone,
44 int trip, int *temp)
45 {
46 struct int34x_thermal_zone *d = zone->devdata;
47 int i, ret = 0;
48
49 if (d->override_ops && d->override_ops->get_trip_temp)
50 return d->override_ops->get_trip_temp(zone, trip, temp);
51
52 mutex_lock(&d->trip_mutex);
53
54 if (trip < d->aux_trip_nr)
55 *temp = d->aux_trips[trip];
56 else if (trip == d->crt_trip_id)
57 *temp = d->crt_temp;
58 else if (trip == d->psv_trip_id)
59 *temp = d->psv_temp;
60 else if (trip == d->hot_trip_id)
61 *temp = d->hot_temp;
62 else {
63 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
64 if (d->act_trips[i].valid &&
65 d->act_trips[i].id == trip) {
66 *temp = d->act_trips[i].temp;
67 break;
68 }
69 }
70 if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT)
71 ret = -EINVAL;
72 }
73
74 mutex_unlock(&d->trip_mutex);
75
76 return ret;
77 }
78
int340x_thermal_get_trip_type(struct thermal_zone_device *zone, int trip, enum thermal_trip_type *type)79 static int int340x_thermal_get_trip_type(struct thermal_zone_device *zone,
80 int trip,
81 enum thermal_trip_type *type)
82 {
83 struct int34x_thermal_zone *d = zone->devdata;
84 int i, ret = 0;
85
86 if (d->override_ops && d->override_ops->get_trip_type)
87 return d->override_ops->get_trip_type(zone, trip, type);
88
89 mutex_lock(&d->trip_mutex);
90
91 if (trip < d->aux_trip_nr)
92 *type = THERMAL_TRIP_PASSIVE;
93 else if (trip == d->crt_trip_id)
94 *type = THERMAL_TRIP_CRITICAL;
95 else if (trip == d->hot_trip_id)
96 *type = THERMAL_TRIP_HOT;
97 else if (trip == d->psv_trip_id)
98 *type = THERMAL_TRIP_PASSIVE;
99 else {
100 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
101 if (d->act_trips[i].valid &&
102 d->act_trips[i].id == trip) {
103 *type = THERMAL_TRIP_ACTIVE;
104 break;
105 }
106 }
107 if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT)
108 ret = -EINVAL;
109 }
110
111 mutex_unlock(&d->trip_mutex);
112
113 return ret;
114 }
115
int340x_thermal_set_trip_temp(struct thermal_zone_device *zone, int trip, int temp)116 static int int340x_thermal_set_trip_temp(struct thermal_zone_device *zone,
117 int trip, int temp)
118 {
119 struct int34x_thermal_zone *d = zone->devdata;
120 acpi_status status;
121 char name[10];
122
123 if (d->override_ops && d->override_ops->set_trip_temp)
124 return d->override_ops->set_trip_temp(zone, trip, temp);
125
126 snprintf(name, sizeof(name), "PAT%d", trip);
127 status = acpi_execute_simple_method(d->adev->handle, name,
128 millicelsius_to_deci_kelvin(temp));
129 if (ACPI_FAILURE(status))
130 return -EIO;
131
132 d->aux_trips[trip] = temp;
133
134 return 0;
135 }
136
137
int340x_thermal_get_trip_hyst(struct thermal_zone_device *zone, int trip, int *temp)138 static int int340x_thermal_get_trip_hyst(struct thermal_zone_device *zone,
139 int trip, int *temp)
140 {
141 struct int34x_thermal_zone *d = zone->devdata;
142 acpi_status status;
143 unsigned long long hyst;
144
145 if (d->override_ops && d->override_ops->get_trip_hyst)
146 return d->override_ops->get_trip_hyst(zone, trip, temp);
147
148 status = acpi_evaluate_integer(d->adev->handle, "GTSH", NULL, &hyst);
149 if (ACPI_FAILURE(status))
150 *temp = 0;
151 else
152 *temp = hyst * 100;
153
154 return 0;
155 }
156
157 static struct thermal_zone_device_ops int340x_thermal_zone_ops = {
158 .get_temp = int340x_thermal_get_zone_temp,
159 .get_trip_temp = int340x_thermal_get_trip_temp,
160 .get_trip_type = int340x_thermal_get_trip_type,
161 .set_trip_temp = int340x_thermal_set_trip_temp,
162 .get_trip_hyst = int340x_thermal_get_trip_hyst,
163 };
164
int340x_thermal_get_trip_config(acpi_handle handle, char *name, int *temp)165 static int int340x_thermal_get_trip_config(acpi_handle handle, char *name,
166 int *temp)
167 {
168 unsigned long long r;
169 acpi_status status;
170
171 status = acpi_evaluate_integer(handle, name, NULL, &r);
172 if (ACPI_FAILURE(status))
173 return -EIO;
174
175 *temp = deci_kelvin_to_millicelsius(r);
176
177 return 0;
178 }
179
int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone)180 int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone)
181 {
182 int trip_cnt = int34x_zone->aux_trip_nr;
183 int i;
184
185 mutex_lock(&int34x_zone->trip_mutex);
186
187 int34x_zone->crt_trip_id = -1;
188 if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_CRT",
189 &int34x_zone->crt_temp))
190 int34x_zone->crt_trip_id = trip_cnt++;
191
192 int34x_zone->hot_trip_id = -1;
193 if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_HOT",
194 &int34x_zone->hot_temp))
195 int34x_zone->hot_trip_id = trip_cnt++;
196
197 int34x_zone->psv_trip_id = -1;
198 if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_PSV",
199 &int34x_zone->psv_temp))
200 int34x_zone->psv_trip_id = trip_cnt++;
201
202 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
203 char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
204
205 if (int340x_thermal_get_trip_config(int34x_zone->adev->handle,
206 name,
207 &int34x_zone->act_trips[i].temp))
208 break;
209
210 int34x_zone->act_trips[i].id = trip_cnt++;
211 int34x_zone->act_trips[i].valid = true;
212 }
213
214 mutex_unlock(&int34x_zone->trip_mutex);
215
216 return trip_cnt;
217 }
218 EXPORT_SYMBOL_GPL(int340x_thermal_read_trips);
219
220 static struct thermal_zone_params int340x_thermal_params = {
221 .governor_name = "user_space",
222 .no_hwmon = true,
223 };
224
int340x_thermal_zone_add(struct acpi_device *adev, struct thermal_zone_device_ops *override_ops)225 struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
226 struct thermal_zone_device_ops *override_ops)
227 {
228 struct int34x_thermal_zone *int34x_thermal_zone;
229 acpi_status status;
230 unsigned long long trip_cnt;
231 int trip_mask = 0;
232 int ret;
233
234 int34x_thermal_zone = kzalloc(sizeof(*int34x_thermal_zone),
235 GFP_KERNEL);
236 if (!int34x_thermal_zone)
237 return ERR_PTR(-ENOMEM);
238
239 mutex_init(&int34x_thermal_zone->trip_mutex);
240
241 int34x_thermal_zone->adev = adev;
242 int34x_thermal_zone->override_ops = override_ops;
243
244 status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
245 if (ACPI_FAILURE(status))
246 trip_cnt = 0;
247 else {
248 int i;
249
250 int34x_thermal_zone->aux_trips =
251 kcalloc(trip_cnt,
252 sizeof(*int34x_thermal_zone->aux_trips),
253 GFP_KERNEL);
254 if (!int34x_thermal_zone->aux_trips) {
255 ret = -ENOMEM;
256 goto err_trip_alloc;
257 }
258 trip_mask = BIT(trip_cnt) - 1;
259 int34x_thermal_zone->aux_trip_nr = trip_cnt;
260 for (i = 0; i < trip_cnt; ++i)
261 int34x_thermal_zone->aux_trips[i] = THERMAL_TEMP_INVALID;
262 }
263
264 trip_cnt = int340x_thermal_read_trips(int34x_thermal_zone);
265
266 int34x_thermal_zone->lpat_table = acpi_lpat_get_conversion_table(
267 adev->handle);
268
269 int34x_thermal_zone->zone = thermal_zone_device_register(
270 acpi_device_bid(adev),
271 trip_cnt,
272 trip_mask, int34x_thermal_zone,
273 &int340x_thermal_zone_ops,
274 &int340x_thermal_params,
275 0, 0);
276 if (IS_ERR(int34x_thermal_zone->zone)) {
277 ret = PTR_ERR(int34x_thermal_zone->zone);
278 goto err_thermal_zone;
279 }
280 ret = thermal_zone_device_enable(int34x_thermal_zone->zone);
281 if (ret)
282 goto err_enable;
283
284 return int34x_thermal_zone;
285
286 err_enable:
287 thermal_zone_device_unregister(int34x_thermal_zone->zone);
288 err_thermal_zone:
289 acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
290 kfree(int34x_thermal_zone->aux_trips);
291 err_trip_alloc:
292 mutex_destroy(&int34x_thermal_zone->trip_mutex);
293 kfree(int34x_thermal_zone);
294 return ERR_PTR(ret);
295 }
296 EXPORT_SYMBOL_GPL(int340x_thermal_zone_add);
297
int340x_thermal_zone_remove(struct int34x_thermal_zone *int34x_thermal_zone)298 void int340x_thermal_zone_remove(struct int34x_thermal_zone
299 *int34x_thermal_zone)
300 {
301 thermal_zone_device_unregister(int34x_thermal_zone->zone);
302 acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
303 kfree(int34x_thermal_zone->aux_trips);
304 mutex_destroy(&int34x_thermal_zone->trip_mutex);
305 kfree(int34x_thermal_zone);
306 }
307 EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
308
309 MODULE_AUTHOR("Aaron Lu <aaron.lu@intel.com>");
310 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
311 MODULE_DESCRIPTION("Intel INT340x common thermal zone handler");
312 MODULE_LICENSE("GPL v2");
313