Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Chris Morgan | b02712f | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 9 | #include <dm/lists.h> |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 12 | #include <power/rk8xx_pmic.h> |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 13 | #include <power/pmic.h> |
Chris Morgan | b02712f | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 14 | #include <sysreset.h> |
| 15 | |
| 16 | static int rk8xx_sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 17 | { |
| 18 | struct rk8xx_priv *priv = dev_get_priv(dev->parent); |
| 19 | |
| 20 | if (type != SYSRESET_POWER_OFF) |
| 21 | return -EPROTONOSUPPORT; |
| 22 | |
| 23 | switch (priv->variant) { |
| 24 | case RK805_ID: |
| 25 | case RK808_ID: |
| 26 | case RK816_ID: |
| 27 | case RK818_ID: |
| 28 | pmic_clrsetbits(dev->parent, REG_DEVCTRL, 0, BIT(0)); |
| 29 | break; |
| 30 | case RK809_ID: |
| 31 | case RK817_ID: |
| 32 | pmic_clrsetbits(dev->parent, RK817_REG_SYS_CFG3, 0, |
| 33 | BIT(0)); |
| 34 | break; |
| 35 | default: |
| 36 | printf("Unknown PMIC RK%x: Cannot shutdown\n", |
| 37 | priv->variant); |
| 38 | return -EPROTONOSUPPORT; |
| 39 | }; |
| 40 | |
| 41 | return -EINPROGRESS; |
| 42 | } |
| 43 | |
| 44 | static struct sysreset_ops rk8xx_sysreset_ops = { |
| 45 | .request = rk8xx_sysreset_request, |
| 46 | }; |
| 47 | |
| 48 | U_BOOT_DRIVER(rk8xx_sysreset) = { |
| 49 | .name = "rk8xx_sysreset", |
| 50 | .id = UCLASS_SYSRESET, |
| 51 | .ops = &rk8xx_sysreset_ops, |
| 52 | }; |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 53 | |
Chris Morgan | 7c9de74 | 2022-05-27 13:18:20 -0500 | [diff] [blame] | 54 | /* In the event of a plug-in and the appropriate option has been |
| 55 | * selected, we simply shutdown instead of continue the normal boot |
| 56 | * process. Please note the rk808 is not supported as it doesn't |
| 57 | * have the appropriate register. |
| 58 | */ |
| 59 | void rk8xx_off_for_plugin(struct udevice *dev) |
| 60 | { |
| 61 | struct rk8xx_priv *priv = dev_get_priv(dev); |
| 62 | |
| 63 | switch (priv->variant) { |
| 64 | case RK805_ID: |
| 65 | case RK816_ID: |
| 66 | case RK818_ID: |
| 67 | if (pmic_reg_read(dev, RK8XX_ON_SOURCE) & RK8XX_ON_PLUG_IN) { |
| 68 | printf("Power Off due to plug-in event\n"); |
| 69 | pmic_clrsetbits(dev, REG_DEVCTRL, 0, BIT(0)); |
| 70 | } |
| 71 | break; |
| 72 | case RK809_ID: |
| 73 | case RK817_ID: |
| 74 | if (pmic_reg_read(dev, RK817_ON_SOURCE) & RK8XX_ON_PLUG_IN) { |
| 75 | printf("Power Off due to plug-in event\n"); |
| 76 | pmic_clrsetbits(dev, RK817_REG_SYS_CFG3, 0, |
| 77 | BIT(0)); |
| 78 | } |
| 79 | break; |
| 80 | default: |
| 81 | printf("PMIC RK%x: Cannot read boot reason.\n", |
| 82 | priv->variant); |
| 83 | } |
| 84 | } |
| 85 | |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 86 | static struct reg_data rk817_init_reg[] = { |
| 87 | /* enable the under-voltage protection, |
| 88 | * the under-voltage protection will shutdown the LDO3 and reset the PMIC |
| 89 | */ |
| 90 | { RK817_BUCK4_CMIN, 0x60, 0x60}, |
| 91 | }; |
| 92 | |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 93 | static const struct pmic_child_info pmic_children_info[] = { |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 94 | { .prefix = "DCDC_REG", .driver = "rk8xx_buck"}, |
| 95 | { .prefix = "LDO_REG", .driver = "rk8xx_ldo"}, |
| 96 | { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"}, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 97 | { }, |
| 98 | }; |
| 99 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 100 | static int rk8xx_reg_count(struct udevice *dev) |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 101 | { |
| 102 | return RK808_NUM_OF_REGS; |
| 103 | } |
| 104 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 105 | static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 106 | int len) |
| 107 | { |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 108 | int ret; |
| 109 | |
| 110 | ret = dm_i2c_write(dev, reg, buff, len); |
| 111 | if (ret) { |
Simon Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 112 | debug("write error to device: %p register: %#x!\n", dev, reg); |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 113 | return ret; |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 119 | static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len) |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 120 | { |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 121 | int ret; |
| 122 | |
| 123 | ret = dm_i2c_read(dev, reg, buff, len); |
| 124 | if (ret) { |
Simon Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 125 | debug("read error from device: %p register: %#x!\n", dev, reg); |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 126 | return ret; |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 133 | static int rk8xx_bind(struct udevice *dev) |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 134 | { |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 135 | ofnode regulators_node; |
Chris Morgan | b02712f | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 136 | int children, ret; |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 137 | |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 138 | regulators_node = dev_read_subnode(dev, "regulators"); |
| 139 | if (!ofnode_valid(regulators_node)) { |
Simon Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 140 | debug("%s: %s regulators subnode not found!\n", __func__, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 141 | dev->name); |
| 142 | return -ENXIO; |
| 143 | } |
| 144 | |
| 145 | debug("%s: '%s' - found regulators subnode\n", __func__, dev->name); |
| 146 | |
Chris Morgan | b02712f | 2022-05-27 13:18:19 -0500 | [diff] [blame] | 147 | if (CONFIG_IS_ENABLED(SYSRESET)) { |
| 148 | ret = device_bind_driver_to_node(dev, "rk8xx_sysreset", |
| 149 | "rk8xx_sysreset", |
| 150 | dev_ofnode(dev), NULL); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | } |
| 154 | |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 155 | children = pmic_bind_children(dev, regulators_node, pmic_children_info); |
| 156 | if (!children) |
| 157 | debug("%s: %s - no child found\n", __func__, dev->name); |
| 158 | |
| 159 | /* Always return success for this device */ |
| 160 | return 0; |
| 161 | } |
| 162 | #endif |
| 163 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 164 | static int rk8xx_probe(struct udevice *dev) |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 165 | { |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 166 | struct rk8xx_priv *priv = dev_get_priv(dev); |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 167 | struct reg_data *init_data = NULL; |
| 168 | int init_data_num = 0; |
| 169 | int ret = 0, i, show_variant; |
| 170 | u8 msb, lsb, id_msb, id_lsb; |
| 171 | u8 on_source = 0, off_source = 0; |
| 172 | u8 power_en0, power_en1, power_en2, power_en3; |
| 173 | u8 value; |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 174 | |
| 175 | /* read Chip variant */ |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 176 | if (device_is_compatible(dev, "rockchip,rk817") || |
| 177 | device_is_compatible(dev, "rockchip,rk809")) { |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 178 | id_msb = RK817_ID_MSB; |
| 179 | id_lsb = RK817_ID_LSB; |
| 180 | } else { |
| 181 | id_msb = ID_MSB; |
| 182 | id_lsb = ID_LSB; |
| 183 | } |
| 184 | |
| 185 | ret = rk8xx_read(dev, id_msb, &msb, 1); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | ret = rk8xx_read(dev, id_lsb, &lsb, 1); |
| 189 | if (ret) |
| 190 | return ret; |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 191 | |
| 192 | priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 193 | show_variant = priv->variant; |
| 194 | switch (priv->variant) { |
| 195 | case RK808_ID: |
| 196 | show_variant = 0x808; /* RK808 hardware ID is 0 */ |
| 197 | break; |
| 198 | case RK805_ID: |
| 199 | case RK816_ID: |
| 200 | case RK818_ID: |
| 201 | on_source = RK8XX_ON_SOURCE; |
| 202 | off_source = RK8XX_OFF_SOURCE; |
| 203 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 204 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 205 | case RK817_ID: |
| 206 | on_source = RK817_ON_SOURCE; |
| 207 | off_source = RK817_OFF_SOURCE; |
| 208 | init_data = rk817_init_reg; |
| 209 | init_data_num = ARRAY_SIZE(rk817_init_reg); |
| 210 | power_en0 = pmic_reg_read(dev, RK817_POWER_EN0); |
| 211 | power_en1 = pmic_reg_read(dev, RK817_POWER_EN1); |
| 212 | power_en2 = pmic_reg_read(dev, RK817_POWER_EN2); |
| 213 | power_en3 = pmic_reg_read(dev, RK817_POWER_EN3); |
| 214 | |
| 215 | value = (power_en0 & 0x0f) | ((power_en1 & 0x0f) << 4); |
| 216 | pmic_reg_write(dev, RK817_POWER_EN_SAVE0, value); |
| 217 | value = (power_en2 & 0x0f) | ((power_en3 & 0x0f) << 4); |
| 218 | pmic_reg_write(dev, RK817_POWER_EN_SAVE1, value); |
| 219 | break; |
| 220 | default: |
| 221 | printf("Unknown PMIC: RK%x!!\n", priv->variant); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | for (i = 0; i < init_data_num; i++) { |
| 226 | ret = pmic_clrsetbits(dev, |
| 227 | init_data[i].reg, |
| 228 | init_data[i].mask, |
| 229 | init_data[i].val); |
| 230 | if (ret < 0) { |
| 231 | printf("%s: i2c set reg 0x%x failed, ret=%d\n", |
| 232 | __func__, init_data[i].reg, ret); |
| 233 | } |
| 234 | |
| 235 | debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg, |
| 236 | pmic_reg_read(dev, init_data[i].reg)); |
| 237 | } |
| 238 | |
| 239 | printf("PMIC: RK%x ", show_variant); |
| 240 | |
| 241 | if (on_source && off_source) |
| 242 | printf("(on=0x%02x, off=0x%02x)", |
| 243 | pmic_reg_read(dev, on_source), |
| 244 | pmic_reg_read(dev, off_source)); |
| 245 | printf("\n"); |
Chris Morgan | 7c9de74 | 2022-05-27 13:18:20 -0500 | [diff] [blame] | 246 | if (CONFIG_IS_ENABLED(ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON)) |
| 247 | rk8xx_off_for_plugin(dev); |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 252 | static struct dm_pmic_ops rk8xx_ops = { |
| 253 | .reg_count = rk8xx_reg_count, |
| 254 | .read = rk8xx_read, |
| 255 | .write = rk8xx_write, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 258 | static const struct udevice_id rk8xx_ids[] = { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 259 | { .compatible = "rockchip,rk805" }, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 260 | { .compatible = "rockchip,rk808" }, |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 261 | { .compatible = "rockchip,rk809" }, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 262 | { .compatible = "rockchip,rk816" }, |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 263 | { .compatible = "rockchip,rk817" }, |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 264 | { .compatible = "rockchip,rk818" }, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 265 | { } |
| 266 | }; |
| 267 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 268 | U_BOOT_DRIVER(rockchip_rk805) = { |
| 269 | .name = "rockchip_rk805", |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 270 | .id = UCLASS_PMIC, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 271 | .of_match = rk8xx_ids, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 272 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 273 | .bind = rk8xx_bind, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 274 | #endif |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 275 | .priv_auto = sizeof(struct rk8xx_priv), |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 276 | .probe = rk8xx_probe, |
| 277 | .ops = &rk8xx_ops, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 278 | }; |
Walter Lozano | 48e5b04 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 279 | |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 280 | DM_DRIVER_ALIAS(rockchip_rk805, rockchip_rk808) |