Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * Based on Rockchip's drivers/power/pmic/pmic_rk808.c: |
| 6 | * Copyright (C) 2012 rockchips |
| 7 | * zyw <zyw@rock-chips.com> |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <dm.h> |
| 14 | #include <errno.h> |
| 15 | #include <power/rk808_pmic.h> |
| 16 | #include <power/pmic.h> |
| 17 | #include <power/regulator.h> |
| 18 | |
| 19 | #ifndef CONFIG_SPL_BUILD |
| 20 | #define ENABLE_DRIVER |
| 21 | #endif |
| 22 | |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 23 | /* Field Definitions */ |
| 24 | #define RK808_BUCK_VSEL_MASK 0x3f |
| 25 | #define RK808_BUCK4_VSEL_MASK 0xf |
| 26 | #define RK808_LDO_VSEL_MASK 0x1f |
| 27 | |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 28 | struct rk808_reg_info { |
| 29 | uint min_uv; |
| 30 | uint step_uv; |
| 31 | s8 vsel_reg; |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 32 | u8 vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static const struct rk808_reg_info rk808_buck[] = { |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 36 | { 712500, 12500, REG_BUCK1_ON_VSEL, RK808_BUCK_VSEL_MASK, }, |
| 37 | { 712500, 12500, REG_BUCK2_ON_VSEL, RK808_BUCK_VSEL_MASK, }, |
| 38 | { 712500, 12500, -1, RK808_BUCK_VSEL_MASK, }, |
| 39 | { 1800000, 100000, REG_BUCK4_ON_VSEL, RK808_BUCK4_VSEL_MASK, }, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static const struct rk808_reg_info rk808_ldo[] = { |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 43 | { 1800000, 100000, REG_LDO1_ON_VSEL, RK808_LDO_VSEL_MASK, }, |
| 44 | { 1800000, 100000, REG_LDO2_ON_VSEL, RK808_LDO_VSEL_MASK, }, |
| 45 | { 800000, 100000, REG_LDO3_ON_VSEL, RK808_BUCK4_VSEL_MASK, }, |
| 46 | { 1800000, 100000, REG_LDO4_ON_VSEL, RK808_LDO_VSEL_MASK, }, |
| 47 | { 1800000, 100000, REG_LDO5_ON_VSEL, RK808_LDO_VSEL_MASK, }, |
| 48 | { 800000, 100000, REG_LDO6_ON_VSEL, RK808_LDO_VSEL_MASK, }, |
| 49 | { 800000, 100000, REG_LDO7_ON_VSEL, RK808_LDO_VSEL_MASK, }, |
| 50 | { 1800000, 100000, REG_LDO8_ON_VSEL, RK808_LDO_VSEL_MASK, }, |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | |
| 54 | static int _buck_set_value(struct udevice *pmic, int buck, int uvolt) |
| 55 | { |
| 56 | const struct rk808_reg_info *info = &rk808_buck[buck - 1]; |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 57 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 58 | int val; |
| 59 | |
| 60 | if (info->vsel_reg == -1) |
| 61 | return -ENOSYS; |
| 62 | val = (uvolt - info->min_uv) / info->step_uv; |
| 63 | debug("%s: reg=%x, mask=%x, val=%x\n", __func__, info->vsel_reg, mask, |
| 64 | val); |
| 65 | |
| 66 | return pmic_clrsetbits(pmic, info->vsel_reg, mask, val); |
| 67 | } |
| 68 | |
| 69 | static int _buck_set_enable(struct udevice *pmic, int buck, bool enable) |
| 70 | { |
| 71 | uint mask; |
| 72 | int ret; |
| 73 | |
| 74 | buck--; |
| 75 | mask = 1 << buck; |
| 76 | if (enable) { |
Jacob Chen | 0237cfd | 2017-05-02 14:54:48 +0800 | [diff] [blame] | 77 | ret = pmic_clrsetbits(pmic, REG_DCDC_ILMAX, 0, 3 << (buck * 2)); |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 78 | if (ret) |
| 79 | return ret; |
| 80 | ret = pmic_clrsetbits(pmic, REG_DCDC_UV_ACT, 1 << buck, 0); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | return pmic_clrsetbits(pmic, REG_DCDC_EN, mask, enable ? mask : 0); |
| 86 | } |
| 87 | |
| 88 | #ifdef ENABLE_DRIVER |
| 89 | static int buck_get_value(struct udevice *dev) |
| 90 | { |
| 91 | int buck = dev->driver_data - 1; |
| 92 | const struct rk808_reg_info *info = &rk808_buck[buck]; |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 93 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 94 | int ret, val; |
| 95 | |
| 96 | if (info->vsel_reg == -1) |
| 97 | return -ENOSYS; |
| 98 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 99 | if (ret < 0) |
| 100 | return ret; |
| 101 | val = ret & mask; |
| 102 | |
| 103 | return info->min_uv + val * info->step_uv; |
| 104 | } |
| 105 | |
| 106 | static int buck_set_value(struct udevice *dev, int uvolt) |
| 107 | { |
| 108 | int buck = dev->driver_data; |
| 109 | |
| 110 | return _buck_set_value(dev->parent, buck, uvolt); |
| 111 | } |
| 112 | |
| 113 | static int buck_set_enable(struct udevice *dev, bool enable) |
| 114 | { |
| 115 | int buck = dev->driver_data; |
| 116 | |
| 117 | return _buck_set_enable(dev->parent, buck, enable); |
| 118 | } |
| 119 | |
| 120 | static bool buck_get_enable(struct udevice *dev) |
| 121 | { |
| 122 | int buck = dev->driver_data - 1; |
| 123 | int ret; |
| 124 | uint mask; |
| 125 | |
| 126 | mask = 1 << buck; |
| 127 | |
| 128 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | |
| 132 | return ret & mask ? true : false; |
| 133 | } |
| 134 | |
| 135 | static int ldo_get_value(struct udevice *dev) |
| 136 | { |
| 137 | int ldo = dev->driver_data - 1; |
| 138 | const struct rk808_reg_info *info = &rk808_ldo[ldo]; |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 139 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 140 | int ret, val; |
| 141 | |
| 142 | if (info->vsel_reg == -1) |
| 143 | return -ENOSYS; |
| 144 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 145 | if (ret < 0) |
| 146 | return ret; |
| 147 | val = ret & mask; |
| 148 | |
| 149 | return info->min_uv + val * info->step_uv; |
| 150 | } |
| 151 | |
| 152 | static int ldo_set_value(struct udevice *dev, int uvolt) |
| 153 | { |
| 154 | int ldo = dev->driver_data - 1; |
| 155 | const struct rk808_reg_info *info = &rk808_ldo[ldo]; |
Jacob Chen | 21a6a25 | 2017-05-02 14:54:50 +0800 | [diff] [blame^] | 156 | int mask = info->vsel_mask; |
Simon Glass | adecfef | 2016-01-21 19:43:30 -0700 | [diff] [blame] | 157 | int val; |
| 158 | |
| 159 | if (info->vsel_reg == -1) |
| 160 | return -ENOSYS; |
| 161 | val = (uvolt - info->min_uv) / info->step_uv; |
| 162 | debug("%s: reg=%x, mask=%x, val=%x\n", __func__, info->vsel_reg, mask, |
| 163 | val); |
| 164 | |
| 165 | return pmic_clrsetbits(dev->parent, info->vsel_reg, mask, val); |
| 166 | } |
| 167 | |
| 168 | static int ldo_set_enable(struct udevice *dev, bool enable) |
| 169 | { |
| 170 | int ldo = dev->driver_data - 1; |
| 171 | uint mask; |
| 172 | |
| 173 | mask = 1 << ldo; |
| 174 | |
| 175 | return pmic_clrsetbits(dev->parent, REG_LDO_EN, mask, |
| 176 | enable ? mask : 0); |
| 177 | } |
| 178 | |
| 179 | static bool ldo_get_enable(struct udevice *dev) |
| 180 | { |
| 181 | int ldo = dev->driver_data - 1; |
| 182 | int ret; |
| 183 | uint mask; |
| 184 | |
| 185 | mask = 1 << ldo; |
| 186 | |
| 187 | ret = pmic_reg_read(dev->parent, REG_LDO_EN); |
| 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | |
| 191 | return ret & mask ? true : false; |
| 192 | } |
| 193 | |
| 194 | static int switch_set_enable(struct udevice *dev, bool enable) |
| 195 | { |
| 196 | int sw = dev->driver_data - 1; |
| 197 | uint mask; |
| 198 | |
| 199 | mask = 1 << (sw + 5); |
| 200 | |
| 201 | return pmic_clrsetbits(dev->parent, REG_DCDC_EN, mask, |
| 202 | enable ? mask : 0); |
| 203 | } |
| 204 | |
| 205 | static bool switch_get_enable(struct udevice *dev) |
| 206 | { |
| 207 | int sw = dev->driver_data - 1; |
| 208 | int ret; |
| 209 | uint mask; |
| 210 | |
| 211 | mask = 1 << (sw + 5); |
| 212 | |
| 213 | ret = pmic_reg_read(dev->parent, REG_DCDC_EN); |
| 214 | if (ret < 0) |
| 215 | return ret; |
| 216 | |
| 217 | return ret & mask ? true : false; |
| 218 | } |
| 219 | |
| 220 | static int rk808_buck_probe(struct udevice *dev) |
| 221 | { |
| 222 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 223 | |
| 224 | uc_pdata = dev_get_uclass_platdata(dev); |
| 225 | |
| 226 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 227 | uc_pdata->mode_count = 0; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int rk808_ldo_probe(struct udevice *dev) |
| 233 | { |
| 234 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 235 | |
| 236 | uc_pdata = dev_get_uclass_platdata(dev); |
| 237 | |
| 238 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 239 | uc_pdata->mode_count = 0; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int rk808_switch_probe(struct udevice *dev) |
| 245 | { |
| 246 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 247 | |
| 248 | uc_pdata = dev_get_uclass_platdata(dev); |
| 249 | |
| 250 | uc_pdata->type = REGULATOR_TYPE_FIXED; |
| 251 | uc_pdata->mode_count = 0; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static const struct dm_regulator_ops rk808_buck_ops = { |
| 257 | .get_value = buck_get_value, |
| 258 | .set_value = buck_set_value, |
| 259 | .get_enable = buck_get_enable, |
| 260 | .set_enable = buck_set_enable, |
| 261 | }; |
| 262 | |
| 263 | static const struct dm_regulator_ops rk808_ldo_ops = { |
| 264 | .get_value = ldo_get_value, |
| 265 | .set_value = ldo_set_value, |
| 266 | .get_enable = ldo_get_enable, |
| 267 | .set_enable = ldo_set_enable, |
| 268 | }; |
| 269 | |
| 270 | static const struct dm_regulator_ops rk808_switch_ops = { |
| 271 | .get_enable = switch_get_enable, |
| 272 | .set_enable = switch_set_enable, |
| 273 | }; |
| 274 | |
| 275 | U_BOOT_DRIVER(rk808_buck) = { |
| 276 | .name = "rk808_buck", |
| 277 | .id = UCLASS_REGULATOR, |
| 278 | .ops = &rk808_buck_ops, |
| 279 | .probe = rk808_buck_probe, |
| 280 | }; |
| 281 | |
| 282 | U_BOOT_DRIVER(rk808_ldo) = { |
| 283 | .name = "rk808_ldo", |
| 284 | .id = UCLASS_REGULATOR, |
| 285 | .ops = &rk808_ldo_ops, |
| 286 | .probe = rk808_ldo_probe, |
| 287 | }; |
| 288 | |
| 289 | U_BOOT_DRIVER(rk808_switch) = { |
| 290 | .name = "rk808_switch", |
| 291 | .id = UCLASS_REGULATOR, |
| 292 | .ops = &rk808_switch_ops, |
| 293 | .probe = rk808_switch_probe, |
| 294 | }; |
| 295 | #endif |
| 296 | |
| 297 | int rk808_spl_configure_buck(struct udevice *pmic, int buck, int uvolt) |
| 298 | { |
| 299 | int ret; |
| 300 | |
| 301 | ret = _buck_set_value(pmic, buck, uvolt); |
| 302 | if (ret) |
| 303 | return ret; |
| 304 | |
| 305 | return _buck_set_enable(pmic, buck, true); |
| 306 | } |