Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ACPI / PMIC: Use common LPAT table handling functions

The LPAT table processing functions from this modules are moved to a
standalone module with exported interface functions.
Using new interface functions in this module.

Signed-off-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Signed-off-by: Zhang Rui <rui.zhang@intel.com>

authored by

Srinivas Pandruvada and committed by
Zhang Rui
ac586e2d c55d6282

+18 -115
+18 -115
drivers/acpi/pmic/intel_pmic.c
··· 16 16 #include <linux/module.h> 17 17 #include <linux/acpi.h> 18 18 #include <linux/regmap.h> 19 + #include <acpi/acpi_lpat.h> 19 20 #include "intel_pmic.h" 20 21 21 22 #define PMIC_POWER_OPREGION_ID 0x8d 22 23 #define PMIC_THERMAL_OPREGION_ID 0x8c 23 24 24 - struct acpi_lpat { 25 - int temp; 26 - int raw; 27 - }; 28 - 29 25 struct intel_pmic_opregion { 30 26 struct mutex lock; 31 - struct acpi_lpat *lpat; 32 - int lpat_count; 27 + struct acpi_lpat_conversion_table *lpat_table; 33 28 struct regmap *regmap; 34 29 struct intel_pmic_opregion_data *data; 35 30 }; ··· 43 48 } 44 49 } 45 50 return -ENOENT; 46 - } 47 - 48 - /** 49 - * raw_to_temp(): Return temperature from raw value through LPAT table 50 - * 51 - * @lpat: the temperature_raw mapping table 52 - * @count: the count of the above mapping table 53 - * @raw: the raw value, used as a key to get the temerature from the 54 - * above mapping table 55 - * 56 - * A positive value will be returned on success, a negative errno will 57 - * be returned in error cases. 58 - */ 59 - static int raw_to_temp(struct acpi_lpat *lpat, int count, int raw) 60 - { 61 - int i, delta_temp, delta_raw, temp; 62 - 63 - for (i = 0; i < count - 1; i++) { 64 - if ((raw >= lpat[i].raw && raw <= lpat[i+1].raw) || 65 - (raw <= lpat[i].raw && raw >= lpat[i+1].raw)) 66 - break; 67 - } 68 - 69 - if (i == count - 1) 70 - return -ENOENT; 71 - 72 - delta_temp = lpat[i+1].temp - lpat[i].temp; 73 - delta_raw = lpat[i+1].raw - lpat[i].raw; 74 - temp = lpat[i].temp + (raw - lpat[i].raw) * delta_temp / delta_raw; 75 - 76 - return temp; 77 - } 78 - 79 - /** 80 - * temp_to_raw(): Return raw value from temperature through LPAT table 81 - * 82 - * @lpat: the temperature_raw mapping table 83 - * @count: the count of the above mapping table 84 - * @temp: the temperature, used as a key to get the raw value from the 85 - * above mapping table 86 - * 87 - * A positive value will be returned on success, a negative errno will 88 - * be returned in error cases. 89 - */ 90 - static int temp_to_raw(struct acpi_lpat *lpat, int count, int temp) 91 - { 92 - int i, delta_temp, delta_raw, raw; 93 - 94 - for (i = 0; i < count - 1; i++) { 95 - if (temp >= lpat[i].temp && temp <= lpat[i+1].temp) 96 - break; 97 - } 98 - 99 - if (i == count - 1) 100 - return -ENOENT; 101 - 102 - delta_temp = lpat[i+1].temp - lpat[i].temp; 103 - delta_raw = lpat[i+1].raw - lpat[i].raw; 104 - raw = lpat[i].raw + (temp - lpat[i].temp) * delta_raw / delta_temp; 105 - 106 - return raw; 107 - } 108 - 109 - static void pmic_thermal_lpat(struct intel_pmic_opregion *opregion, 110 - acpi_handle handle, struct device *dev) 111 - { 112 - struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 113 - union acpi_object *obj_p, *obj_e; 114 - int *lpat, i; 115 - acpi_status status; 116 - 117 - status = acpi_evaluate_object(handle, "LPAT", NULL, &buffer); 118 - if (ACPI_FAILURE(status)) 119 - return; 120 - 121 - obj_p = (union acpi_object *)buffer.pointer; 122 - if (!obj_p || (obj_p->type != ACPI_TYPE_PACKAGE) || 123 - (obj_p->package.count % 2) || (obj_p->package.count < 4)) 124 - goto out; 125 - 126 - lpat = devm_kmalloc(dev, sizeof(int) * obj_p->package.count, 127 - GFP_KERNEL); 128 - if (!lpat) 129 - goto out; 130 - 131 - for (i = 0; i < obj_p->package.count; i++) { 132 - obj_e = &obj_p->package.elements[i]; 133 - if (obj_e->type != ACPI_TYPE_INTEGER) { 134 - devm_kfree(dev, lpat); 135 - goto out; 136 - } 137 - lpat[i] = (s64)obj_e->integer.value; 138 - } 139 - 140 - opregion->lpat = (struct acpi_lpat *)lpat; 141 - opregion->lpat_count = obj_p->package.count / 2; 142 - 143 - out: 144 - kfree(buffer.pointer); 145 51 } 146 52 147 53 static acpi_status intel_pmic_power_handler(u32 function, ··· 88 192 if (raw_temp < 0) 89 193 return raw_temp; 90 194 91 - if (!opregion->lpat) { 195 + if (!opregion->lpat_table) { 92 196 *value = raw_temp; 93 197 return 0; 94 198 } 95 199 96 - temp = raw_to_temp(opregion->lpat, opregion->lpat_count, raw_temp); 200 + temp = acpi_lpat_raw_to_temp(opregion->lpat_table, raw_temp); 97 201 if (temp < 0) 98 202 return temp; 99 203 ··· 119 223 if (!opregion->data->update_aux) 120 224 return -ENXIO; 121 225 122 - if (opregion->lpat) { 123 - raw_temp = temp_to_raw(opregion->lpat, opregion->lpat_count, 124 - *value); 226 + if (opregion->lpat_table) { 227 + raw_temp = acpi_lpat_temp_to_raw(opregion->lpat_table, *value); 125 228 if (raw_temp < 0) 126 229 return raw_temp; 127 230 } else { ··· 209 314 { 210 315 acpi_status status; 211 316 struct intel_pmic_opregion *opregion; 317 + int ret; 212 318 213 319 if (!dev || !regmap || !d) 214 320 return -EINVAL; ··· 223 327 224 328 mutex_init(&opregion->lock); 225 329 opregion->regmap = regmap; 226 - pmic_thermal_lpat(opregion, handle, dev); 330 + opregion->lpat_table = acpi_lpat_get_conversion_table(handle); 227 331 228 332 status = acpi_install_address_space_handler(handle, 229 333 PMIC_POWER_OPREGION_ID, 230 334 intel_pmic_power_handler, 231 335 NULL, opregion); 232 - if (ACPI_FAILURE(status)) 233 - return -ENODEV; 336 + if (ACPI_FAILURE(status)) { 337 + ret = -ENODEV; 338 + goto out_error; 339 + } 234 340 235 341 status = acpi_install_address_space_handler(handle, 236 342 PMIC_THERMAL_OPREGION_ID, ··· 241 343 if (ACPI_FAILURE(status)) { 242 344 acpi_remove_address_space_handler(handle, PMIC_POWER_OPREGION_ID, 243 345 intel_pmic_power_handler); 244 - return -ENODEV; 346 + ret = -ENODEV; 347 + goto out_error; 245 348 } 246 349 247 350 opregion->data = d; 248 351 return 0; 352 + 353 + out_error: 354 + acpi_lpat_free_conversion_table(opregion->lpat_table); 355 + return ret; 249 356 } 250 357 EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler); 251 358