2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-04-25 12:07:37 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
|
|
|
|
*
|
|
|
|
* Sunxi PMIC bus access helpers
|
|
|
|
*
|
|
|
|
* The axp152 & axp209 use an i2c bus, the axp221 uses the p2wi bus and the
|
|
|
|
* axp223 uses the rsb bus, these functions abstract this.
|
|
|
|
*/
|
|
|
|
|
2021-10-08 05:17:20 +00:00
|
|
|
#include <axp_pmic.h>
|
2015-04-25 12:07:37 +00:00
|
|
|
#include <common.h>
|
2021-10-08 05:17:23 +00:00
|
|
|
#include <dm.h>
|
2015-04-25 12:07:37 +00:00
|
|
|
#include <asm/arch/p2wi.h>
|
|
|
|
#include <asm/arch/rsb.h>
|
2015-04-25 20:18:09 +00:00
|
|
|
#include <i2c.h>
|
2021-10-08 05:17:23 +00:00
|
|
|
#include <power/pmic.h>
|
2015-04-25 12:07:37 +00:00
|
|
|
#include <asm/arch/pmic_bus.h>
|
|
|
|
|
2015-04-25 20:18:09 +00:00
|
|
|
#define AXP152_I2C_ADDR 0x30
|
|
|
|
|
|
|
|
#define AXP209_I2C_ADDR 0x34
|
|
|
|
|
2021-01-11 20:11:33 +00:00
|
|
|
#define AXP305_I2C_ADDR 0x36
|
2023-07-30 00:11:01 +00:00
|
|
|
#define AXP313_I2C_ADDR 0x36
|
2021-01-11 20:11:33 +00:00
|
|
|
|
2015-04-25 12:07:37 +00:00
|
|
|
#define AXP221_CHIP_ADDR 0x68
|
|
|
|
|
2021-10-08 05:17:23 +00:00
|
|
|
#if CONFIG_IS_ENABLED(PMIC_AXP)
|
|
|
|
static struct udevice *pmic;
|
|
|
|
#else
|
2021-10-08 05:17:22 +00:00
|
|
|
static int pmic_i2c_address(void)
|
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_AXP152_POWER))
|
|
|
|
return AXP152_I2C_ADDR;
|
|
|
|
if (IS_ENABLED(CONFIG_AXP305_POWER))
|
|
|
|
return AXP305_I2C_ADDR;
|
2023-07-30 00:11:01 +00:00
|
|
|
if (IS_ENABLED(CONFIG_AXP313_POWER))
|
|
|
|
return AXP313_I2C_ADDR;
|
2021-10-08 05:17:22 +00:00
|
|
|
|
|
|
|
/* Other AXP2xx and AXP8xx variants */
|
|
|
|
return AXP209_I2C_ADDR;
|
|
|
|
}
|
2021-10-08 05:17:23 +00:00
|
|
|
#endif
|
2021-10-08 05:17:22 +00:00
|
|
|
|
2015-04-25 12:07:37 +00:00
|
|
|
int pmic_bus_init(void)
|
|
|
|
{
|
|
|
|
/* This cannot be 0 because it is used in SPL before BSS is ready */
|
|
|
|
static int needs_init = 1;
|
2021-10-08 05:17:22 +00:00
|
|
|
int ret = 0;
|
2015-04-25 12:07:37 +00:00
|
|
|
|
|
|
|
if (!needs_init)
|
|
|
|
return 0;
|
|
|
|
|
2021-10-08 05:17:23 +00:00
|
|
|
#if CONFIG_IS_ENABLED(PMIC_AXP)
|
|
|
|
ret = uclass_get_device_by_driver(UCLASS_PMIC, DM_DRIVER_GET(axp_pmic),
|
|
|
|
&pmic);
|
|
|
|
#else
|
2021-10-08 05:17:22 +00:00
|
|
|
if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI)) {
|
|
|
|
p2wi_init();
|
|
|
|
ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR,
|
|
|
|
AXP_PMIC_MODE_REG,
|
|
|
|
AXP_PMIC_MODE_P2WI);
|
|
|
|
} else if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB)) {
|
|
|
|
ret = rsb_init();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-04-25 12:07:37 +00:00
|
|
|
|
2021-10-08 05:17:22 +00:00
|
|
|
ret = rsb_set_device_address(AXP_PMIC_PRI_DEVICE_ADDR,
|
|
|
|
AXP_PMIC_PRI_RUNTIME_ADDR);
|
|
|
|
}
|
2021-10-08 05:17:23 +00:00
|
|
|
#endif
|
2021-10-08 05:17:22 +00:00
|
|
|
|
|
|
|
needs_init = ret;
|
2015-04-25 12:07:37 +00:00
|
|
|
|
2021-10-08 05:17:22 +00:00
|
|
|
return ret;
|
2015-04-25 12:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_bus_read(u8 reg, u8 *data)
|
|
|
|
{
|
2021-10-08 05:17:23 +00:00
|
|
|
#if CONFIG_IS_ENABLED(PMIC_AXP)
|
|
|
|
return pmic_read(pmic, reg, data, 1);
|
|
|
|
#else
|
2021-10-08 05:17:22 +00:00
|
|
|
if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
|
|
|
|
return p2wi_read(reg, data);
|
|
|
|
if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
|
|
|
|
return rsb_read(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);
|
|
|
|
|
|
|
|
return i2c_read(pmic_i2c_address(), reg, 1, data, 1);
|
2021-10-08 05:17:23 +00:00
|
|
|
#endif
|
2015-04-25 12:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_bus_write(u8 reg, u8 data)
|
|
|
|
{
|
2021-10-08 05:17:23 +00:00
|
|
|
#if CONFIG_IS_ENABLED(PMIC_AXP)
|
|
|
|
return pmic_write(pmic, reg, &data, 1);
|
|
|
|
#else
|
2021-10-08 05:17:22 +00:00
|
|
|
if (IS_ENABLED(CONFIG_SYS_I2C_SUN6I_P2WI))
|
|
|
|
return p2wi_write(reg, data);
|
|
|
|
if (IS_ENABLED(CONFIG_SYS_I2C_SUN8I_RSB))
|
|
|
|
return rsb_write(AXP_PMIC_PRI_RUNTIME_ADDR, reg, data);
|
|
|
|
|
|
|
|
return i2c_write(pmic_i2c_address(), reg, 1, &data, 1);
|
2021-10-08 05:17:23 +00:00
|
|
|
#endif
|
2015-04-25 12:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_bus_setbits(u8 reg, u8 bits)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u8 val;
|
|
|
|
|
|
|
|
ret = pmic_bus_read(reg, &val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-11-21 18:05:26 +00:00
|
|
|
if ((val & bits) == bits)
|
|
|
|
return 0;
|
|
|
|
|
2015-04-25 12:07:37 +00:00
|
|
|
val |= bits;
|
|
|
|
return pmic_bus_write(reg, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_bus_clrbits(u8 reg, u8 bits)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u8 val;
|
|
|
|
|
|
|
|
ret = pmic_bus_read(reg, &val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-11-21 18:05:26 +00:00
|
|
|
if (!(val & bits))
|
|
|
|
return 0;
|
|
|
|
|
2015-04-25 12:07:37 +00:00
|
|
|
val &= ~bits;
|
|
|
|
return pmic_bus_write(reg, val);
|
|
|
|
}
|