int axp_get_sid(unsigned int *sid) { u8 *dest = (u8 *)sid; int i, ret; ret = pmic_bus_init(); if (ret) return ret; ret = pmic_bus_write(AXP221_PAGE, 1); if (ret) return ret; for (i = 0; i < 16; i++) { ret = pmic_bus_read(AXP221_SID + i, &dest[i]); if (ret) return ret; } pmic_bus_write(AXP221_PAGE, 0); for (i = 0; i < 4; i++) sid[i] = be32_to_cpu(sid[i]); return 0; }
int axp_init(void) { u8 axp_chip_id; int ret; ret = pmic_bus_init(); if (ret) return ret; ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id); if (ret) return ret; if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17)) return -ENODEV; /* * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting * from android these are sometimes on. */ ret = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT); if (ret) return ret; ret = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT); if (ret) return ret; return 0; }
int axp_set_aldo4(unsigned int mvolt) { int rc; static const unsigned int vindex[] = { 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500, 2700, 2800, 3000, 3100, 3200, 3300 }; u8 cfg, reg; if (mvolt == 0) return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4); /* Translate mvolt to register cfg value, requested <= selected */ for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--); rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®); if (rc) return rc; /* LDO4 configuration is in lower 4 bits */ reg = (reg & 0xf0) | (cfg << 0); rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg); if (rc) return rc; return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4); }
int axp221_set_eldo(int eldo_num, unsigned int mvolt) { int ret; u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); u8 addr, bits; switch (eldo_num) { case 3: addr = AXP221_ELDO3_CTRL; bits = AXP221_OUTPUT_CTRL2_ELDO3_EN; break; case 2: addr = AXP221_ELDO2_CTRL; bits = AXP221_OUTPUT_CTRL2_ELDO2_EN; break; case 1: addr = AXP221_ELDO1_CTRL; bits = AXP221_OUTPUT_CTRL2_ELDO1_EN; break; default: return -EINVAL; } if (mvolt == 0) return axp221_clrbits(AXP221_OUTPUT_CTRL2, bits); ret = pmic_bus_write(addr, cfg); if (ret) return ret; return axp221_setbits(AXP221_OUTPUT_CTRL2, bits); }
int axp_set_dcdc2(unsigned int mvolt) { int rc; u8 cfg, current; if (mvolt == 0) return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2); rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2); if (rc) return rc; cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25); /* Do we really need to be this gentle? It has built-in voltage slope */ while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, ¤t)) == 0 && current != cfg) { if (current < cfg) current++; else current--; rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current); if (rc) break; } return rc; }
static int axp_gpio_direction_output(struct udevice *dev, unsigned pin, int val) { __maybe_unused int ret; u8 reg; switch (pin) { #ifdef CONFIG_AXP221_POWER /* Only available on axp221/axp223 */ case SUNXI_GPIO_AXP0_VBUS_ENABLE: ret = pmic_bus_clrbits(AXP221_MISC_CTRL, AXP221_MISC_CTRL_N_VBUSEN_FUNC); if (ret) return ret; return axp_gpio_set_value(dev, pin, val); #endif default: reg = axp_get_gpio_ctrl_reg(pin); if (reg == 0) return -EINVAL; return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH : AXP_GPIO_CTRL_OUTPUT_LOW); } }
int axp_set_fldo(int fldo_num, unsigned int mvolt) { int ret; u8 cfg; if (fldo_num < 1 || fldo_num > 3) return -EINVAL; if (mvolt == 0) return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3, AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1)); if (fldo_num < 3) { cfg = axp818_mvolt_to_cfg(mvolt, 700, 1450, 50); ret = pmic_bus_write(AXP818_FLDO1_CTRL + (fldo_num - 1), cfg); } else { /* * Special case for FLDO3, which is DCDC5 / 2 or FLDOIN / 2 * Since FLDOIN is unknown, test against DCDC5. */ if (mvolt * 2 == CONFIG_AXP_DCDC5_VOLT) ret = pmic_bus_clrbits(AXP818_FLDO2_3_CTRL, AXP818_FLDO2_3_CTRL_FLDO3_VOL); else ret = pmic_bus_setbits(AXP818_FLDO2_3_CTRL, AXP818_FLDO2_3_CTRL_FLDO3_VOL); } if (ret) return ret; return pmic_bus_setbits(AXP818_OUTPUT_CTRL3, AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1)); }
int axp_set_aldo(int aldo_num, unsigned int mvolt) { int ret; u8 cfg; if (aldo_num < 1 || aldo_num > 3) return -EINVAL; if (mvolt == 0 && aldo_num == 3) return pmic_bus_clrbits(AXP809_OUTPUT_CTRL2, AXP809_OUTPUT_CTRL2_ALDO3_EN); if (mvolt == 0) return pmic_bus_clrbits(AXP809_OUTPUT_CTRL1, AXP809_OUTPUT_CTRL1_ALDO1_EN << (aldo_num - 1)); cfg = axp809_mvolt_to_cfg(mvolt, 700, 3300, 100); ret = pmic_bus_write(AXP809_ALDO1_CTRL + (aldo_num - 1), cfg); if (ret) return ret; if (aldo_num == 3) return pmic_bus_setbits(AXP809_OUTPUT_CTRL2, AXP809_OUTPUT_CTRL2_ALDO3_EN); return pmic_bus_clrbits(AXP809_OUTPUT_CTRL1, AXP809_OUTPUT_CTRL1_ALDO1_EN << (aldo_num - 1)); }
int axp_init(void) { u8 ver; int i, rc; rc = pmic_bus_init(); if (rc) return rc; rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver); if (rc) return rc; /* Low 4 bits is chip version */ ver &= 0x0f; if (ver != 0x1) return -EINVAL; /* Mask all interrupts */ for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) { rc = pmic_bus_write(i, 0); if (rc) return rc; } /* * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting * from android these are sometimes on. */ rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT); if (rc) return rc; rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT); if (rc) return rc; rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT); if (rc) return rc; return 0; }
int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF); /* infinite loop during shutdown */ while (1) {} /* not reached */ return 0; }
static int axp221_clrbits(u8 reg, u8 bits) { int ret; u8 val; ret = pmic_bus_read(reg, &val); if (ret) return ret; val &= ~bits; return pmic_bus_write(reg, val); }
int pmic_bus_setbits(u8 reg, u8 bits) { int ret; u8 val; ret = pmic_bus_read(reg, &val); if (ret) return ret; val |= bits; return pmic_bus_write(reg, val); }
int axp_set_dcdc3(unsigned int mvolt) { u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); int rc; if (mvolt == 0) return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3); rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg); if (rc) return rc; return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3); }
int axp_set_dcdc1(unsigned int mvolt) { int ret; u8 cfg = axp818_mvolt_to_cfg(mvolt, 1600, 3400, 100); if (mvolt == 0) return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1, AXP818_OUTPUT_CTRL1_DCDC1_EN); ret = pmic_bus_write(AXP818_DCDC1_CTRL, cfg); if (ret) return ret; return pmic_bus_setbits(AXP818_OUTPUT_CTRL1, AXP818_OUTPUT_CTRL1_DCDC1_EN); }
int axp_set_dcdc4(unsigned int mvolt) { int ret; u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20); if (mvolt == 0) return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, AXP221_OUTPUT_CTRL1_DCDC4_EN); ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg); if (ret) return ret; return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, AXP221_OUTPUT_CTRL1_DCDC4_EN); }
int axp221_set_dcdc5(unsigned int mvolt) { int ret; u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50); if (mvolt == 0) return axp221_clrbits(AXP221_OUTPUT_CTRL1, AXP221_OUTPUT_CTRL1_DCDC5_EN); ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg); if (ret) return ret; return axp221_setbits(AXP221_OUTPUT_CTRL1, AXP221_OUTPUT_CTRL1_DCDC5_EN); }
int axp221_set_dldo4(unsigned int mvolt) { int ret; u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); if (mvolt == 0) return axp221_clrbits(AXP221_OUTPUT_CTRL2, AXP221_OUTPUT_CTRL2_DLDO4_EN); ret = pmic_bus_write(AXP221_DLDO4_CTRL, cfg); if (ret) return ret; return axp221_setbits(AXP221_OUTPUT_CTRL2, AXP221_OUTPUT_CTRL2_DLDO4_EN); }
int axp_set_aldo3(unsigned int mvolt) { int ret; u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); if (mvolt == 0) return pmic_bus_clrbits(AXP221_OUTPUT_CTRL3, AXP221_OUTPUT_CTRL3_ALDO3_EN); ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg); if (ret) return ret; return pmic_bus_setbits(AXP221_OUTPUT_CTRL3, AXP221_OUTPUT_CTRL3_ALDO3_EN); }
int axp_set_dcdc3(unsigned int mvolt) { int ret; u8 cfg = axp809_mvolt_to_cfg(mvolt, 600, 1860, 20); if (mvolt == 0) return pmic_bus_clrbits(AXP809_OUTPUT_CTRL1, AXP809_OUTPUT_CTRL1_DCDC3_EN); ret = pmic_bus_write(AXP809_DCDC3_CTRL, cfg); if (ret) return ret; return pmic_bus_setbits(AXP809_OUTPUT_CTRL1, AXP809_OUTPUT_CTRL1_DCDC3_EN); }
static int axp_gpio_direction_input(struct udevice *dev, unsigned pin) { u8 reg; switch (pin) { #ifndef CONFIG_AXP152_POWER /* NA on axp152 */ case SUNXI_GPIO_AXP0_VBUS_DETECT: return 0; #endif default: reg = axp_get_gpio_ctrl_reg(pin); if (reg == 0) return -EINVAL; return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT); } }
int axp_set_dcdc4(unsigned int mvolt) { int ret; u8 cfg = axp809_mvolt_to_cfg(mvolt, 600, 1540, 20); if (mvolt >= 1540) cfg = 0x30 + axp809_mvolt_to_cfg(mvolt, 1800, 2600, 100); if (mvolt == 0) return pmic_bus_clrbits(AXP809_OUTPUT_CTRL1, AXP809_OUTPUT_CTRL1_DCDC4_EN); ret = pmic_bus_write(AXP809_DCDC5_CTRL, cfg); if (ret) return ret; return pmic_bus_setbits(AXP809_OUTPUT_CTRL1, AXP809_OUTPUT_CTRL1_DCDC4_EN); }
int axp_set_eldo(int eldo_num, unsigned int mvolt) { int ret; u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); if (eldo_num < 1 || eldo_num > 3) return -EINVAL; if (mvolt == 0) return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2, AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1)); ret = pmic_bus_write(AXP221_ELDO1_CTRL + (eldo_num - 1), cfg); if (ret) return ret; return pmic_bus_setbits(AXP221_OUTPUT_CTRL2, AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1)); }
int axp_set_aldo3(unsigned int mvolt) { u8 cfg; int rc; if (mvolt == 0) return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3); if (mvolt == -1) cfg = 0x80; /* determined by LDO3IN pin */ else cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg); if (rc) return rc; return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3); }
int axp_set_dldo(int dldo_num, unsigned int mvolt) { u8 cfg = axp809_mvolt_to_cfg(mvolt, 700, 3300, 100); int ret; if (dldo_num < 1 || dldo_num > 2) return -EINVAL; if (mvolt == 0) return pmic_bus_clrbits(AXP809_OUTPUT_CTRL2, AXP809_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); if (dldo_num == 1 && mvolt > 3300) cfg += 1 + axp809_mvolt_to_cfg(mvolt, 3400, 4200, 200); ret = pmic_bus_write(AXP809_DLDO1_CTRL + (dldo_num - 1), cfg); if (ret) return ret; return pmic_bus_setbits(AXP809_OUTPUT_CTRL2, AXP809_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); }
int axp_set_dcdc3(unsigned int mvolt) { int ret; u8 cfg; if (mvolt >= 1220) cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20); else cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10); if (mvolt == 0) return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1, AXP818_OUTPUT_CTRL1_DCDC3_EN); ret = pmic_bus_write(AXP818_DCDC3_CTRL, cfg); if (ret) return ret; return pmic_bus_setbits(AXP818_OUTPUT_CTRL1, AXP818_OUTPUT_CTRL1_DCDC3_EN); }
int axp_set_dcdc5(unsigned int mvolt) { int ret; u8 cfg; if (mvolt >= 1140) cfg = 32 + axp818_mvolt_to_cfg(mvolt, 1140, 1840, 20); else cfg = axp818_mvolt_to_cfg(mvolt, 800, 1120, 10); if (mvolt == 0) return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1, AXP818_OUTPUT_CTRL1_DCDC5_EN); ret = pmic_bus_write(AXP818_DCDC5_CTRL, cfg); if (ret) return ret; return pmic_bus_setbits(AXP818_OUTPUT_CTRL1, AXP818_OUTPUT_CTRL1_DCDC5_EN); }
int axp_set_aldo2(unsigned int mvolt) { int rc; u8 cfg, reg; if (mvolt == 0) return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2); cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100); rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®); if (rc) return rc; /* LDO2 configuration is in upper 4 bits */ reg = (reg & 0x0f) | (cfg << 4); rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg); if (rc) return rc; return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2); }
static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val) { u8 reg; switch (pin) { #ifdef CONFIG_AXP221_POWER /* Only available on axp221/axp223 */ case SUNXI_GPIO_AXP0_VBUS_ENABLE: if (val) return pmic_bus_setbits(AXP221_VBUS_IPSOUT, AXP221_VBUS_IPSOUT_DRIVEBUS); else return pmic_bus_clrbits(AXP221_VBUS_IPSOUT, AXP221_VBUS_IPSOUT_DRIVEBUS); #endif default: reg = axp_get_gpio_ctrl_reg(pin); if (reg == 0) return -EINVAL; return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH : AXP_GPIO_CTRL_OUTPUT_LOW); } }