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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/arch/p2wi.h>
|
|
|
|
#include <asm/arch/rsb.h>
|
2015-04-25 20:18:09 +00:00
|
|
|
#include <i2c.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
|
|
|
|
|
2015-04-25 12:07:37 +00:00
|
|
|
#define AXP221_CHIP_ADDR 0x68
|
|
|
|
#define AXP221_CTRL_ADDR 0x3e
|
|
|
|
#define AXP221_INIT_DATA 0x3e
|
|
|
|
|
2015-11-28 17:07:22 +00:00
|
|
|
/* AXP818 device and runtime addresses are same as AXP223 */
|
2015-04-25 12:07:37 +00:00
|
|
|
#define AXP223_DEVICE_ADDR 0x3a3
|
|
|
|
#define AXP223_RUNTIME_ADDR 0x2d
|
|
|
|
|
|
|
|
int pmic_bus_init(void)
|
|
|
|
{
|
|
|
|
/* This cannot be 0 because it is used in SPL before BSS is ready */
|
|
|
|
static int needs_init = 1;
|
2015-04-25 20:18:09 +00:00
|
|
|
__maybe_unused int ret;
|
2015-04-25 12:07:37 +00:00
|
|
|
|
|
|
|
if (!needs_init)
|
|
|
|
return 0;
|
|
|
|
|
2016-05-02 02:28:15 +00:00
|
|
|
#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
|
2015-04-25 20:18:09 +00:00
|
|
|
# ifdef CONFIG_MACH_SUN6I
|
2015-04-25 12:07:37 +00:00
|
|
|
p2wi_init();
|
|
|
|
ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
|
|
|
|
AXP221_INIT_DATA);
|
2016-11-30 07:30:30 +00:00
|
|
|
# elif defined CONFIG_MACH_SUN8I_R40
|
|
|
|
/* Nothing. R40 uses the AXP221s in I2C mode */
|
|
|
|
ret = 0;
|
2015-04-25 20:18:09 +00:00
|
|
|
# else
|
2015-04-25 12:07:37 +00:00
|
|
|
ret = rsb_init();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
|
2015-04-25 20:18:09 +00:00
|
|
|
# endif
|
2015-04-25 12:07:37 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-04-25 20:18:09 +00:00
|
|
|
#endif
|
2015-04-25 12:07:37 +00:00
|
|
|
|
|
|
|
needs_init = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_bus_read(u8 reg, u8 *data)
|
|
|
|
{
|
2015-04-25 20:18:09 +00:00
|
|
|
#ifdef CONFIG_AXP152_POWER
|
|
|
|
return i2c_read(AXP152_I2C_ADDR, reg, 1, data, 1);
|
|
|
|
#elif defined CONFIG_AXP209_POWER
|
|
|
|
return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
|
2021-01-11 20:11:33 +00:00
|
|
|
#elif defined CONFIG_AXP305_POWER
|
|
|
|
return i2c_read(AXP305_I2C_ADDR, reg, 1, data, 1);
|
2016-05-02 02:28:15 +00:00
|
|
|
#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
|
2015-04-25 20:18:09 +00:00
|
|
|
# ifdef CONFIG_MACH_SUN6I
|
2015-04-25 12:07:37 +00:00
|
|
|
return p2wi_read(reg, data);
|
2016-11-30 07:30:30 +00:00
|
|
|
# elif defined CONFIG_MACH_SUN8I_R40
|
|
|
|
return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
|
2015-04-25 20:18:09 +00:00
|
|
|
# else
|
2015-04-25 12:07:37 +00:00
|
|
|
return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
|
2015-04-25 20:18:09 +00:00
|
|
|
# endif
|
2015-04-25 12:07:37 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_bus_write(u8 reg, u8 data)
|
|
|
|
{
|
2015-04-25 20:18:09 +00:00
|
|
|
#ifdef CONFIG_AXP152_POWER
|
|
|
|
return i2c_write(AXP152_I2C_ADDR, reg, 1, &data, 1);
|
|
|
|
#elif defined CONFIG_AXP209_POWER
|
|
|
|
return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
|
2021-01-11 20:11:33 +00:00
|
|
|
#elif defined CONFIG_AXP305_POWER
|
|
|
|
return i2c_write(AXP305_I2C_ADDR, reg, 1, &data, 1);
|
2016-05-02 02:28:15 +00:00
|
|
|
#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
|
2015-04-25 20:18:09 +00:00
|
|
|
# ifdef CONFIG_MACH_SUN6I
|
2015-04-25 12:07:37 +00:00
|
|
|
return p2wi_write(reg, data);
|
2016-11-30 07:30:30 +00:00
|
|
|
# elif defined CONFIG_MACH_SUN8I_R40
|
|
|
|
return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
|
2015-04-25 20:18:09 +00:00
|
|
|
# else
|
2015-04-25 12:07:37 +00:00
|
|
|
return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
|
2015-04-25 20:18:09 +00:00
|
|
|
# endif
|
2015-04-25 12:07:37 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|