]> git.sur5r.net Git - u-boot/blobdiff - drivers/power/pmic/pmic_max77686.c
Merge git://git.denx.de/u-boot-sh
[u-boot] / drivers / power / pmic / pmic_max77686.c
index d4c430e22ed144dfbbf6243a07a0eab7dec861ad..8e653316d1b38d362db2398e4eafc64f223ad69e 100644 (file)
@@ -42,20 +42,39 @@ static unsigned int max77686_ldo_volt2hex(int ldo, ulong uV)
        return 0;
 }
 
+static int max77686_buck_volt2hex(int buck, ulong uV)
+{
+       int hex = 0;
+
+       if (buck < 5 || buck > 9) {
+               debug("%s: buck %d is not supported\n", __func__, buck);
+               return -EINVAL;
+       }
+
+       hex = (uV - 750000) / 50000;
+
+       if (hex >= 0 && hex <= MAX77686_BUCK_VOLT_MAX_HEX)
+               return hex;
+
+       debug("%s: %ld is wrong voltage value for BUCK%d\n",
+             __func__, uV, buck);
+       return -EINVAL;
+}
+
 int max77686_set_ldo_voltage(struct pmic *p, int ldo, ulong uV)
 {
        unsigned int val, ret, hex, adr;
 
-       if (ldo < 1 && ldo > 26) {
+       if (ldo < 1 || ldo > 26) {
                printf("%s: %d is wrong ldo number\n", __func__, ldo);
-               return -1;
+               return -EINVAL;
        }
 
        adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
        hex = max77686_ldo_volt2hex(ldo, uV);
 
        if (!hex)
-               return -1;
+               return -EINVAL;
 
        ret = pmic_reg_read(p, adr, &val);
        if (ret)
@@ -68,13 +87,40 @@ int max77686_set_ldo_voltage(struct pmic *p, int ldo, ulong uV)
        return ret;
 }
 
+int max77686_set_buck_voltage(struct pmic *p, int buck, ulong uV)
+{
+       unsigned int val, adr;
+       int hex, ret;
+
+       if (buck < 5 || buck > 9) {
+               printf("%s: %d is an unsupported bucket number\n",
+                      __func__, buck);
+               return -EINVAL;
+       }
+
+       adr = max77686_buck_addr[buck] + 1;
+       hex = max77686_buck_volt2hex(buck, uV);
+
+       if (hex < 0)
+               return hex;
+
+       ret = pmic_reg_read(p, adr, &val);
+       if (ret)
+               return ret;
+
+       val &= ~MAX77686_BUCK_VOLT_MASK;
+       ret |= pmic_reg_write(p, adr, val | hex);
+
+       return ret;
+}
+
 int max77686_set_ldo_mode(struct pmic *p, int ldo, char opmode)
 {
        unsigned int val, ret, adr, mode;
 
-       if (ldo < 1 && 26 < ldo) {
+       if (ldo < 1 || 26 < ldo) {
                printf("%s: %d is wrong ldo number\n", __func__, ldo);
-               return -1;
+               return -EINVAL;
        }
 
        adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
@@ -115,7 +161,7 @@ int max77686_set_ldo_mode(struct pmic *p, int ldo, char opmode)
        if (mode == 0xff) {
                printf("%s: %d is not supported on LDO%d\n",
                       __func__, opmode, ldo);
-               return -1;
+               return -ENOTSUPP;
        }
 
        ret = pmic_reg_read(p, adr, &val);
@@ -136,7 +182,7 @@ int max77686_set_buck_mode(struct pmic *p, int buck, char opmode)
        size = ARRAY_SIZE(max77686_buck_addr);
        if (buck >= size) {
                printf("%s: %d is wrong buck number\n", __func__, buck);
-               return -1;
+               return -EINVAL;
        }
 
        adr = max77686_buck_addr[buck];
@@ -157,7 +203,7 @@ int max77686_set_buck_mode(struct pmic *p, int buck, char opmode)
        /* mode */
        switch (opmode) {
        case OPMODE_OFF:
-               mode = MAX77686_BUCK_MODE_OFF;
+               mode = MAX77686_BUCK_MODE_OFF << mode_shift;
                break;
        case OPMODE_STANDBY:
                switch (buck) {
@@ -192,7 +238,7 @@ int max77686_set_buck_mode(struct pmic *p, int buck, char opmode)
        if (mode == 0xff) {
                printf("%s: %d is not supported on BUCK%d\n",
                       __func__, opmode, buck);
-               return -1;
+               return -ENOTSUPP;
        }
 
        ret = pmic_reg_read(p, adr, &val);
@@ -210,34 +256,37 @@ int pmic_init(unsigned char bus)
 {
        static const char name[] = "MAX77686_PMIC";
        struct pmic *p = pmic_alloc();
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+       const void *blob = gd->fdt_blob;
+       int node, parent, tmp;
+#endif
 
        if (!p) {
                printf("%s: POWER allocation error!\n", __func__);
                return -ENOMEM;
        }
 
-#ifdef CONFIG_OF_CONTROL
-       const void *blob = gd->fdt_blob;
-       int node, parent;
-
+#if CONFIG_IS_ENABLED(OF_CONTROL)
        node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_MAX77686_PMIC);
        if (node < 0) {
                debug("PMIC: No node for PMIC Chip in device tree\n");
                debug("node = %d\n", node);
-               return -1;
+               return -ENODEV;
        }
 
        parent = fdt_parent_offset(blob, node);
        if (parent < 0) {
                debug("%s: Cannot find node parent\n", __func__);
-               return -1;
+               return -ENODEV;
        }
 
-       p->bus = i2c_get_bus_num_fdt(parent);
-       if (p->bus < 0) {
+       /* tmp since p->bus is unsigned */
+       tmp = i2c_get_bus_num_fdt(parent);
+       if (tmp < 0) {
                debug("%s: Cannot find I2C bus\n", __func__);
-               return -1;
+               return -ENODEV;
        }
+       p->bus = tmp;
        p->hw.i2c.addr = fdtdec_get_int(blob, node, "reg", 9);
 #else
        p->bus = bus;
@@ -246,7 +295,7 @@ int pmic_init(unsigned char bus)
 
        p->name = name;
        p->interface = PMIC_I2C;
-       p->number_of_regs = PMIC_NUM_OF_REGS;
+       p->number_of_regs = MAX77686_NUM_OF_REGS;
        p->hw.i2c.tx_num = 1;
 
        puts("Board PMIC init\n");