uboot/drivers/power/pmic/pmic_max77686.c
<<
>>
Prefs
   1/*
   2 *  Copyright (C) 2012 Samsung Electronics
   3 *  Rajeshwari Shinde <rajeshwari.s@samsung.com>
   4 *
   5 * SPDX-License-Identifier:     GPL-2.0+
   6 */
   7
   8#include <common.h>
   9#include <fdtdec.h>
  10#include <i2c.h>
  11#include <power/pmic.h>
  12#include <power/max77686_pmic.h>
  13#include <errno.h>
  14
  15DECLARE_GLOBAL_DATA_PTR;
  16
  17static const char max77686_buck_addr[] = {
  18        0xff, 0x10, 0x12, 0x1c, 0x26, 0x30, 0x32, 0x34, 0x36, 0x38
  19};
  20
  21static unsigned int max77686_ldo_volt2hex(int ldo, ulong uV)
  22{
  23        unsigned int hex = 0;
  24
  25        switch (ldo) {
  26        case 1:
  27        case 2:
  28        case 6:
  29        case 7:
  30        case 8:
  31        case 15:
  32                hex = (uV - 800000) / 25000;
  33                break;
  34        default:
  35                hex = (uV - 800000) / 50000;
  36        }
  37
  38        if (hex >= 0 && hex <= MAX77686_LDO_VOLT_MAX_HEX)
  39                return hex;
  40
  41        debug("%s: %ld is wrong voltage value for LDO%d\n", __func__, uV, ldo);
  42        return 0;
  43}
  44
  45static int max77686_buck_volt2hex(int buck, ulong uV)
  46{
  47        int hex = 0;
  48
  49        if (buck < 5 || buck > 9) {
  50                debug("%s: buck %d is not supported\n", __func__, buck);
  51                return -EINVAL;
  52        }
  53
  54        hex = (uV - 750000) / 50000;
  55
  56        if (hex >= 0 && hex <= MAX77686_BUCK_VOLT_MAX_HEX)
  57                return hex;
  58
  59        debug("%s: %ld is wrong voltage value for BUCK%d\n",
  60              __func__, uV, buck);
  61        return -EINVAL;
  62}
  63
  64int max77686_set_ldo_voltage(struct pmic *p, int ldo, ulong uV)
  65{
  66        unsigned int val, ret, hex, adr;
  67
  68        if (ldo < 1 || ldo > 26) {
  69                printf("%s: %d is wrong ldo number\n", __func__, ldo);
  70                return -EINVAL;
  71        }
  72
  73        adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
  74        hex = max77686_ldo_volt2hex(ldo, uV);
  75
  76        if (!hex)
  77                return -EINVAL;
  78
  79        ret = pmic_reg_read(p, adr, &val);
  80        if (ret)
  81                return ret;
  82
  83        val &= ~MAX77686_LDO_VOLT_MASK;
  84        val |= hex;
  85        ret |= pmic_reg_write(p, adr, val);
  86
  87        return ret;
  88}
  89
  90int max77686_set_buck_voltage(struct pmic *p, int buck, ulong uV)
  91{
  92        unsigned int val, adr;
  93        int hex, ret;
  94
  95        if (buck < 5 || buck > 9) {
  96                printf("%s: %d is an unsupported bucket number\n",
  97                       __func__, buck);
  98                return -EINVAL;
  99        }
 100
 101        adr = max77686_buck_addr[buck] + 1;
 102        hex = max77686_buck_volt2hex(buck, uV);
 103
 104        if (hex < 0)
 105                return hex;
 106
 107        ret = pmic_reg_read(p, adr, &val);
 108        if (ret)
 109                return ret;
 110
 111        val &= ~MAX77686_BUCK_VOLT_MASK;
 112        ret |= pmic_reg_write(p, adr, val | hex);
 113
 114        return ret;
 115}
 116
 117int max77686_set_ldo_mode(struct pmic *p, int ldo, char opmode)
 118{
 119        unsigned int val, ret, adr, mode;
 120
 121        if (ldo < 1 || 26 < ldo) {
 122                printf("%s: %d is wrong ldo number\n", __func__, ldo);
 123                return -EINVAL;
 124        }
 125
 126        adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
 127
 128        /* mode */
 129        switch (opmode) {
 130        case OPMODE_OFF:
 131                mode = MAX77686_LDO_MODE_OFF;
 132                break;
 133        case OPMODE_STANDBY:
 134                switch (ldo) {
 135                case 2:
 136                case 6:
 137                case 7:
 138                case 8:
 139                case 10:
 140                case 11:
 141                case 12:
 142                case 14:
 143                case 15:
 144                case 16:
 145                        mode = MAX77686_LDO_MODE_STANDBY;
 146                        break;
 147                default:
 148                        mode = 0xff;
 149                }
 150                break;
 151        case OPMODE_LPM:
 152                mode = MAX77686_LDO_MODE_LPM;
 153                break;
 154        case OPMODE_ON:
 155                mode = MAX77686_LDO_MODE_ON;
 156                break;
 157        default:
 158                mode = 0xff;
 159        }
 160
 161        if (mode == 0xff) {
 162                printf("%s: %d is not supported on LDO%d\n",
 163                       __func__, opmode, ldo);
 164                return -ENOTSUPP;
 165        }
 166
 167        ret = pmic_reg_read(p, adr, &val);
 168        if (ret)
 169                return ret;
 170
 171        val &= ~MAX77686_LDO_MODE_MASK;
 172        val |= mode;
 173        ret |= pmic_reg_write(p, adr, val);
 174
 175        return ret;
 176}
 177
 178int max77686_set_buck_mode(struct pmic *p, int buck, char opmode)
 179{
 180        unsigned int val, ret, mask, adr, size, mode, mode_shift;
 181
 182        size = ARRAY_SIZE(max77686_buck_addr);
 183        if (buck >= size) {
 184                printf("%s: %d is wrong buck number\n", __func__, buck);
 185                return -EINVAL;
 186        }
 187
 188        adr = max77686_buck_addr[buck];
 189
 190        /* mask */
 191        switch (buck) {
 192        case 2:
 193        case 3:
 194        case 4:
 195                mode_shift = MAX77686_BUCK_MODE_SHIFT_2;
 196                break;
 197        default:
 198                mode_shift = MAX77686_BUCK_MODE_SHIFT_1;
 199        }
 200
 201        mask = MAX77686_BUCK_MODE_MASK << mode_shift;
 202
 203        /* mode */
 204        switch (opmode) {
 205        case OPMODE_OFF:
 206                mode = MAX77686_BUCK_MODE_OFF << mode_shift;
 207                break;
 208        case OPMODE_STANDBY:
 209                switch (buck) {
 210                case 1:
 211                case 2:
 212                case 3:
 213                case 4:
 214                        mode = MAX77686_BUCK_MODE_STANDBY << mode_shift;
 215                        break;
 216                default:
 217                        mode = 0xff;
 218                }
 219                break;
 220        case OPMODE_LPM:
 221                switch (buck) {
 222                case 2:
 223                case 3:
 224                case 4:
 225                        mode = MAX77686_BUCK_MODE_LPM << mode_shift;
 226                        break;
 227                default:
 228                        mode = 0xff;
 229                }
 230                break;
 231        case OPMODE_ON:
 232                mode = MAX77686_BUCK_MODE_ON << mode_shift;
 233                break;
 234        default:
 235                mode = 0xff;
 236        }
 237
 238        if (mode == 0xff) {
 239                printf("%s: %d is not supported on BUCK%d\n",
 240                       __func__, opmode, buck);
 241                return -ENOTSUPP;
 242        }
 243
 244        ret = pmic_reg_read(p, adr, &val);
 245        if (ret)
 246                return ret;
 247
 248        val &= ~mask;
 249        val |= mode;
 250        ret |= pmic_reg_write(p, adr, val);
 251
 252        return ret;
 253}
 254
 255int pmic_init(unsigned char bus)
 256{
 257        static const char name[] = "MAX77686_PMIC";
 258        struct pmic *p = pmic_alloc();
 259#if CONFIG_IS_ENABLED(OF_CONTROL)
 260        const void *blob = gd->fdt_blob;
 261        int node, parent, tmp;
 262#endif
 263
 264        if (!p) {
 265                printf("%s: POWER allocation error!\n", __func__);
 266                return -ENOMEM;
 267        }
 268
 269#if CONFIG_IS_ENABLED(OF_CONTROL)
 270        node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_MAX77686_PMIC);
 271        if (node < 0) {
 272                debug("PMIC: No node for PMIC Chip in device tree\n");
 273                debug("node = %d\n", node);
 274                return -ENODEV;
 275        }
 276
 277        parent = fdt_parent_offset(blob, node);
 278        if (parent < 0) {
 279                debug("%s: Cannot find node parent\n", __func__);
 280                return -ENODEV;
 281        }
 282
 283        /* tmp since p->bus is unsigned */
 284        tmp = i2c_get_bus_num_fdt(parent);
 285        if (tmp < 0) {
 286                debug("%s: Cannot find I2C bus\n", __func__);
 287                return -ENODEV;
 288        }
 289        p->bus = tmp;
 290        p->hw.i2c.addr = fdtdec_get_int(blob, node, "reg", 9);
 291#else
 292        p->bus = bus;
 293        p->hw.i2c.addr = MAX77686_I2C_ADDR;
 294#endif
 295
 296        p->name = name;
 297        p->interface = PMIC_I2C;
 298        p->number_of_regs = MAX77686_NUM_OF_REGS;
 299        p->hw.i2c.tx_num = 1;
 300
 301        puts("Board PMIC init\n");
 302
 303        return 0;
 304}
 305