2 * intel_pmic.c - Intel PMIC operation region driver
4 * Copyright (C) 2014 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/export.h>
17 #include <linux/acpi.h>
18 #include <linux/regmap.h>
19 #include <acpi/acpi_lpat.h>
20 #include "intel_pmic.h"
22 #define PMIC_POWER_OPREGION_ID 0x8d
23 #define PMIC_THERMAL_OPREGION_ID 0x8c
24 #define PMIC_REGS_OPREGION_ID 0x8f
26 struct intel_pmic_regs_handler_ctx
{
31 struct intel_pmic_opregion
{
33 struct acpi_lpat_conversion_table
*lpat_table
;
34 struct regmap
*regmap
;
35 struct intel_pmic_opregion_data
*data
;
36 struct intel_pmic_regs_handler_ctx ctx
;
39 static int pmic_get_reg_bit(int address
, struct pmic_table
*table
,
40 int count
, int *reg
, int *bit
)
44 for (i
= 0; i
< count
; i
++) {
45 if (table
[i
].address
== address
) {
55 static acpi_status
intel_pmic_power_handler(u32 function
,
56 acpi_physical_address address
, u32 bits
, u64
*value64
,
57 void *handler_context
, void *region_context
)
59 struct intel_pmic_opregion
*opregion
= region_context
;
60 struct regmap
*regmap
= opregion
->regmap
;
61 struct intel_pmic_opregion_data
*d
= opregion
->data
;
64 if (bits
!= 32 || !value64
)
65 return AE_BAD_PARAMETER
;
67 if (function
== ACPI_WRITE
&& !(*value64
== 0 || *value64
== 1))
68 return AE_BAD_PARAMETER
;
70 result
= pmic_get_reg_bit(address
, d
->power_table
,
71 d
->power_table_count
, ®
, &bit
);
72 if (result
== -ENOENT
)
73 return AE_BAD_PARAMETER
;
75 mutex_lock(&opregion
->lock
);
77 result
= function
== ACPI_READ
?
78 d
->get_power(regmap
, reg
, bit
, value64
) :
79 d
->update_power(regmap
, reg
, bit
, *value64
== 1);
81 mutex_unlock(&opregion
->lock
);
83 return result
? AE_ERROR
: AE_OK
;
86 static int pmic_read_temp(struct intel_pmic_opregion
*opregion
,
91 if (!opregion
->data
->get_raw_temp
)
94 raw_temp
= opregion
->data
->get_raw_temp(opregion
->regmap
, reg
);
98 if (!opregion
->lpat_table
) {
103 temp
= acpi_lpat_raw_to_temp(opregion
->lpat_table
, raw_temp
);
111 static int pmic_thermal_temp(struct intel_pmic_opregion
*opregion
, int reg
,
112 u32 function
, u64
*value
)
114 return function
== ACPI_READ
?
115 pmic_read_temp(opregion
, reg
, value
) : -EINVAL
;
118 static int pmic_thermal_aux(struct intel_pmic_opregion
*opregion
, int reg
,
119 u32 function
, u64
*value
)
123 if (function
== ACPI_READ
)
124 return pmic_read_temp(opregion
, reg
, value
);
126 if (!opregion
->data
->update_aux
)
129 if (opregion
->lpat_table
) {
130 raw_temp
= acpi_lpat_temp_to_raw(opregion
->lpat_table
, *value
);
137 return opregion
->data
->update_aux(opregion
->regmap
, reg
, raw_temp
);
140 static int pmic_thermal_pen(struct intel_pmic_opregion
*opregion
, int reg
,
141 int bit
, u32 function
, u64
*value
)
143 struct intel_pmic_opregion_data
*d
= opregion
->data
;
144 struct regmap
*regmap
= opregion
->regmap
;
146 if (!d
->get_policy
|| !d
->update_policy
)
149 if (function
== ACPI_READ
)
150 return d
->get_policy(regmap
, reg
, bit
, value
);
152 if (*value
!= 0 && *value
!= 1)
155 return d
->update_policy(regmap
, reg
, bit
, *value
);
158 static bool pmic_thermal_is_temp(int address
)
160 return (address
<= 0x3c) && !(address
% 12);
163 static bool pmic_thermal_is_aux(int address
)
165 return (address
>= 4 && address
<= 0x40 && !((address
- 4) % 12)) ||
166 (address
>= 8 && address
<= 0x44 && !((address
- 8) % 12));
169 static bool pmic_thermal_is_pen(int address
)
171 return address
>= 0x48 && address
<= 0x5c;
174 static acpi_status
intel_pmic_thermal_handler(u32 function
,
175 acpi_physical_address address
, u32 bits
, u64
*value64
,
176 void *handler_context
, void *region_context
)
178 struct intel_pmic_opregion
*opregion
= region_context
;
179 struct intel_pmic_opregion_data
*d
= opregion
->data
;
180 int reg
, bit
, result
;
182 if (bits
!= 32 || !value64
)
183 return AE_BAD_PARAMETER
;
185 result
= pmic_get_reg_bit(address
, d
->thermal_table
,
186 d
->thermal_table_count
, ®
, &bit
);
187 if (result
== -ENOENT
)
188 return AE_BAD_PARAMETER
;
190 mutex_lock(&opregion
->lock
);
192 if (pmic_thermal_is_temp(address
))
193 result
= pmic_thermal_temp(opregion
, reg
, function
, value64
);
194 else if (pmic_thermal_is_aux(address
))
195 result
= pmic_thermal_aux(opregion
, reg
, function
, value64
);
196 else if (pmic_thermal_is_pen(address
))
197 result
= pmic_thermal_pen(opregion
, reg
, bit
,
202 mutex_unlock(&opregion
->lock
);
205 if (result
== -EINVAL
)
206 return AE_BAD_PARAMETER
;
214 static acpi_status
intel_pmic_regs_handler(u32 function
,
215 acpi_physical_address address
, u32 bits
, u64
*value64
,
216 void *handler_context
, void *region_context
)
218 struct intel_pmic_opregion
*opregion
= region_context
;
225 opregion
->ctx
.addr
|= (*value64
& 0xff) << 8;
228 opregion
->ctx
.addr
|= *value64
& 0xff;
231 opregion
->ctx
.val
= *value64
& 0xff;
235 result
= regmap_write(opregion
->regmap
, opregion
->ctx
.addr
,
238 result
= regmap_read(opregion
->regmap
, opregion
->ctx
.addr
,
241 *value64
= opregion
->ctx
.val
;
243 memset(&opregion
->ctx
, 0x00, sizeof(opregion
->ctx
));
247 if (result
== -EINVAL
)
248 return AE_BAD_PARAMETER
;
256 int intel_pmic_install_opregion_handler(struct device
*dev
, acpi_handle handle
,
257 struct regmap
*regmap
,
258 struct intel_pmic_opregion_data
*d
)
261 struct intel_pmic_opregion
*opregion
;
264 if (!dev
|| !regmap
|| !d
)
270 opregion
= devm_kzalloc(dev
, sizeof(*opregion
), GFP_KERNEL
);
274 mutex_init(&opregion
->lock
);
275 opregion
->regmap
= regmap
;
276 opregion
->lpat_table
= acpi_lpat_get_conversion_table(handle
);
278 status
= acpi_install_address_space_handler(handle
,
279 PMIC_POWER_OPREGION_ID
,
280 intel_pmic_power_handler
,
282 if (ACPI_FAILURE(status
)) {
287 status
= acpi_install_address_space_handler(handle
,
288 PMIC_THERMAL_OPREGION_ID
,
289 intel_pmic_thermal_handler
,
291 if (ACPI_FAILURE(status
)) {
292 acpi_remove_address_space_handler(handle
, PMIC_POWER_OPREGION_ID
,
293 intel_pmic_power_handler
);
295 goto out_remove_power_handler
;
298 status
= acpi_install_address_space_handler(handle
,
299 PMIC_REGS_OPREGION_ID
, intel_pmic_regs_handler
, NULL
,
301 if (ACPI_FAILURE(status
)) {
303 goto out_remove_thermal_handler
;
309 out_remove_thermal_handler
:
310 acpi_remove_address_space_handler(handle
, PMIC_THERMAL_OPREGION_ID
,
311 intel_pmic_thermal_handler
);
313 out_remove_power_handler
:
314 acpi_remove_address_space_handler(handle
, PMIC_POWER_OPREGION_ID
,
315 intel_pmic_power_handler
);
318 acpi_lpat_free_conversion_table(opregion
->lpat_table
);
321 EXPORT_SYMBOL_GPL(intel_pmic_install_opregion_handler
);