1 // SPDX-License-Identifier: GPL-2.0-only
3 * int340x_thermal_zone.c
4 * Copyright (c) 2015, Intel Corporation.
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 "int340x_thermal_zone.h"
13 static int int340x_thermal_get_zone_temp(struct thermal_zone_device *zone,
16 struct int34x_thermal_zone *d = zone->devdata;
17 unsigned long long tmp;
20 if (d->override_ops && d->override_ops->get_temp)
21 return d->override_ops->get_temp(zone, temp);
23 status = acpi_evaluate_integer(d->adev->handle, "_TMP", NULL, &tmp);
24 if (ACPI_FAILURE(status))
30 conv_temp = acpi_lpat_raw_to_temp(d->lpat_table, (int)tmp);
34 *temp = (unsigned long)conv_temp * 10;
36 /* _TMP returns the temperature in tenths of degrees Kelvin */
37 *temp = DECI_KELVIN_TO_MILLICELSIUS(tmp);
42 static int int340x_thermal_get_trip_temp(struct thermal_zone_device *zone,
45 struct int34x_thermal_zone *d = zone->devdata;
48 if (d->override_ops && d->override_ops->get_trip_temp)
49 return d->override_ops->get_trip_temp(zone, trip, temp);
51 if (trip < d->aux_trip_nr)
52 *temp = d->aux_trips[trip];
53 else if (trip == d->crt_trip_id)
55 else if (trip == d->psv_trip_id)
57 else if (trip == d->hot_trip_id)
60 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
61 if (d->act_trips[i].valid &&
62 d->act_trips[i].id == trip) {
63 *temp = d->act_trips[i].temp;
67 if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT)
74 static int int340x_thermal_get_trip_type(struct thermal_zone_device *zone,
76 enum thermal_trip_type *type)
78 struct int34x_thermal_zone *d = zone->devdata;
81 if (d->override_ops && d->override_ops->get_trip_type)
82 return d->override_ops->get_trip_type(zone, trip, type);
84 if (trip < d->aux_trip_nr)
85 *type = THERMAL_TRIP_PASSIVE;
86 else if (trip == d->crt_trip_id)
87 *type = THERMAL_TRIP_CRITICAL;
88 else if (trip == d->hot_trip_id)
89 *type = THERMAL_TRIP_HOT;
90 else if (trip == d->psv_trip_id)
91 *type = THERMAL_TRIP_PASSIVE;
93 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
94 if (d->act_trips[i].valid &&
95 d->act_trips[i].id == trip) {
96 *type = THERMAL_TRIP_ACTIVE;
100 if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT)
107 static int int340x_thermal_set_trip_temp(struct thermal_zone_device *zone,
110 struct int34x_thermal_zone *d = zone->devdata;
114 if (d->override_ops && d->override_ops->set_trip_temp)
115 return d->override_ops->set_trip_temp(zone, trip, temp);
117 snprintf(name, sizeof(name), "PAT%d", trip);
118 status = acpi_execute_simple_method(d->adev->handle, name,
119 MILLICELSIUS_TO_DECI_KELVIN(temp));
120 if (ACPI_FAILURE(status))
123 d->aux_trips[trip] = temp;
129 static int int340x_thermal_get_trip_hyst(struct thermal_zone_device *zone,
132 struct int34x_thermal_zone *d = zone->devdata;
134 unsigned long long hyst;
136 if (d->override_ops && d->override_ops->get_trip_hyst)
137 return d->override_ops->get_trip_hyst(zone, trip, temp);
139 status = acpi_evaluate_integer(d->adev->handle, "GTSH", NULL, &hyst);
140 if (ACPI_FAILURE(status))
148 static struct thermal_zone_device_ops int340x_thermal_zone_ops = {
149 .get_temp = int340x_thermal_get_zone_temp,
150 .get_trip_temp = int340x_thermal_get_trip_temp,
151 .get_trip_type = int340x_thermal_get_trip_type,
152 .set_trip_temp = int340x_thermal_set_trip_temp,
153 .get_trip_hyst = int340x_thermal_get_trip_hyst,
156 static int int340x_thermal_get_trip_config(acpi_handle handle, char *name,
159 unsigned long long r;
162 status = acpi_evaluate_integer(handle, name, NULL, &r);
163 if (ACPI_FAILURE(status))
166 *temp = DECI_KELVIN_TO_MILLICELSIUS(r);
171 int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone)
173 int trip_cnt = int34x_zone->aux_trip_nr;
176 int34x_zone->crt_trip_id = -1;
177 if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_CRT",
178 &int34x_zone->crt_temp))
179 int34x_zone->crt_trip_id = trip_cnt++;
181 int34x_zone->hot_trip_id = -1;
182 if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_HOT",
183 &int34x_zone->hot_temp))
184 int34x_zone->hot_trip_id = trip_cnt++;
186 int34x_zone->psv_trip_id = -1;
187 if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_PSV",
188 &int34x_zone->psv_temp))
189 int34x_zone->psv_trip_id = trip_cnt++;
191 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
192 char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
194 if (int340x_thermal_get_trip_config(int34x_zone->adev->handle,
196 &int34x_zone->act_trips[i].temp))
199 int34x_zone->act_trips[i].id = trip_cnt++;
200 int34x_zone->act_trips[i].valid = true;
205 EXPORT_SYMBOL_GPL(int340x_thermal_read_trips);
207 static struct thermal_zone_params int340x_thermal_params = {
208 .governor_name = "user_space",
212 struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
213 struct thermal_zone_device_ops *override_ops)
215 struct int34x_thermal_zone *int34x_thermal_zone;
217 unsigned long long trip_cnt;
221 int34x_thermal_zone = kzalloc(sizeof(*int34x_thermal_zone),
223 if (!int34x_thermal_zone)
224 return ERR_PTR(-ENOMEM);
226 int34x_thermal_zone->adev = adev;
227 int34x_thermal_zone->override_ops = override_ops;
229 status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
230 if (ACPI_FAILURE(status))
233 int34x_thermal_zone->aux_trips =
235 sizeof(*int34x_thermal_zone->aux_trips),
237 if (!int34x_thermal_zone->aux_trips) {
241 trip_mask = BIT(trip_cnt) - 1;
242 int34x_thermal_zone->aux_trip_nr = trip_cnt;
245 trip_cnt = int340x_thermal_read_trips(int34x_thermal_zone);
247 int34x_thermal_zone->lpat_table = acpi_lpat_get_conversion_table(
250 int34x_thermal_zone->zone = thermal_zone_device_register(
251 acpi_device_bid(adev),
253 trip_mask, int34x_thermal_zone,
254 &int340x_thermal_zone_ops,
255 &int340x_thermal_params,
257 if (IS_ERR(int34x_thermal_zone->zone)) {
258 ret = PTR_ERR(int34x_thermal_zone->zone);
259 goto err_thermal_zone;
262 return int34x_thermal_zone;
265 acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
266 kfree(int34x_thermal_zone->aux_trips);
268 kfree(int34x_thermal_zone);
271 EXPORT_SYMBOL_GPL(int340x_thermal_zone_add);
273 void int340x_thermal_zone_remove(struct int34x_thermal_zone
274 *int34x_thermal_zone)
276 thermal_zone_device_unregister(int34x_thermal_zone->zone);
277 acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
278 kfree(int34x_thermal_zone->aux_trips);
279 kfree(int34x_thermal_zone);
281 EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
283 MODULE_AUTHOR("Aaron Lu <aaron.lu@intel.com>");
284 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
285 MODULE_DESCRIPTION("Intel INT340x common thermal zone handler");
286 MODULE_LICENSE("GPL v2");