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_REG", .driver = "rk8xx_buck"}, 30 { .prefix = "LDO_REG", .driver = "rk8xx_ldo"}, 31 { .prefix = "SWITCH_REG", .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 int rk8xx_reg_count(struct udevice *dev) 48 { 49 return RK808_NUM_OF_REGS; 50 } 51 52 static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff, 53 int len) 54 { 55 int ret; 56 57 ret = dm_i2c_write(dev, reg, buff, len); 58 if (ret) { 59 printf("%s: write reg 0x%02x failed, ret=%d\n", __func__, reg, ret); 60 return ret; 61 } 62 63 return 0; 64 } 65 66 static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len) 67 { 68 int ret; 69 70 ret = dm_i2c_read(dev, reg, buff, len); 71 if (ret) { 72 printf("%s: read reg 0x%02x failed, ret=%d\n", __func__, reg, ret); 73 return ret; 74 } 75 76 return 0; 77 } 78 79 static int rk8xx_shutdown(struct udevice *dev) 80 { 81 struct rk8xx_priv *priv = dev_get_priv(dev); 82 u8 val, dev_off, devctrl_reg; 83 int ret = 0; 84 85 switch (priv->variant) { 86 case RK808_ID: 87 devctrl_reg = REG_DEVCTRL; 88 dev_off = BIT(3); 89 break; 90 case RK805_ID: 91 case RK816_ID: 92 case RK818_ID: 93 devctrl_reg = REG_DEVCTRL; 94 dev_off = BIT(0); 95 break; 96 case RK809_ID: 97 case RK817_ID: 98 devctrl_reg = RK817_REG_SYS_CFG3; 99 dev_off = BIT(0); 100 break; 101 default: 102 printf("Unknown PMIC: RK%x\n", priv->variant); 103 return -EINVAL; 104 } 105 106 ret = dm_i2c_read(dev, devctrl_reg, &val, 1); 107 if (ret) { 108 printf("%s: read reg 0x%02x failed, ret=%d\n", __func__, devctrl_reg, ret); 109 return ret; 110 } 111 112 val |= dev_off; 113 ret = dm_i2c_write(dev, devctrl_reg, &val, 1); 114 if (ret) { 115 printf("%s: write reg 0x%02x failed, ret=%d\n", __func__, devctrl_reg, ret); 116 return ret; 117 } 118 119 return 0; 120 } 121 122 #if CONFIG_IS_ENABLED(PMIC_CHILDREN) 123 static int rk8xx_bind(struct udevice *dev) 124 { 125 ofnode regulators_node; 126 int children; 127 128 regulators_node = dev_read_subnode(dev, "regulators"); 129 if (!ofnode_valid(regulators_node)) { 130 debug("%s: %s regulators subnode not found!\n", __func__, 131 dev->name); 132 return -ENXIO; 133 } 134 135 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name); 136 137 children = pmic_bind_children(dev, regulators_node, pmic_children_info); 138 if (!children) 139 debug("%s: %s - no child found\n", __func__, dev->name); 140 141 children = pmic_bind_children(dev, dev->node, power_key_info); 142 if (!children) 143 debug("%s: %s - no child found\n", __func__, dev->name); 144 145 children = pmic_bind_children(dev, dev->node, fuel_gauge_info); 146 if (!children) 147 debug("%s: %s - no child found\n", __func__, dev->name); 148 149 /* Always return success for this device */ 150 return 0; 151 } 152 #endif 153 154 static int rk8xx_probe(struct udevice *dev) 155 { 156 struct rk8xx_priv *priv = dev_get_priv(dev); 157 struct reg_data *init_data = NULL; 158 int init_data_num = 0; 159 int ret = 0, i, show_variant; 160 uint8_t msb, lsb, id_msb, id_lsb; 161 uint8_t on_source = 0, off_source = 0; 162 163 /* read Chip variant */ 164 if (device_is_compatible(dev, "rockchip,rk817") || 165 device_is_compatible(dev, "rockchip,rk809")) { 166 id_msb = RK817_ID_MSB; 167 id_lsb = RK817_ID_LSB; 168 } else { 169 id_msb = ID_MSB; 170 id_lsb = ID_LSB; 171 } 172 173 ret = rk8xx_read(dev, id_msb, &msb, 1); 174 if (ret) 175 return ret; 176 ret = rk8xx_read(dev, id_lsb, &lsb, 1); 177 if (ret) 178 return ret; 179 180 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK; 181 show_variant = priv->variant; 182 switch (priv->variant) { 183 case RK808_ID: 184 show_variant = 0x808; /* RK808 hardware ID is 0 */ 185 break; 186 case RK805_ID: 187 case RK816_ID: 188 case RK818_ID: 189 on_source = RK8XX_ON_SOURCE; 190 off_source = RK8XX_OFF_SOURCE; 191 break; 192 case RK809_ID: 193 case RK817_ID: 194 on_source = RK817_ON_SOURCE; 195 off_source = RK817_OFF_SOURCE; 196 #ifdef CONFIG_DM_CHARGE_DISPLAY 197 init_data = rk817_init_reg; 198 init_data_num = ARRAY_SIZE(rk817_init_reg); 199 #endif 200 break; 201 default: 202 printf("Unknown PMIC: RK%x!!\n", priv->variant); 203 return -EINVAL; 204 } 205 206 for (i = 0; i < init_data_num; i++) { 207 ret = pmic_clrsetbits(dev, 208 init_data[i].reg, 209 init_data[i].mask, 210 init_data[i].val); 211 if (ret < 0) { 212 printf("%s: i2c set reg 0x%x failed, ret=%d\n", 213 __func__, init_data[i].reg, ret); 214 } 215 216 debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg, 217 pmic_reg_read(dev, init_data[i].reg)); 218 } 219 220 printf("PMIC: RK%x ", show_variant); 221 222 if (on_source && off_source) { 223 on_source = pmic_reg_read(dev, on_source); 224 off_source = pmic_reg_read(dev, off_source); 225 printf("(on=0x%02x, off=0x%02x)", 226 pmic_reg_read(dev, on_source), 227 pmic_reg_read(dev, off_source)); 228 } 229 printf("\n"); 230 231 return 0; 232 } 233 234 static struct dm_pmic_ops rk8xx_ops = { 235 .reg_count = rk8xx_reg_count, 236 .read = rk8xx_read, 237 .write = rk8xx_write, 238 .shutdown = rk8xx_shutdown, 239 }; 240 241 static const struct udevice_id rk8xx_ids[] = { 242 { .compatible = "rockchip,rk805" }, 243 { .compatible = "rockchip,rk808" }, 244 { .compatible = "rockchip,rk809" }, 245 { .compatible = "rockchip,rk816" }, 246 { .compatible = "rockchip,rk817" }, 247 { .compatible = "rockchip,rk818" }, 248 { } 249 }; 250 251 U_BOOT_DRIVER(pmic_rk8xx) = { 252 .name = "rk8xx pmic", 253 .id = UCLASS_PMIC, 254 .of_match = rk8xx_ids, 255 #if CONFIG_IS_ENABLED(PMIC_CHILDREN) 256 .bind = rk8xx_bind, 257 #endif 258 .priv_auto_alloc_size = sizeof(struct rk8xx_priv), 259 .probe = rk8xx_probe, 260 .ops = &rk8xx_ops, 261 }; 262