1 /* 2 * Copyright (C) 2015 Google, Inc 3 * Written by Simon Glass <sjg@chromium.org> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <dm.h> 10 #include <errno.h> 11 #include <power/rk8xx_pmic.h> 12 #include <power/pmic.h> 13 14 DECLARE_GLOBAL_DATA_PTR; 15 16 /* 17 * Only when system suspend while U-Boot charge needs this config support 18 */ 19 #ifdef CONFIG_DM_CHARGE_DISPLAY 20 static struct reg_data rk817_init_reg[] = { 21 /* Set pmic_sleep as sleep function */ 22 { RK817_PMIC_SYS_CFG3, 0x08, 0x18 }, 23 /* Set pmic_int active low */ 24 { RK817_GPIO_INT_CFG, 0x00, 0x02 }, 25 }; 26 #endif 27 28 static const struct pmic_child_info pmic_children_info[] = { 29 { .prefix = "DCDC", .driver = "rk8xx_buck"}, 30 { .prefix = "LDO", .driver = "rk8xx_ldo"}, 31 { .prefix = "SWITCH", .driver = "rk8xx_switch"}, 32 { }, 33 }; 34 35 static const struct pmic_child_info power_key_info[] = { 36 { .prefix = "pwrkey", .driver = "rk8xx_pwrkey"}, 37 { }, 38 }; 39 40 static const struct pmic_child_info fuel_gauge_info[] = { 41 { .prefix = "battery", .driver = "rk818_fg"}, 42 { .prefix = "battery", .driver = "rk817_fg"}, 43 { .prefix = "battery", .driver = "rk816_fg"}, 44 { }, 45 }; 46 47 static const struct pmic_child_info rk817_codec_info[] = { 48 { .prefix = "codec", .driver = "rk817_codec"}, 49 { }, 50 }; 51 52 static int rk8xx_reg_count(struct udevice *dev) 53 { 54 return RK808_NUM_OF_REGS; 55 } 56 57 static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff, 58 int len) 59 { 60 int ret; 61 62 ret = dm_i2c_write(dev, reg, buff, len); 63 if (ret) { 64 printf("%s: write reg 0x%02x failed, ret=%d\n", __func__, reg, ret); 65 return ret; 66 } 67 68 return 0; 69 } 70 71 static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len) 72 { 73 int ret; 74 75 ret = dm_i2c_read(dev, reg, buff, len); 76 if (ret) { 77 printf("%s: read reg 0x%02x failed, ret=%d\n", __func__, reg, ret); 78 return ret; 79 } 80 81 return 0; 82 } 83 84 static int rk8xx_shutdown(struct udevice *dev) 85 { 86 struct rk8xx_priv *priv = dev_get_priv(dev); 87 u8 val, dev_off, devctrl_reg; 88 int ret = 0; 89 90 switch (priv->variant) { 91 case RK808_ID: 92 devctrl_reg = REG_DEVCTRL; 93 dev_off = BIT(3); 94 break; 95 case RK805_ID: 96 case RK816_ID: 97 case RK818_ID: 98 devctrl_reg = REG_DEVCTRL; 99 dev_off = BIT(0); 100 break; 101 case RK809_ID: 102 case RK817_ID: 103 devctrl_reg = RK817_REG_SYS_CFG3; 104 dev_off = BIT(0); 105 break; 106 default: 107 printf("Unknown PMIC: RK%x\n", priv->variant); 108 return -EINVAL; 109 } 110 111 ret = dm_i2c_read(dev, devctrl_reg, &val, 1); 112 if (ret) { 113 printf("%s: read reg 0x%02x failed, ret=%d\n", __func__, devctrl_reg, ret); 114 return ret; 115 } 116 117 val |= dev_off; 118 ret = dm_i2c_write(dev, devctrl_reg, &val, 1); 119 if (ret) { 120 printf("%s: write reg 0x%02x failed, ret=%d\n", __func__, devctrl_reg, ret); 121 return ret; 122 } 123 124 return 0; 125 } 126 127 #if CONFIG_IS_ENABLED(PMIC_CHILDREN) 128 static int rk8xx_bind(struct udevice *dev) 129 { 130 ofnode regulators_node; 131 int children; 132 133 regulators_node = dev_read_subnode(dev, "regulators"); 134 if (!ofnode_valid(regulators_node)) { 135 debug("%s: %s regulators subnode not found!\n", __func__, 136 dev->name); 137 return -ENXIO; 138 } 139 140 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name); 141 142 children = pmic_bind_children(dev, regulators_node, pmic_children_info); 143 if (!children) 144 debug("%s: %s - no child found\n", __func__, dev->name); 145 146 children = pmic_bind_children(dev, dev->node, power_key_info); 147 if (!children) 148 debug("%s: %s - no child found\n", __func__, dev->name); 149 150 children = pmic_bind_children(dev, dev->node, fuel_gauge_info); 151 if (!children) 152 debug("%s: %s - no child found\n", __func__, dev->name); 153 154 children = pmic_bind_children(dev, dev->node, rk817_codec_info); 155 if (!children) 156 debug("%s: %s - no child found\n", __func__, dev->name); 157 158 /* Always return success for this device */ 159 return 0; 160 } 161 #endif 162 163 static int rk8xx_probe(struct udevice *dev) 164 { 165 struct rk8xx_priv *priv = dev_get_priv(dev); 166 struct reg_data *init_data = NULL; 167 int init_data_num = 0; 168 int ret = 0, i, show_variant; 169 uint8_t msb, lsb, id_msb, id_lsb; 170 uint8_t on_source = 0, off_source = 0; 171 172 /* read Chip variant */ 173 if (device_is_compatible(dev, "rockchip,rk817") || 174 device_is_compatible(dev, "rockchip,rk809")) { 175 id_msb = RK817_ID_MSB; 176 id_lsb = RK817_ID_LSB; 177 } else { 178 id_msb = ID_MSB; 179 id_lsb = ID_LSB; 180 } 181 182 ret = rk8xx_read(dev, id_msb, &msb, 1); 183 if (ret) 184 return ret; 185 ret = rk8xx_read(dev, id_lsb, &lsb, 1); 186 if (ret) 187 return ret; 188 189 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK; 190 show_variant = priv->variant; 191 switch (priv->variant) { 192 case RK808_ID: 193 show_variant = 0x808; /* RK808 hardware ID is 0 */ 194 break; 195 case RK805_ID: 196 case RK816_ID: 197 case RK818_ID: 198 on_source = RK8XX_ON_SOURCE; 199 off_source = RK8XX_OFF_SOURCE; 200 break; 201 case RK809_ID: 202 case RK817_ID: 203 on_source = RK817_ON_SOURCE; 204 off_source = RK817_OFF_SOURCE; 205 #ifdef CONFIG_DM_CHARGE_DISPLAY 206 init_data = rk817_init_reg; 207 init_data_num = ARRAY_SIZE(rk817_init_reg); 208 #endif 209 break; 210 default: 211 printf("Unknown PMIC: RK%x!!\n", priv->variant); 212 return -EINVAL; 213 } 214 215 for (i = 0; i < init_data_num; i++) { 216 ret = pmic_clrsetbits(dev, 217 init_data[i].reg, 218 init_data[i].mask, 219 init_data[i].val); 220 if (ret < 0) { 221 printf("%s: i2c set reg 0x%x failed, ret=%d\n", 222 __func__, init_data[i].reg, ret); 223 } 224 225 debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg, 226 pmic_reg_read(dev, init_data[i].reg)); 227 } 228 229 printf("PMIC: RK%x ", show_variant); 230 231 if (on_source && off_source) { 232 on_source = pmic_reg_read(dev, on_source); 233 off_source = pmic_reg_read(dev, off_source); 234 printf("(on=0x%02x, off=0x%02x)", 235 pmic_reg_read(dev, on_source), 236 pmic_reg_read(dev, off_source)); 237 } 238 printf("\n"); 239 240 return 0; 241 } 242 243 static struct dm_pmic_ops rk8xx_ops = { 244 .reg_count = rk8xx_reg_count, 245 .read = rk8xx_read, 246 .write = rk8xx_write, 247 .shutdown = rk8xx_shutdown, 248 }; 249 250 static const struct udevice_id rk8xx_ids[] = { 251 { .compatible = "rockchip,rk805" }, 252 { .compatible = "rockchip,rk808" }, 253 { .compatible = "rockchip,rk809" }, 254 { .compatible = "rockchip,rk816" }, 255 { .compatible = "rockchip,rk817" }, 256 { .compatible = "rockchip,rk818" }, 257 { } 258 }; 259 260 U_BOOT_DRIVER(pmic_rk8xx) = { 261 .name = "rk8xx pmic", 262 .id = UCLASS_PMIC, 263 .of_match = rk8xx_ids, 264 #if CONFIG_IS_ENABLED(PMIC_CHILDREN) 265 .bind = rk8xx_bind, 266 #endif 267 .priv_auto_alloc_size = sizeof(struct rk8xx_priv), 268 .probe = rk8xx_probe, 269 .ops = &rk8xx_ops, 270 }; 271