acpi_lpat.c 4.0 KB

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