18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * acpi_lpat.c - LPAT table processing functions 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Intel Corporation. All rights reserved. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/export.h> 98c2ecf20Sopenharmony_ci#include <linux/acpi.h> 108c2ecf20Sopenharmony_ci#include <acpi/acpi_lpat.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/** 138c2ecf20Sopenharmony_ci * acpi_lpat_raw_to_temp(): Return temperature from raw value through 148c2ecf20Sopenharmony_ci * LPAT conversion table 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * @lpat_table: the temperature_raw mapping table structure 178c2ecf20Sopenharmony_ci * @raw: the raw value, used as a key to get the temperature from the 188c2ecf20Sopenharmony_ci * above mapping table 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * A positive converted temperature value will be returned on success, 218c2ecf20Sopenharmony_ci * a negative errno will be returned in error cases. 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_ciint acpi_lpat_raw_to_temp(struct acpi_lpat_conversion_table *lpat_table, 248c2ecf20Sopenharmony_ci int raw) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci int i, delta_temp, delta_raw, temp; 278c2ecf20Sopenharmony_ci struct acpi_lpat *lpat = lpat_table->lpat; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci for (i = 0; i < lpat_table->lpat_count - 1; i++) { 308c2ecf20Sopenharmony_ci if ((raw >= lpat[i].raw && raw <= lpat[i+1].raw) || 318c2ecf20Sopenharmony_ci (raw <= lpat[i].raw && raw >= lpat[i+1].raw)) 328c2ecf20Sopenharmony_ci break; 338c2ecf20Sopenharmony_ci } 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci if (i == lpat_table->lpat_count - 1) 368c2ecf20Sopenharmony_ci return -ENOENT; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci delta_temp = lpat[i+1].temp - lpat[i].temp; 398c2ecf20Sopenharmony_ci delta_raw = lpat[i+1].raw - lpat[i].raw; 408c2ecf20Sopenharmony_ci temp = lpat[i].temp + (raw - lpat[i].raw) * delta_temp / delta_raw; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci return temp; 438c2ecf20Sopenharmony_ci} 448c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(acpi_lpat_raw_to_temp); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/** 478c2ecf20Sopenharmony_ci * acpi_lpat_temp_to_raw(): Return raw value from temperature through 488c2ecf20Sopenharmony_ci * LPAT conversion table 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * @lpat_table: the temperature_raw mapping table 518c2ecf20Sopenharmony_ci * @temp: the temperature, used as a key to get the raw value from the 528c2ecf20Sopenharmony_ci * above mapping table 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * The raw value will be returned on success, 558c2ecf20Sopenharmony_ci * a negative errno will be returned in error cases. 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_ciint acpi_lpat_temp_to_raw(struct acpi_lpat_conversion_table *lpat_table, 588c2ecf20Sopenharmony_ci int temp) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci int i, delta_temp, delta_raw, raw; 618c2ecf20Sopenharmony_ci struct acpi_lpat *lpat = lpat_table->lpat; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci for (i = 0; i < lpat_table->lpat_count - 1; i++) { 648c2ecf20Sopenharmony_ci if (temp >= lpat[i].temp && temp <= lpat[i+1].temp) 658c2ecf20Sopenharmony_ci break; 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (i == lpat_table->lpat_count - 1) 698c2ecf20Sopenharmony_ci return -ENOENT; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci delta_temp = lpat[i+1].temp - lpat[i].temp; 728c2ecf20Sopenharmony_ci delta_raw = lpat[i+1].raw - lpat[i].raw; 738c2ecf20Sopenharmony_ci raw = lpat[i].raw + (temp - lpat[i].temp) * delta_raw / delta_temp; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci return raw; 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(acpi_lpat_temp_to_raw); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/** 808c2ecf20Sopenharmony_ci * acpi_lpat_get_conversion_table(): Parse ACPI LPAT table if present. 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci * @handle: Handle to acpi device 838c2ecf20Sopenharmony_ci * 848c2ecf20Sopenharmony_ci * Parse LPAT table to a struct of type acpi_lpat_table. On success 858c2ecf20Sopenharmony_ci * it returns a pointer to newly allocated table. This table must 868c2ecf20Sopenharmony_ci * be freed by the caller when finished processing, using a call to 878c2ecf20Sopenharmony_ci * acpi_lpat_free_conversion_table. 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_cistruct acpi_lpat_conversion_table *acpi_lpat_get_conversion_table(acpi_handle 908c2ecf20Sopenharmony_ci handle) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci struct acpi_lpat_conversion_table *lpat_table = NULL; 938c2ecf20Sopenharmony_ci struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 948c2ecf20Sopenharmony_ci union acpi_object *obj_p, *obj_e; 958c2ecf20Sopenharmony_ci int *lpat, i; 968c2ecf20Sopenharmony_ci acpi_status status; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci status = acpi_evaluate_object(handle, "LPAT", NULL, &buffer); 998c2ecf20Sopenharmony_ci if (ACPI_FAILURE(status)) 1008c2ecf20Sopenharmony_ci return NULL; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci obj_p = (union acpi_object *)buffer.pointer; 1038c2ecf20Sopenharmony_ci if (!obj_p || (obj_p->type != ACPI_TYPE_PACKAGE) || 1048c2ecf20Sopenharmony_ci (obj_p->package.count % 2) || (obj_p->package.count < 4)) 1058c2ecf20Sopenharmony_ci goto out; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci lpat = kcalloc(obj_p->package.count, sizeof(int), GFP_KERNEL); 1088c2ecf20Sopenharmony_ci if (!lpat) 1098c2ecf20Sopenharmony_ci goto out; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci for (i = 0; i < obj_p->package.count; i++) { 1128c2ecf20Sopenharmony_ci obj_e = &obj_p->package.elements[i]; 1138c2ecf20Sopenharmony_ci if (obj_e->type != ACPI_TYPE_INTEGER) { 1148c2ecf20Sopenharmony_ci kfree(lpat); 1158c2ecf20Sopenharmony_ci goto out; 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci lpat[i] = (s64)obj_e->integer.value; 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci lpat_table = kzalloc(sizeof(*lpat_table), GFP_KERNEL); 1218c2ecf20Sopenharmony_ci if (!lpat_table) { 1228c2ecf20Sopenharmony_ci kfree(lpat); 1238c2ecf20Sopenharmony_ci goto out; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci lpat_table->lpat = (struct acpi_lpat *)lpat; 1278c2ecf20Sopenharmony_ci lpat_table->lpat_count = obj_p->package.count / 2; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ciout: 1308c2ecf20Sopenharmony_ci kfree(buffer.pointer); 1318c2ecf20Sopenharmony_ci return lpat_table; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(acpi_lpat_get_conversion_table); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci/** 1368c2ecf20Sopenharmony_ci * acpi_lpat_free_conversion_table(): Free LPAT table. 1378c2ecf20Sopenharmony_ci * 1388c2ecf20Sopenharmony_ci * @lpat_table: the temperature_raw mapping table structure 1398c2ecf20Sopenharmony_ci * 1408c2ecf20Sopenharmony_ci * Frees the LPAT table previously allocated by a call to 1418c2ecf20Sopenharmony_ci * acpi_lpat_get_conversion_table. 1428c2ecf20Sopenharmony_ci */ 1438c2ecf20Sopenharmony_civoid acpi_lpat_free_conversion_table(struct acpi_lpat_conversion_table 1448c2ecf20Sopenharmony_ci *lpat_table) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci if (lpat_table) { 1478c2ecf20Sopenharmony_ci kfree(lpat_table->lpat); 1488c2ecf20Sopenharmony_ci kfree(lpat_table); 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(acpi_lpat_free_conversion_table); 152