1 /* 2 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl> 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <errno.h> 9 #include <asm/arch/p2wi.h> 10 #include <axp221.h> 11 12 static u8 axp221_mvolt_to_cfg(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 static int axp221_setbits(u8 reg, u8 bits) 23 { 24 int ret; 25 u8 val; 26 27 ret = p2wi_read(reg, &val); 28 if (ret) 29 return ret; 30 31 val |= bits; 32 return p2wi_write(reg, val); 33 } 34 35 int axp221_set_dcdc1(unsigned int mvolt) 36 { 37 int ret; 38 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100); 39 40 ret = p2wi_write(AXP221_DCDC1_CTRL, cfg); 41 if (ret) 42 return ret; 43 44 return axp221_setbits(AXP221_OUTPUT_CTRL2, 45 AXP221_OUTPUT_CTRL2_DCDC1_EN); 46 } 47 48 int axp221_set_dcdc2(unsigned int mvolt) 49 { 50 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20); 51 52 return p2wi_write(AXP221_DCDC2_CTRL, cfg); 53 } 54 55 int axp221_set_dcdc3(unsigned int mvolt) 56 { 57 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20); 58 59 return p2wi_write(AXP221_DCDC3_CTRL, cfg); 60 } 61 62 int axp221_set_dcdc4(unsigned int mvolt) 63 { 64 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20); 65 66 return p2wi_write(AXP221_DCDC4_CTRL, cfg); 67 } 68 69 int axp221_set_dcdc5(unsigned int mvolt) 70 { 71 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50); 72 73 return p2wi_write(AXP221_DCDC5_CTRL, cfg); 74 } 75 76 int axp221_set_dldo1(unsigned int mvolt) 77 { 78 int ret; 79 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); 80 81 ret = p2wi_write(AXP221_DLDO1_CTRL, cfg); 82 if (ret) 83 return ret; 84 85 return axp221_setbits(AXP221_OUTPUT_CTRL2, 86 AXP221_OUTPUT_CTRL2_DLDO1_EN); 87 } 88 89 int axp221_set_dldo2(unsigned int mvolt) 90 { 91 int ret; 92 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); 93 94 ret = p2wi_write(AXP221_DLDO2_CTRL, cfg); 95 if (ret) 96 return ret; 97 98 return axp221_setbits(AXP221_OUTPUT_CTRL2, 99 AXP221_OUTPUT_CTRL2_DLDO2_EN); 100 } 101 102 int axp221_set_dldo3(unsigned int mvolt) 103 { 104 int ret; 105 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); 106 107 ret = p2wi_write(AXP221_DLDO3_CTRL, cfg); 108 if (ret) 109 return ret; 110 111 return axp221_setbits(AXP221_OUTPUT_CTRL2, 112 AXP221_OUTPUT_CTRL2_DLDO3_EN); 113 } 114 115 int axp221_set_dldo4(unsigned int mvolt) 116 { 117 int ret; 118 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); 119 120 ret = p2wi_write(AXP221_DLDO4_CTRL, cfg); 121 if (ret) 122 return ret; 123 124 return axp221_setbits(AXP221_OUTPUT_CTRL2, 125 AXP221_OUTPUT_CTRL2_DLDO4_EN); 126 } 127 128 int axp221_set_aldo1(unsigned int mvolt) 129 { 130 int ret; 131 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); 132 133 ret = p2wi_write(AXP221_ALDO1_CTRL, cfg); 134 if (ret) 135 return ret; 136 137 return axp221_setbits(AXP221_OUTPUT_CTRL1, 138 AXP221_OUTPUT_CTRL1_ALDO1_EN); 139 } 140 141 int axp221_set_aldo2(unsigned int mvolt) 142 { 143 int ret; 144 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); 145 146 ret = p2wi_write(AXP221_ALDO2_CTRL, cfg); 147 if (ret) 148 return ret; 149 150 return axp221_setbits(AXP221_OUTPUT_CTRL1, 151 AXP221_OUTPUT_CTRL1_ALDO2_EN); 152 } 153 154 int axp221_set_aldo3(unsigned int mvolt) 155 { 156 int ret; 157 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); 158 159 ret = p2wi_write(AXP221_ALDO3_CTRL, cfg); 160 if (ret) 161 return ret; 162 163 return axp221_setbits(AXP221_OUTPUT_CTRL3, 164 AXP221_OUTPUT_CTRL3_ALDO3_EN); 165 } 166 167 int axp221_init(void) 168 { 169 u8 axp_chip_id; 170 int ret; 171 172 p2wi_init(); 173 ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR, 174 AXP221_INIT_DATA); 175 if (ret) 176 return ret; 177 178 ret = p2wi_read(AXP221_CHIP_ID, &axp_chip_id); 179 if (ret) 180 return ret; 181 182 if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17)) 183 return -ENODEV; 184 185 return 0; 186 } 187