Martin Fuzzey | d309f62 | 2020-01-14 15:56:17 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Flowbird |
| 4 | * Martin Fuzzey <martin.fuzzey@flowbird.group> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <power/da9063_pmic.h> |
| 10 | #include <power/pmic.h> |
| 11 | #include <power/regulator.h> |
| 12 | |
| 13 | #define DA9063_BUCK_EN 0x01 |
| 14 | #define DA9063_LDO_EN 0x01 |
| 15 | #define DA9063_VBUCK_MASK 0x7F |
| 16 | #define DA9063_BUCK_SL 0x80 |
| 17 | #define DA9063_LDO_SL 0x80 |
| 18 | |
| 19 | #define DA9063_VLDO1_MASK 0x3F |
| 20 | #define DA9063_VLDO2_MASK 0x3F |
| 21 | #define DA9063_VLDO3_MASK 0x7F |
| 22 | #define DA9063_VLDO4_MASK 0x7F |
| 23 | #define DA9063_VLDO5_MASK 0x3F |
| 24 | #define DA9063_VLDO6_MASK 0x3F |
| 25 | #define DA9063_VLDO7_MASK 0x3F |
| 26 | #define DA9063_VLDO8_MASK 0x3F |
| 27 | #define DA9063_VLDO9_MASK 0x3F |
| 28 | #define DA9063_VLDO10_MASK 0x3F |
| 29 | #define DA9063_VLDO11_MASK 0x3F |
| 30 | |
| 31 | #define DA9063_BUCK_MODE_MASK 0xC0 |
| 32 | #define DA9063_BUCK_MODE_MANUAL 0x00 |
| 33 | #define DA9063_BUCK_MODE_SLEEP 0x40 |
| 34 | #define DA9063_BUCK_MODE_SYNC 0x80 |
| 35 | #define DA9063_BUCK_MODE_AUTO 0xC0 |
| 36 | |
| 37 | #define DA9063_BIO_ILIM_MASK 0x0F |
| 38 | #define DA9063_BMEM_ILIM_MASK 0xF0 |
| 39 | #define DA9063_BPRO_ILIM_MASK 0x0F |
| 40 | #define DA9063_BPERI_ILIM_MASK 0xF0 |
| 41 | #define DA9063_BCORE1_ILIM_MASK 0x0F |
| 42 | #define DA9063_BCORE2_ILIM_MASK 0xF0 |
| 43 | |
| 44 | struct da9063_reg_info { |
| 45 | uint min_uV; |
| 46 | uint step_uV; |
| 47 | uint max_uV; |
| 48 | uint min_uA; |
| 49 | uint step_uA; |
| 50 | uint max_uA; |
| 51 | uint en_reg; |
| 52 | uint vsel_reg; |
| 53 | uint mode_reg; |
| 54 | uint ilim_reg; |
| 55 | u8 en_mask; |
| 56 | u8 vsel_mask; |
| 57 | u8 ilim_mask; |
| 58 | const char *dt_node_name; |
| 59 | const int *current_limits; |
| 60 | }; |
| 61 | |
| 62 | struct da9063_priv { |
| 63 | const struct da9063_reg_info *reg_info; |
| 64 | }; |
| 65 | |
| 66 | static struct dm_regulator_mode da9063_ldo_modes[] = { |
| 67 | { .id = DA9063_LDOMODE_SLEEP, |
| 68 | .register_value = DA9063_LDO_SL, .name = "SLEEP" }, |
| 69 | { .id = DA9063_LDOMODE_NORMAL, |
| 70 | .register_value = 0, .name = "NORMAL" }, |
| 71 | }; |
| 72 | |
| 73 | #define DA9063_LDO(regl_name, min_mV, step_mV, max_mV) \ |
| 74 | .min_uV = (min_mV) * 1000, \ |
| 75 | .step_uV = (step_mV) * 1000, \ |
| 76 | .max_uV = (max_mV) * 1000, \ |
| 77 | .en_reg = DA9063_REG_##regl_name##_CONT, \ |
| 78 | .en_mask = DA9063_LDO_EN, \ |
| 79 | .vsel_reg = DA9063_REG_V##regl_name##_A, \ |
| 80 | .vsel_mask = DA9063_V##regl_name##_MASK, \ |
| 81 | .mode_reg = DA9063_REG_V##regl_name##_A \ |
| 82 | |
| 83 | /* This array is directly indexed so must stay in numerical order */ |
| 84 | static const struct da9063_reg_info da9063_ldo_info[] = { |
| 85 | { DA9063_LDO(LDO1, 600, 20, 1860) }, |
| 86 | { DA9063_LDO(LDO2, 600, 20, 1860) }, |
| 87 | { DA9063_LDO(LDO3, 900, 20, 3440) }, |
| 88 | { DA9063_LDO(LDO4, 900, 20, 3440) }, |
| 89 | { DA9063_LDO(LDO5, 900, 50, 3600) }, |
| 90 | { DA9063_LDO(LDO6, 900, 50, 3600) }, |
| 91 | { DA9063_LDO(LDO7, 900, 50, 3600) }, |
| 92 | { DA9063_LDO(LDO8, 900, 50, 3600) }, |
| 93 | { DA9063_LDO(LDO9, 950, 50, 3600) }, |
| 94 | { DA9063_LDO(LDO10, 900, 50, 3600) }, |
| 95 | { DA9063_LDO(LDO11, 900, 50, 3600) }, |
| 96 | }; |
| 97 | |
| 98 | static struct dm_regulator_mode da9063_buck_modes[] = { |
| 99 | { .id = DA9063_BUCKMODE_SLEEP, |
| 100 | .register_value = DA9063_BUCK_MODE_SLEEP, .name = "SLEEP" }, |
| 101 | { .id = DA9063_BUCKMODE_SYNC, |
| 102 | .register_value = DA9063_BUCK_MODE_SYNC, .name = "SYNC" }, |
| 103 | { .id = DA9063_BUCKMODE_AUTO, |
| 104 | .register_value = DA9063_BUCK_MODE_AUTO, .name = "AUTO" }, |
| 105 | }; |
| 106 | |
| 107 | #define DA9063_BUCK(regl_name, dt_name, \ |
| 108 | min_mV, step_mV, max_mV, \ |
| 109 | min_mA, step_mA, max_mA, _ilim_reg) \ |
| 110 | .dt_node_name = dt_name, \ |
| 111 | .min_uV = (min_mV) * 1000, \ |
| 112 | .step_uV = (step_mV) * 1000, \ |
| 113 | .max_uV = (max_mV) * 1000, \ |
| 114 | .min_uA = (min_mA) * 1000, \ |
| 115 | .step_uA = (step_mA) * 1000, \ |
| 116 | .max_uA = (max_mA) * 1000, \ |
| 117 | .en_reg = DA9063_REG_##regl_name##_CONT, \ |
| 118 | .en_mask = DA9063_BUCK_EN, \ |
| 119 | .vsel_reg = DA9063_REG_V##regl_name##_A, \ |
| 120 | .vsel_mask = DA9063_VBUCK_MASK, \ |
| 121 | .mode_reg = DA9063_REG_##regl_name##_CFG, \ |
| 122 | .ilim_reg = DA9063_REG_BUCK_ILIM_##_ilim_reg, \ |
| 123 | .ilim_mask = DA9063_##regl_name##_ILIM_MASK |
| 124 | |
| 125 | static const struct da9063_reg_info da9063_buck_info[] = { |
| 126 | /* mV mA */ |
| 127 | { DA9063_BUCK(BCORE1, "bcore1", 300, 10, 1570, 500, 100, 2000, C) }, |
| 128 | { DA9063_BUCK(BCORE2, "bcore2", 300, 10, 1570, 500, 100, 2000, C) }, |
| 129 | { DA9063_BUCK(BPRO, "bpro", 530, 10, 1800, 500, 100, 2000, B) }, |
| 130 | { DA9063_BUCK(BMEM, "bmem", 800, 20, 3340, 1500, 100, 3000, A) }, |
| 131 | { DA9063_BUCK(BIO, "bio", 800, 20, 3340, 1500, 100, 3000, A) }, |
| 132 | { DA9063_BUCK(BPERI, "bperi", 800, 20, 3340, 1500, 100, 3000, B) }, |
| 133 | }; |
| 134 | |
| 135 | static int da9063_get_enable(struct udevice *dev) |
| 136 | { |
| 137 | const struct da9063_priv *priv = dev->priv; |
| 138 | const struct da9063_reg_info *info = priv->reg_info; |
| 139 | int ret; |
| 140 | |
| 141 | ret = pmic_reg_read(dev->parent, info->en_reg); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
| 145 | return ret & info->en_mask ? true : false; |
| 146 | } |
| 147 | |
| 148 | static int da9063_set_enable(struct udevice *dev, bool enable) |
| 149 | { |
| 150 | const struct da9063_priv *priv = dev->priv; |
| 151 | const struct da9063_reg_info *info = priv->reg_info; |
| 152 | |
| 153 | return pmic_clrsetbits(dev->parent, info->en_reg, |
| 154 | info->en_mask, enable ? info->en_mask : 0); |
| 155 | } |
| 156 | |
| 157 | static int da9063_get_voltage(struct udevice *dev) |
| 158 | { |
| 159 | const struct da9063_priv *priv = dev->priv; |
| 160 | const struct da9063_reg_info *info = priv->reg_info; |
| 161 | int ret; |
| 162 | |
| 163 | ret = pmic_reg_read(dev->parent, info->vsel_reg); |
| 164 | if (ret < 0) |
| 165 | return ret; |
| 166 | |
| 167 | return info->min_uV + (ret & info->vsel_mask) * info->step_uV; |
| 168 | } |
| 169 | |
| 170 | static int da9063_set_voltage(struct udevice *dev, int uV) |
| 171 | { |
| 172 | const struct da9063_priv *priv = dev->priv; |
| 173 | const struct da9063_reg_info *info = priv->reg_info; |
| 174 | uint sel; |
| 175 | |
| 176 | if (uV < info->min_uV || uV > info->max_uV) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | sel = (uV - info->min_uV) / info->step_uV; |
| 180 | |
| 181 | return pmic_clrsetbits(dev->parent, info->vsel_reg, |
| 182 | info->vsel_mask, sel); |
| 183 | } |
| 184 | |
| 185 | static const struct dm_regulator_mode |
| 186 | *da9063_find_mode_by_id(int id, |
| 187 | const struct dm_regulator_mode *modes, |
| 188 | uint mode_count) |
| 189 | { |
| 190 | for (; mode_count; mode_count--) { |
| 191 | if (modes->id == id) |
| 192 | return modes; |
| 193 | modes++; |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | static int ldo_get_mode(struct udevice *dev) |
| 199 | { |
| 200 | const struct da9063_priv *priv = dev->priv; |
| 201 | const struct da9063_reg_info *info = priv->reg_info; |
| 202 | int val; |
| 203 | |
| 204 | val = pmic_reg_read(dev->parent, info->mode_reg); |
| 205 | if (val < 0) |
| 206 | return val; |
| 207 | |
| 208 | if (val & DA9063_LDO_SL) |
| 209 | return DA9063_LDOMODE_SLEEP; |
| 210 | else |
| 211 | return DA9063_LDOMODE_NORMAL; |
| 212 | } |
| 213 | |
| 214 | static int ldo_set_mode(struct udevice *dev, int mode_id) |
| 215 | { |
| 216 | const struct da9063_priv *priv = dev->priv; |
| 217 | const struct da9063_reg_info *info = priv->reg_info; |
| 218 | const struct dm_regulator_mode *mode; |
| 219 | |
| 220 | mode = da9063_find_mode_by_id(mode_id, |
| 221 | da9063_ldo_modes, |
| 222 | ARRAY_SIZE(da9063_ldo_modes)); |
| 223 | if (!mode) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | return pmic_clrsetbits(dev->parent, info->mode_reg, |
| 227 | DA9063_LDO_SL, mode->register_value); |
| 228 | } |
| 229 | |
| 230 | static int buck_get_mode(struct udevice *dev) |
| 231 | { |
| 232 | const struct da9063_priv *priv = dev->priv; |
| 233 | const struct da9063_reg_info *info = priv->reg_info; |
| 234 | int i; |
| 235 | int val; |
| 236 | |
| 237 | val = pmic_reg_read(dev->parent, info->mode_reg); |
| 238 | if (val < 0) |
| 239 | return val; |
| 240 | |
| 241 | val &= DA9063_BUCK_MODE_MASK; |
| 242 | if (val == DA9063_BUCK_MODE_MANUAL) { |
| 243 | val = pmic_reg_read(dev->parent, info->vsel_reg); |
| 244 | if (val < 0) |
| 245 | return val; |
| 246 | |
| 247 | if (val & DA9063_BUCK_SL) |
| 248 | return DA9063_BUCKMODE_SLEEP; |
| 249 | else |
| 250 | return DA9063_BUCKMODE_SYNC; |
| 251 | } |
| 252 | |
| 253 | for (i = 0; i < ARRAY_SIZE(da9063_buck_modes); i++) { |
| 254 | if (da9063_buck_modes[i].register_value == val) |
| 255 | return da9063_buck_modes[i].id; |
| 256 | } |
| 257 | |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | static int buck_set_mode(struct udevice *dev, int mode_id) |
| 262 | { |
| 263 | const struct da9063_priv *priv = dev->priv; |
| 264 | const struct da9063_reg_info *info = priv->reg_info; |
| 265 | const struct dm_regulator_mode *mode; |
| 266 | |
| 267 | mode = da9063_find_mode_by_id(mode_id, |
| 268 | da9063_buck_modes, |
| 269 | ARRAY_SIZE(da9063_buck_modes)); |
| 270 | if (!mode) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | return pmic_clrsetbits(dev->parent, info->mode_reg, |
| 274 | DA9063_BUCK_MODE_MASK, mode->register_value); |
| 275 | } |
| 276 | |
| 277 | static int buck_get_current_limit(struct udevice *dev) |
| 278 | { |
| 279 | const struct da9063_priv *priv = dev->priv; |
| 280 | const struct da9063_reg_info *info = priv->reg_info; |
| 281 | int val; |
| 282 | |
| 283 | val = pmic_reg_read(dev->parent, info->ilim_reg); |
| 284 | if (val < 0) |
| 285 | return val; |
| 286 | |
| 287 | val &= info->ilim_mask; |
| 288 | val >>= (ffs(info->ilim_mask) - 1); |
| 289 | |
| 290 | return info->min_uA + val * info->step_uA; |
| 291 | } |
| 292 | |
| 293 | static int buck_set_current_limit(struct udevice *dev, int uA) |
| 294 | { |
| 295 | const struct da9063_priv *priv = dev->priv; |
| 296 | const struct da9063_reg_info *info = priv->reg_info; |
| 297 | int val; |
| 298 | |
| 299 | if (uA < info->min_uA || uA > info->max_uA) |
| 300 | return -EINVAL; |
| 301 | |
| 302 | val = (uA - info->min_uA) / info->step_uA; |
| 303 | val <<= (ffs(info->ilim_mask) - 1); |
| 304 | |
| 305 | return pmic_clrsetbits(dev->parent, info->ilim_reg, |
| 306 | info->ilim_mask, val); |
| 307 | } |
| 308 | |
| 309 | static int da9063_ldo_probe(struct udevice *dev) |
| 310 | { |
| 311 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 312 | struct da9063_priv *priv = dev->priv; |
| 313 | |
| 314 | /* LDOs are named numerically in DT so can directly index */ |
| 315 | if (dev->driver_data < 1 || |
| 316 | dev->driver_data > ARRAY_SIZE(da9063_ldo_info)) |
| 317 | return -EINVAL; |
| 318 | priv->reg_info = &da9063_ldo_info[dev->driver_data - 1]; |
| 319 | |
| 320 | uc_pdata = dev_get_uclass_platdata(dev); |
| 321 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 322 | uc_pdata->mode = da9063_ldo_modes; |
| 323 | uc_pdata->mode_count = ARRAY_SIZE(da9063_ldo_modes); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int da9063_buck_probe(struct udevice *dev) |
| 329 | { |
| 330 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 331 | struct da9063_priv *priv = dev->priv; |
| 332 | int i; |
| 333 | |
| 334 | /* Bucks have names rather than numbers so need to match with DT */ |
| 335 | for (i = 0; i < ARRAY_SIZE(da9063_buck_info); i++) { |
| 336 | const struct da9063_reg_info *info = &da9063_buck_info[i]; |
| 337 | |
| 338 | if (!strcmp(info->dt_node_name, dev->name)) { |
| 339 | priv->reg_info = info; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | if (!priv->reg_info) |
| 344 | return -ENODEV; |
| 345 | |
| 346 | uc_pdata = dev_get_uclass_platdata(dev); |
| 347 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 348 | uc_pdata->mode = da9063_buck_modes; |
| 349 | uc_pdata->mode_count = ARRAY_SIZE(da9063_buck_modes); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static const struct dm_regulator_ops da9063_ldo_ops = { |
| 355 | .get_value = da9063_get_voltage, |
| 356 | .set_value = da9063_set_voltage, |
| 357 | .get_enable = da9063_get_enable, |
| 358 | .set_enable = da9063_set_enable, |
| 359 | .get_mode = ldo_get_mode, |
| 360 | .set_mode = ldo_set_mode, |
| 361 | }; |
| 362 | |
| 363 | U_BOOT_DRIVER(da9063_ldo) = { |
| 364 | .name = DA9063_LDO_DRIVER, |
| 365 | .id = UCLASS_REGULATOR, |
| 366 | .ops = &da9063_ldo_ops, |
| 367 | .probe = da9063_ldo_probe, |
| 368 | .priv_auto_alloc_size = sizeof(struct da9063_priv), |
| 369 | }; |
| 370 | |
| 371 | static const struct dm_regulator_ops da9063_buck_ops = { |
| 372 | .get_value = da9063_get_voltage, |
| 373 | .set_value = da9063_set_voltage, |
| 374 | .get_enable = da9063_get_enable, |
| 375 | .set_enable = da9063_set_enable, |
| 376 | .get_mode = buck_get_mode, |
| 377 | .set_mode = buck_set_mode, |
| 378 | .get_current = buck_get_current_limit, |
| 379 | .set_current = buck_set_current_limit, |
| 380 | }; |
| 381 | |
| 382 | U_BOOT_DRIVER(da9063_buck) = { |
| 383 | .name = DA9063_BUCK_DRIVER, |
| 384 | .id = UCLASS_REGULATOR, |
| 385 | .ops = &da9063_buck_ops, |
| 386 | .probe = da9063_buck_probe, |
| 387 | .priv_auto_alloc_size = sizeof(struct da9063_priv), |
| 388 | }; |