1 // SPDX-License-Identifier: GPL-2.0
3 * XPower AXP288 PMIC operation region driver
5 * Copyright (C) 2014 Intel Corporation. All rights reserved.
8 #include <linux/acpi.h>
9 #include <linux/init.h>
10 #include <linux/mfd/axp20x.h>
11 #include <linux/regmap.h>
12 #include <linux/platform_device.h>
13 #include <asm/iosf_mbi.h>
14 #include "intel_pmic.h"
16 #define XPOWER_GPADC_LOW 0x5b
17 #define XPOWER_GPI1_CTRL 0x92
19 #define GPI1_LDO_MASK GENMASK(2, 0)
20 #define GPI1_LDO_ON (3 << 0)
21 #define GPI1_LDO_OFF (4 << 0)
23 #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK GENMASK(1, 0)
24 #define AXP288_ADC_TS_CURRENT_OFF (0 << 0)
25 #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING (1 << 0)
26 #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND (2 << 0)
27 #define AXP288_ADC_TS_CURRENT_ON (3 << 0)
29 static const struct pmic_table power_table
[] = {
131 /* TMP0 - TMP5 are the same, all from GPADC */
132 static const struct pmic_table thermal_table
[] = {
135 .reg
= XPOWER_GPADC_LOW
139 .reg
= XPOWER_GPADC_LOW
143 .reg
= XPOWER_GPADC_LOW
147 .reg
= XPOWER_GPADC_LOW
151 .reg
= XPOWER_GPADC_LOW
155 .reg
= XPOWER_GPADC_LOW
159 static int intel_xpower_pmic_get_power(struct regmap
*regmap
, int reg
,
164 if (regmap_read(regmap
, reg
, &data
))
167 /* GPIO1 LDO regulator needs special handling */
168 if (reg
== XPOWER_GPI1_CTRL
)
169 *value
= ((data
& GPI1_LDO_MASK
) == GPI1_LDO_ON
);
171 *value
= (data
& BIT(bit
)) ? 1 : 0;
176 static int intel_xpower_pmic_update_power(struct regmap
*regmap
, int reg
,
181 ret
= iosf_mbi_block_punit_i2c_access();
185 /* GPIO1 LDO regulator needs special handling */
186 if (reg
== XPOWER_GPI1_CTRL
) {
187 ret
= regmap_update_bits(regmap
, reg
, GPI1_LDO_MASK
,
188 on
? GPI1_LDO_ON
: GPI1_LDO_OFF
);
192 if (regmap_read(regmap
, reg
, &data
)) {
202 if (regmap_write(regmap
, reg
, data
))
205 iosf_mbi_unblock_punit_i2c_access();
211 * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC
213 * @regmap: regmap of the PMIC device
214 * @reg: register to get the reading
216 * Return a positive value on success, errno on failure.
218 static int intel_xpower_pmic_get_raw_temp(struct regmap
*regmap
, int reg
)
220 int ret
, adc_ts_pin_ctrl
;
224 * The current-source used for the battery temp-sensor (TS) is shared
225 * with the GPADC. For proper fuel-gauge and charger operation the TS
226 * current-source needs to be permanently on. But to read the GPADC we
227 * need to temporary switch the TS current-source to ondemand, so that
228 * the GPADC can use it, otherwise we will always read an all 0 value.
230 * Note that the switching from on to on-ondemand is not necessary
231 * when the TS current-source is off (this happens on devices which
232 * do not use the TS-pin).
234 ret
= regmap_read(regmap
, AXP288_ADC_TS_PIN_CTRL
, &adc_ts_pin_ctrl
);
238 if (adc_ts_pin_ctrl
& AXP288_ADC_TS_CURRENT_ON_OFF_MASK
) {
240 * AXP288_ADC_TS_PIN_CTRL reads are cached by the regmap, so
241 * this does to a single I2C-transfer, and thus there is no
242 * need to explicitly call iosf_mbi_block_punit_i2c_access().
244 ret
= regmap_update_bits(regmap
, AXP288_ADC_TS_PIN_CTRL
,
245 AXP288_ADC_TS_CURRENT_ON_OFF_MASK
,
246 AXP288_ADC_TS_CURRENT_ON_ONDEMAND
);
250 /* Wait a bit after switching the current-source */
251 usleep_range(6000, 10000);
254 ret
= iosf_mbi_block_punit_i2c_access();
258 ret
= regmap_bulk_read(regmap
, AXP288_GP_ADC_H
, buf
, sizeof(buf
));
260 ret
= (buf
[0] << 4) + ((buf
[1] >> 4) & 0x0f);
262 if (adc_ts_pin_ctrl
& AXP288_ADC_TS_CURRENT_ON_OFF_MASK
) {
263 regmap_update_bits(regmap
, AXP288_ADC_TS_PIN_CTRL
,
264 AXP288_ADC_TS_CURRENT_ON_OFF_MASK
,
265 AXP288_ADC_TS_CURRENT_ON
);
268 iosf_mbi_unblock_punit_i2c_access();
273 static int intel_xpower_exec_mipi_pmic_seq_element(struct regmap
*regmap
,
274 u16 i2c_address
, u32 reg_address
,
277 struct device
*dev
= regmap_get_device(regmap
);
280 if (i2c_address
!= 0x34) {
281 dev_err(dev
, "Unexpected i2c-addr: 0x%02x (reg-addr 0x%x value 0x%x mask 0x%x)\n",
282 i2c_address
, reg_address
, value
, mask
);
286 ret
= iosf_mbi_block_punit_i2c_access();
290 ret
= regmap_update_bits(regmap
, reg_address
, mask
, value
);
292 iosf_mbi_unblock_punit_i2c_access();
297 static int intel_xpower_lpat_raw_to_temp(struct acpi_lpat_conversion_table
*lpat_table
,
300 struct acpi_lpat first
= lpat_table
->lpat
[0];
301 struct acpi_lpat last
= lpat_table
->lpat
[lpat_table
->lpat_count
- 1];
304 * Some LPAT tables in the ACPI Device for the AXP288 PMIC for some
305 * reason only describe a small temperature range, e.g. 27° - 37°
306 * Celcius. Resulting in errors when the tablet is idle in a cool room.
308 * To avoid these errors clamp the raw value to be inside the LPAT.
310 if (first
.raw
< last
.raw
)
311 raw
= clamp(raw
, first
.raw
, last
.raw
);
313 raw
= clamp(raw
, last
.raw
, first
.raw
);
315 return acpi_lpat_raw_to_temp(lpat_table
, raw
);
318 static const struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data
= {
319 .get_power
= intel_xpower_pmic_get_power
,
320 .update_power
= intel_xpower_pmic_update_power
,
321 .get_raw_temp
= intel_xpower_pmic_get_raw_temp
,
322 .exec_mipi_pmic_seq_element
= intel_xpower_exec_mipi_pmic_seq_element
,
323 .lpat_raw_to_temp
= intel_xpower_lpat_raw_to_temp
,
324 .power_table
= power_table
,
325 .power_table_count
= ARRAY_SIZE(power_table
),
326 .thermal_table
= thermal_table
,
327 .thermal_table_count
= ARRAY_SIZE(thermal_table
),
328 .pmic_i2c_address
= 0x34,
331 static acpi_status
intel_xpower_pmic_gpio_handler(u32 function
,
332 acpi_physical_address address
, u32 bit_width
, u64
*value
,
333 void *handler_context
, void *region_context
)
338 static int intel_xpower_pmic_opregion_probe(struct platform_device
*pdev
)
340 struct device
*parent
= pdev
->dev
.parent
;
341 struct axp20x_dev
*axp20x
= dev_get_drvdata(parent
);
345 status
= acpi_install_address_space_handler(ACPI_HANDLE(parent
),
346 ACPI_ADR_SPACE_GPIO
, intel_xpower_pmic_gpio_handler
,
348 if (ACPI_FAILURE(status
))
351 result
= intel_pmic_install_opregion_handler(&pdev
->dev
,
352 ACPI_HANDLE(parent
), axp20x
->regmap
,
353 &intel_xpower_pmic_opregion_data
);
355 acpi_remove_address_space_handler(ACPI_HANDLE(parent
),
357 intel_xpower_pmic_gpio_handler
);
362 static struct platform_driver intel_xpower_pmic_opregion_driver
= {
363 .probe
= intel_xpower_pmic_opregion_probe
,
365 .name
= "axp288_pmic_acpi",
368 builtin_platform_driver(intel_xpower_pmic_opregion_driver
);