summaryrefslogtreecommitdiff
path: root/drivers/acpi/thermal_lib.c
blob: 4e0519ca9739eff3d1d301d192d52a83406c5327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright 2023 Linaro Limited
 * Copyright 2023 Intel Corporation
 *
 * Library routines for retrieving trip point temperature values from the
 * platform firmware via ACPI.
 */
#include <linux/acpi.h>
#include <linux/units.h>
#include <linux/thermal.h>
#include "internal.h"

/*
 * Minimum temperature for full military grade is 218°K (-55°C) and
 * max temperature is 448°K (175°C). We can consider those values as
 * the boundaries for the [trips] temperature returned by the
 * firmware. Any values out of these boundaries may be considered
 * bogus and we can assume the firmware has no data to provide.
 */
#define TEMP_MIN_DECIK	2180ULL
#define TEMP_MAX_DECIK	4480ULL

static int acpi_trip_temp(struct acpi_device *adev, char *obj_name,
			  int *ret_temp)
{
	unsigned long long temp;
	acpi_status status;

	status = acpi_evaluate_integer(adev->handle, obj_name, NULL, &temp);
	if (ACPI_FAILURE(status)) {
		acpi_handle_debug(adev->handle, "%s evaluation failed\n", obj_name);
		return -ENODATA;
	}

	if (temp >= TEMP_MIN_DECIK && temp <= TEMP_MAX_DECIK) {
		*ret_temp = temp;
	} else {
		acpi_handle_debug(adev->handle, "%s result %llu out of range\n",
				  obj_name, temp);
		*ret_temp = THERMAL_TEMP_INVALID;
	}

	return 0;
}

int acpi_active_trip_temp(struct acpi_device *adev, int id, int *ret_temp)
{
	char obj_name[] = {'_', 'A', 'C', '0' + id, '\0'};

	if (id < 0 || id > 9)
		return -EINVAL;

	return acpi_trip_temp(adev, obj_name, ret_temp);
}
EXPORT_SYMBOL_NS_GPL(acpi_active_trip_temp, ACPI_THERMAL);

int acpi_passive_trip_temp(struct acpi_device *adev, int *ret_temp)
{
	return acpi_trip_temp(adev, "_PSV", ret_temp);
}
EXPORT_SYMBOL_NS_GPL(acpi_passive_trip_temp, ACPI_THERMAL);

int acpi_hot_trip_temp(struct acpi_device *adev, int *ret_temp)
{
	return acpi_trip_temp(adev, "_HOT", ret_temp);
}
EXPORT_SYMBOL_NS_GPL(acpi_hot_trip_temp, ACPI_THERMAL);

int acpi_critical_trip_temp(struct acpi_device *adev, int *ret_temp)
{
	return acpi_trip_temp(adev, "_CRT", ret_temp);
}
EXPORT_SYMBOL_NS_GPL(acpi_critical_trip_temp, ACPI_THERMAL);

static int thermal_temp(int error, int temp_decik, int *ret_temp)
{
	if (error)
		return error;

	if (temp_decik == THERMAL_TEMP_INVALID)
		*ret_temp = THERMAL_TEMP_INVALID;
	else
		*ret_temp = deci_kelvin_to_millicelsius(temp_decik);

	return 0;
}

/**
 * thermal_acpi_active_trip_temp - Retrieve active trip point temperature
 * @adev: Target thermal zone ACPI device object.
 * @id: Active cooling level (0 - 9).
 * @ret_temp: Address to store the retrieved temperature value on success.
 *
 * Evaluate the _ACx object for the thermal zone represented by @adev to obtain
 * the temperature of the active cooling trip point corresponding to the active
 * cooling level given by @id.
 *
 * Return 0 on success or a negative error value on failure.
 */
int thermal_acpi_active_trip_temp(struct acpi_device *adev, int id, int *ret_temp)
{
	int temp_decik;
	int ret = acpi_active_trip_temp(adev, id, &temp_decik);

	return thermal_temp(ret, temp_decik, ret_temp);
}
EXPORT_SYMBOL_GPL(thermal_acpi_active_trip_temp);

/**
 * thermal_acpi_passive_trip_temp - Retrieve passive trip point temperature
 * @adev: Target thermal zone ACPI device object.
 * @ret_temp: Address to store the retrieved temperature value on success.
 *
 * Evaluate the _PSV object for the thermal zone represented by @adev to obtain
 * the temperature of the passive cooling trip point.
 *
 * Return 0 on success or -ENODATA on failure.
 */
int thermal_acpi_passive_trip_temp(struct acpi_device *adev, int *ret_temp)
{
	int temp_decik;
	int ret = acpi_passive_trip_temp(adev, &temp_decik);

	return thermal_temp(ret, temp_decik, ret_temp);
}
EXPORT_SYMBOL_GPL(thermal_acpi_passive_trip_temp);

/**
 * thermal_acpi_hot_trip_temp - Retrieve hot trip point temperature
 * @adev: Target thermal zone ACPI device object.
 * @ret_temp: Address to store the retrieved temperature value on success.
 *
 * Evaluate the _HOT object for the thermal zone represented by @adev to obtain
 * the temperature of the trip point at which the system is expected to be put
 * into the S4 sleep state.
 *
 * Return 0 on success or -ENODATA on failure.
 */
int thermal_acpi_hot_trip_temp(struct acpi_device *adev, int *ret_temp)
{
	int temp_decik;
	int ret = acpi_hot_trip_temp(adev, &temp_decik);

	return thermal_temp(ret, temp_decik, ret_temp);
}
EXPORT_SYMBOL_GPL(thermal_acpi_hot_trip_temp);

/**
 * thermal_acpi_critical_trip_temp - Retrieve critical trip point temperature
 * @adev: Target thermal zone ACPI device object.
 * @ret_temp: Address to store the retrieved temperature value on success.
 *
 * Evaluate the _CRT object for the thermal zone represented by @adev to obtain
 * the temperature of the critical cooling trip point.
 *
 * Return 0 on success or -ENODATA on failure.
 */
int thermal_acpi_critical_trip_temp(struct acpi_device *adev, int *ret_temp)
{
	int temp_decik;
	int ret = acpi_critical_trip_temp(adev, &temp_decik);

	return thermal_temp(ret, temp_decik, ret_temp);
}
EXPORT_SYMBOL_GPL(thermal_acpi_critical_trip_temp);