1 /* 2 * (C) Copyright 2012 3 * Henrik Nordstrom <henrik@henriknordstrom.net> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 #include <common.h> 8 #include <command.h> 9 #include <asm/arch/pmic_bus.h> 10 #include <axp_pmic.h> 11 12 static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div) 13 { 14 if (mvolt < min) 15 mvolt = min; 16 else if (mvolt > max) 17 mvolt = max; 18 19 return (mvolt - min) / div; 20 } 21 22 int axp_set_dcdc2(unsigned int mvolt) 23 { 24 int rc; 25 u8 current, target; 26 27 target = axp152_mvolt_to_target(mvolt, 700, 2275, 25); 28 29 /* Do we really need to be this gentle? It has built-in voltage slope */ 30 while ((rc = pmic_bus_read(AXP152_DCDC2_VOLTAGE, ¤t)) == 0 && 31 current != target) { 32 if (current < target) 33 current++; 34 else 35 current--; 36 rc = pmic_bus_write(AXP152_DCDC2_VOLTAGE, current); 37 if (rc) 38 break; 39 } 40 return rc; 41 } 42 43 int axp_set_dcdc3(unsigned int mvolt) 44 { 45 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50); 46 47 return pmic_bus_write(AXP152_DCDC3_VOLTAGE, target); 48 } 49 50 int axp_set_dcdc4(unsigned int mvolt) 51 { 52 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25); 53 54 return pmic_bus_write(AXP152_DCDC4_VOLTAGE, target); 55 } 56 57 int axp_set_aldo2(unsigned int mvolt) 58 { 59 u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100); 60 61 return pmic_bus_write(AXP152_LDO2_VOLTAGE, target); 62 } 63 64 int axp_init(void) 65 { 66 u8 ver; 67 int rc; 68 69 rc = pmic_bus_init(); 70 if (rc) 71 return rc; 72 73 rc = pmic_bus_read(AXP152_CHIP_VERSION, &ver); 74 if (rc) 75 return rc; 76 77 if (ver != 0x05) 78 return -EINVAL; 79 80 return 0; 81 } 82 83 int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 84 { 85 pmic_bus_write(AXP152_SHUTDOWN, AXP152_POWEROFF); 86 87 /* infinite loop during shutdown */ 88 while (1) {} 89 90 /* not reached */ 91 return 0; 92 } 93