^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * acpi_lpat.c - LPAT table processing functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2015 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <acpi/acpi_lpat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * acpi_lpat_raw_to_temp(): Return temperature from raw value through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * LPAT conversion table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @lpat_table: the temperature_raw mapping table structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @raw: the raw value, used as a key to get the temperature from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * above mapping table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * A positive converted temperature value will be returned on success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * a negative errno will be returned in error cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int acpi_lpat_raw_to_temp(struct acpi_lpat_conversion_table *lpat_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int i, delta_temp, delta_raw, temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct acpi_lpat *lpat = lpat_table->lpat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for (i = 0; i < lpat_table->lpat_count - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if ((raw >= lpat[i].raw && raw <= lpat[i+1].raw) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) (raw <= lpat[i].raw && raw >= lpat[i+1].raw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (i == lpat_table->lpat_count - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) delta_temp = lpat[i+1].temp - lpat[i].temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) delta_raw = lpat[i+1].raw - lpat[i].raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) temp = lpat[i].temp + (raw - lpat[i].raw) * delta_temp / delta_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) EXPORT_SYMBOL_GPL(acpi_lpat_raw_to_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * acpi_lpat_temp_to_raw(): Return raw value from temperature through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * LPAT conversion table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @lpat_table: the temperature_raw mapping table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @temp: the temperature, used as a key to get the raw value from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * above mapping table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * The raw value will be returned on success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * a negative errno will be returned in error cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int acpi_lpat_temp_to_raw(struct acpi_lpat_conversion_table *lpat_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int i, delta_temp, delta_raw, raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct acpi_lpat *lpat = lpat_table->lpat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) for (i = 0; i < lpat_table->lpat_count - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (temp >= lpat[i].temp && temp <= lpat[i+1].temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (i == lpat_table->lpat_count - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) delta_temp = lpat[i+1].temp - lpat[i].temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) delta_raw = lpat[i+1].raw - lpat[i].raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) raw = lpat[i].raw + (temp - lpat[i].temp) * delta_raw / delta_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) EXPORT_SYMBOL_GPL(acpi_lpat_temp_to_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * acpi_lpat_get_conversion_table(): Parse ACPI LPAT table if present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * @handle: Handle to acpi device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Parse LPAT table to a struct of type acpi_lpat_table. On success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * it returns a pointer to newly allocated table. This table must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * be freed by the caller when finished processing, using a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * acpi_lpat_free_conversion_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct acpi_lpat_conversion_table *acpi_lpat_get_conversion_table(acpi_handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct acpi_lpat_conversion_table *lpat_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) union acpi_object *obj_p, *obj_e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int *lpat, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) status = acpi_evaluate_object(handle, "LPAT", NULL, &buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) obj_p = (union acpi_object *)buffer.pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (!obj_p || (obj_p->type != ACPI_TYPE_PACKAGE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) (obj_p->package.count % 2) || (obj_p->package.count < 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) lpat = kcalloc(obj_p->package.count, sizeof(int), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!lpat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) for (i = 0; i < obj_p->package.count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) obj_e = &obj_p->package.elements[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (obj_e->type != ACPI_TYPE_INTEGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) kfree(lpat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) lpat[i] = (s64)obj_e->integer.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) lpat_table = kzalloc(sizeof(*lpat_table), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!lpat_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) kfree(lpat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) lpat_table->lpat = (struct acpi_lpat *)lpat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) lpat_table->lpat_count = obj_p->package.count / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) kfree(buffer.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return lpat_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) EXPORT_SYMBOL_GPL(acpi_lpat_get_conversion_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * acpi_lpat_free_conversion_table(): Free LPAT table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * @lpat_table: the temperature_raw mapping table structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Frees the LPAT table previously allocated by a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * acpi_lpat_get_conversion_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void acpi_lpat_free_conversion_table(struct acpi_lpat_conversion_table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *lpat_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (lpat_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) kfree(lpat_table->lpat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) kfree(lpat_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) EXPORT_SYMBOL_GPL(acpi_lpat_free_conversion_table);