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> |
| 9 | #include <errno.h> |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 10 | #include <power/rk8xx_pmic.h> |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 11 | #include <power/pmic.h> |
| 12 | |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 13 | static struct reg_data rk817_init_reg[] = { |
| 14 | /* enable the under-voltage protection, |
| 15 | * the under-voltage protection will shutdown the LDO3 and reset the PMIC |
| 16 | */ |
| 17 | { RK817_BUCK4_CMIN, 0x60, 0x60}, |
| 18 | }; |
| 19 | |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 20 | static const struct pmic_child_info pmic_children_info[] = { |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 21 | { .prefix = "DCDC_REG", .driver = "rk8xx_buck"}, |
| 22 | { .prefix = "LDO_REG", .driver = "rk8xx_ldo"}, |
| 23 | { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"}, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 24 | { }, |
| 25 | }; |
| 26 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 27 | static int rk8xx_reg_count(struct udevice *dev) |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 28 | { |
| 29 | return RK808_NUM_OF_REGS; |
| 30 | } |
| 31 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 32 | 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] | 33 | int len) |
| 34 | { |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 35 | int ret; |
| 36 | |
| 37 | ret = dm_i2c_write(dev, reg, buff, len); |
| 38 | if (ret) { |
Simon Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 39 | debug("write error to device: %p register: %#x!\n", dev, reg); |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 40 | return ret; |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 46 | 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] | 47 | { |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 48 | int ret; |
| 49 | |
| 50 | ret = dm_i2c_read(dev, reg, buff, len); |
| 51 | if (ret) { |
Simon Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 52 | debug("read error from device: %p register: %#x!\n", dev, reg); |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 53 | return ret; |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 60 | static int rk8xx_bind(struct udevice *dev) |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 61 | { |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 62 | ofnode regulators_node; |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 63 | int children; |
| 64 | |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 65 | regulators_node = dev_read_subnode(dev, "regulators"); |
| 66 | if (!ofnode_valid(regulators_node)) { |
Simon Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 67 | debug("%s: %s regulators subnode not found!\n", __func__, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 68 | dev->name); |
| 69 | return -ENXIO; |
| 70 | } |
| 71 | |
| 72 | debug("%s: '%s' - found regulators subnode\n", __func__, dev->name); |
| 73 | |
| 74 | children = pmic_bind_children(dev, regulators_node, pmic_children_info); |
| 75 | if (!children) |
| 76 | debug("%s: %s - no child found\n", __func__, dev->name); |
| 77 | |
| 78 | /* Always return success for this device */ |
| 79 | return 0; |
| 80 | } |
| 81 | #endif |
| 82 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 83 | static int rk8xx_probe(struct udevice *dev) |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 84 | { |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 85 | struct rk8xx_priv *priv = dev_get_priv(dev); |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 86 | struct reg_data *init_data = NULL; |
| 87 | int init_data_num = 0; |
| 88 | int ret = 0, i, show_variant; |
| 89 | u8 msb, lsb, id_msb, id_lsb; |
| 90 | u8 on_source = 0, off_source = 0; |
| 91 | u8 power_en0, power_en1, power_en2, power_en3; |
| 92 | u8 value; |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 93 | |
| 94 | /* read Chip variant */ |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 95 | if (device_is_compatible(dev, "rockchip,rk817") || |
| 96 | device_is_compatible(dev, "rockchip,rk809")) { |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 97 | id_msb = RK817_ID_MSB; |
| 98 | id_lsb = RK817_ID_LSB; |
| 99 | } else { |
| 100 | id_msb = ID_MSB; |
| 101 | id_lsb = ID_LSB; |
| 102 | } |
| 103 | |
| 104 | ret = rk8xx_read(dev, id_msb, &msb, 1); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | ret = rk8xx_read(dev, id_lsb, &lsb, 1); |
| 108 | if (ret) |
| 109 | return ret; |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 110 | |
| 111 | priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK; |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 112 | show_variant = priv->variant; |
| 113 | switch (priv->variant) { |
| 114 | case RK808_ID: |
| 115 | show_variant = 0x808; /* RK808 hardware ID is 0 */ |
| 116 | break; |
| 117 | case RK805_ID: |
| 118 | case RK816_ID: |
| 119 | case RK818_ID: |
| 120 | on_source = RK8XX_ON_SOURCE; |
| 121 | off_source = RK8XX_OFF_SOURCE; |
| 122 | break; |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 123 | case RK809_ID: |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 124 | case RK817_ID: |
| 125 | on_source = RK817_ON_SOURCE; |
| 126 | off_source = RK817_OFF_SOURCE; |
| 127 | init_data = rk817_init_reg; |
| 128 | init_data_num = ARRAY_SIZE(rk817_init_reg); |
| 129 | power_en0 = pmic_reg_read(dev, RK817_POWER_EN0); |
| 130 | power_en1 = pmic_reg_read(dev, RK817_POWER_EN1); |
| 131 | power_en2 = pmic_reg_read(dev, RK817_POWER_EN2); |
| 132 | power_en3 = pmic_reg_read(dev, RK817_POWER_EN3); |
| 133 | |
| 134 | value = (power_en0 & 0x0f) | ((power_en1 & 0x0f) << 4); |
| 135 | pmic_reg_write(dev, RK817_POWER_EN_SAVE0, value); |
| 136 | value = (power_en2 & 0x0f) | ((power_en3 & 0x0f) << 4); |
| 137 | pmic_reg_write(dev, RK817_POWER_EN_SAVE1, value); |
| 138 | break; |
| 139 | default: |
| 140 | printf("Unknown PMIC: RK%x!!\n", priv->variant); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | for (i = 0; i < init_data_num; i++) { |
| 145 | ret = pmic_clrsetbits(dev, |
| 146 | init_data[i].reg, |
| 147 | init_data[i].mask, |
| 148 | init_data[i].val); |
| 149 | if (ret < 0) { |
| 150 | printf("%s: i2c set reg 0x%x failed, ret=%d\n", |
| 151 | __func__, init_data[i].reg, ret); |
| 152 | } |
| 153 | |
| 154 | debug("%s: reg[0x%x] = 0x%x\n", __func__, init_data[i].reg, |
| 155 | pmic_reg_read(dev, init_data[i].reg)); |
| 156 | } |
| 157 | |
| 158 | printf("PMIC: RK%x ", show_variant); |
| 159 | |
| 160 | if (on_source && off_source) |
| 161 | printf("(on=0x%02x, off=0x%02x)", |
| 162 | pmic_reg_read(dev, on_source), |
| 163 | pmic_reg_read(dev, off_source)); |
| 164 | printf("\n"); |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 169 | static struct dm_pmic_ops rk8xx_ops = { |
| 170 | .reg_count = rk8xx_reg_count, |
| 171 | .read = rk8xx_read, |
| 172 | .write = rk8xx_write, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 175 | static const struct udevice_id rk8xx_ids[] = { |
Elaine Zhang | 1d9077e | 2019-09-26 15:43:55 +0800 | [diff] [blame] | 176 | { .compatible = "rockchip,rk805" }, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 177 | { .compatible = "rockchip,rk808" }, |
Joseph Chen | d6b3e83 | 2019-09-26 15:45:07 +0800 | [diff] [blame] | 178 | { .compatible = "rockchip,rk809" }, |
Elaine Zhang | 04e5a43 | 2019-09-26 15:43:54 +0800 | [diff] [blame] | 179 | { .compatible = "rockchip,rk816" }, |
Joseph Chen | 4a1ae18 | 2019-09-26 15:44:55 +0800 | [diff] [blame] | 180 | { .compatible = "rockchip,rk817" }, |
Jacob Chen | 0234adb | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 181 | { .compatible = "rockchip,rk818" }, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 182 | { } |
| 183 | }; |
| 184 | |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 185 | U_BOOT_DRIVER(pmic_rk8xx) = { |
| 186 | .name = "rk8xx pmic", |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 187 | .id = UCLASS_PMIC, |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 188 | .of_match = rk8xx_ids, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 189 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 190 | .bind = rk8xx_bind, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 191 | #endif |
Kever Yang | 00ab7fb | 2017-05-05 14:50:56 +0800 | [diff] [blame] | 192 | .priv_auto_alloc_size = sizeof(struct rk8xx_priv), |
Jacob Chen | 614704b | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 193 | .probe = rk8xx_probe, |
| 194 | .ops = &rk8xx_ops, |
Simon Glass | a8bd2ac | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 195 | }; |