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