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 <linux/units.h>
12 #include "int340x_thermal_zone.h"
14 static int int340x_thermal_get_zone_temp(struct thermal_zone_device *zone,
17 struct int34x_thermal_zone *d = thermal_zone_device_priv(zone);
18 unsigned long long tmp;
21 status = acpi_evaluate_integer(d->adev->handle, "_TMP", NULL, &tmp);
22 if (ACPI_FAILURE(status))
28 conv_temp = acpi_lpat_raw_to_temp(d->lpat_table, (int)tmp);
32 *temp = conv_temp * 10;
34 /* _TMP returns the temperature in tenths of degrees Kelvin */
35 *temp = deci_kelvin_to_millicelsius(tmp);
41 static int int340x_thermal_set_trip_temp(struct thermal_zone_device *zone,
44 struct int34x_thermal_zone *d = thermal_zone_device_priv(zone);
45 char name[] = {'P', 'A', 'T', '0' + trip, '\0'};
51 status = acpi_execute_simple_method(d->adev->handle, name,
52 millicelsius_to_deci_kelvin(temp));
53 if (ACPI_FAILURE(status))
59 static void int340x_thermal_critical(struct thermal_zone_device *zone)
61 dev_dbg(&zone->device, "%s: critical temperature reached\n", zone->type);
64 static struct thermal_zone_device_ops int340x_thermal_zone_ops = {
65 .get_temp = int340x_thermal_get_zone_temp,
66 .set_trip_temp = int340x_thermal_set_trip_temp,
67 .critical = int340x_thermal_critical,
70 static int int340x_thermal_read_trips(struct acpi_device *zone_adev,
71 struct thermal_trip *zone_trips,
76 ret = thermal_acpi_critical_trip_temp(zone_adev,
77 &zone_trips[trip_cnt].temperature);
79 zone_trips[trip_cnt].type = THERMAL_TRIP_CRITICAL;
83 ret = thermal_acpi_hot_trip_temp(zone_adev,
84 &zone_trips[trip_cnt].temperature);
86 zone_trips[trip_cnt].type = THERMAL_TRIP_HOT;
90 ret = thermal_acpi_passive_trip_temp(zone_adev,
91 &zone_trips[trip_cnt].temperature);
93 zone_trips[trip_cnt].type = THERMAL_TRIP_PASSIVE;
97 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
98 ret = thermal_acpi_active_trip_temp(zone_adev, i,
99 &zone_trips[trip_cnt].temperature);
103 zone_trips[trip_cnt].type = THERMAL_TRIP_ACTIVE;
110 static struct thermal_zone_params int340x_thermal_params = {
111 .governor_name = "user_space",
115 struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
116 int (*get_temp) (struct thermal_zone_device *, int *))
118 struct int34x_thermal_zone *int34x_zone;
119 struct thermal_trip *zone_trips;
120 unsigned long long trip_cnt = 0;
121 unsigned long long hyst;
126 int34x_zone = kzalloc(sizeof(*int34x_zone), GFP_KERNEL);
128 return ERR_PTR(-ENOMEM);
130 int34x_zone->adev = adev;
132 int34x_zone->ops = kmemdup(&int340x_thermal_zone_ops,
133 sizeof(int340x_thermal_zone_ops), GFP_KERNEL);
134 if (!int34x_zone->ops) {
140 int34x_zone->ops->get_temp = get_temp;
142 status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
143 if (ACPI_SUCCESS(status)) {
144 int34x_zone->aux_trip_nr = trip_cnt;
145 trip_mask = BIT(trip_cnt) - 1;
148 zone_trips = kzalloc(sizeof(*zone_trips) * (trip_cnt + INT340X_THERMAL_MAX_TRIP_COUNT),
152 goto err_trips_alloc;
155 for (i = 0; i < trip_cnt; i++) {
156 zone_trips[i].type = THERMAL_TRIP_PASSIVE;
157 zone_trips[i].temperature = THERMAL_TEMP_INVALID;
160 trip_cnt = int340x_thermal_read_trips(adev, zone_trips, trip_cnt);
162 status = acpi_evaluate_integer(adev->handle, "GTSH", NULL, &hyst);
163 if (ACPI_SUCCESS(status))
168 for (i = 0; i < trip_cnt; ++i)
169 zone_trips[i].hysteresis = hyst;
171 int34x_zone->trips = zone_trips;
173 int34x_zone->lpat_table = acpi_lpat_get_conversion_table(adev->handle);
175 int34x_zone->zone = thermal_zone_device_register_with_trips(
176 acpi_device_bid(adev),
177 zone_trips, trip_cnt,
178 trip_mask, int34x_zone,
180 &int340x_thermal_params,
182 if (IS_ERR(int34x_zone->zone)) {
183 ret = PTR_ERR(int34x_zone->zone);
184 goto err_thermal_zone;
186 ret = thermal_zone_device_enable(int34x_zone->zone);
193 thermal_zone_device_unregister(int34x_zone->zone);
195 kfree(int34x_zone->trips);
196 acpi_lpat_free_conversion_table(int34x_zone->lpat_table);
198 kfree(int34x_zone->ops);
203 EXPORT_SYMBOL_GPL(int340x_thermal_zone_add);
205 void int340x_thermal_zone_remove(struct int34x_thermal_zone *int34x_zone)
207 thermal_zone_device_unregister(int34x_zone->zone);
208 acpi_lpat_free_conversion_table(int34x_zone->lpat_table);
209 kfree(int34x_zone->trips);
210 kfree(int34x_zone->ops);
213 EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
215 void int340x_thermal_update_trips(struct int34x_thermal_zone *int34x_zone)
217 struct acpi_device *zone_adev = int34x_zone->adev;
218 struct thermal_trip *zone_trips = int34x_zone->trips;
219 int trip_cnt = int34x_zone->zone->num_trips;
223 mutex_lock(&int34x_zone->zone->lock);
225 for (i = int34x_zone->aux_trip_nr; i < trip_cnt; i++) {
228 switch (zone_trips[i].type) {
229 case THERMAL_TRIP_CRITICAL:
230 err = thermal_acpi_critical_trip_temp(zone_adev, &temp);
232 case THERMAL_TRIP_HOT:
233 err = thermal_acpi_hot_trip_temp(zone_adev, &temp);
235 case THERMAL_TRIP_PASSIVE:
236 err = thermal_acpi_passive_trip_temp(zone_adev, &temp);
238 case THERMAL_TRIP_ACTIVE:
239 err = thermal_acpi_active_trip_temp(zone_adev, act_trip_nr++,
246 zone_trips[i].temperature = THERMAL_TEMP_INVALID;
250 zone_trips[i].temperature = temp;
253 mutex_unlock(&int34x_zone->zone->lock);
255 EXPORT_SYMBOL_GPL(int340x_thermal_update_trips);
259 MODULE_DESCRIPTION("Intel INT340x common thermal zone handler");
260 MODULE_LICENSE("GPL v2");