Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / board / freescale / common / mc34vr500.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2016 Freescale Semiconductor, Inc.
4  * Hou Zhiqiang <Zhiqiang.Hou@freescale.com>
5  */
6
7 #include <common.h>
8 #include <errno.h>
9 #include <i2c.h>
10 #include <log.h>
11 #include <power/pmic.h>
12 #include <power/mc34vr500_pmic.h>
13
14 static uint8_t swxvolt_addr[4] = { MC34VR500_SW1VOLT,
15                                    MC34VR500_SW2VOLT,
16                                    MC34VR500_SW3VOLT,
17                                    MC34VR500_SW4VOLT };
18
19 static uint8_t swx_set_point_base[4] = { 13, 9, 9, 9 };
20
21 int mc34vr500_get_sw_volt(uint8_t sw)
22 {
23         struct pmic *p;
24         u32 swxvolt;
25         uint8_t spb;
26         int sw_volt;
27         int ret;
28
29         debug("%s: Get SW%u volt from swxvolt_addr = 0x%x\n",
30               __func__, sw + 1, swxvolt_addr[sw]);
31         if (sw > SW4) {
32                 printf("%s: Unsupported SW(sw%d)\n", __func__, sw + 1);
33                 return -EINVAL;
34         }
35
36         p = pmic_get("MC34VR500");
37         if (!p) {
38                 printf("%s: Did NOT find PMIC MC34VR500\n", __func__);
39                 return -ENODEV;
40         }
41
42         ret = pmic_probe(p);
43         if (ret)
44                 return ret;
45
46         ret = pmic_reg_read(p, swxvolt_addr[sw], &swxvolt);
47         if (ret) {
48                 printf("%s: Failed to get SW%u volt\n", __func__, sw + 1);
49                 return ret;
50         }
51
52         debug("%s: SW%d step point swxvolt = %u\n", __func__, sw + 1, swxvolt);
53         spb = swx_set_point_base[sw];
54         /* The base of SW volt is 625mV and increase by step 25mV */
55         sw_volt = 625 + (swxvolt - spb) * 25;
56
57         debug("%s: SW%u volt = %dmV\n", __func__, sw + 1, sw_volt);
58         return sw_volt;
59 }
60
61 int mc34vr500_set_sw_volt(uint8_t sw, int sw_volt)
62 {
63         struct pmic *p;
64         u32 swxvolt;
65         uint8_t spb;
66         int ret;
67
68         debug("%s: Set SW%u volt to %dmV\n", __func__, sw + 1, sw_volt);
69         /* The least SW volt is 625mV, and only 4 SW outputs */
70         if (sw > SW4 || sw_volt < 625)
71                 return -EINVAL;
72
73         p = pmic_get("MC34VR500");
74         if (!p) {
75                 printf("%s: Did NOT find PMIC MC34VR500\n", __func__);
76                 return -ENODEV;
77         }
78
79         ret = pmic_probe(p);
80         if (ret)
81                 return ret;
82
83         spb = swx_set_point_base[sw];
84         /* The base of SW volt is 625mV and increase by step 25mV */
85         swxvolt = (sw_volt - 625) / 25 + spb;
86         debug("%s: SW%d step point swxvolt = %u\n", __func__, sw + 1, swxvolt);
87         if (swxvolt > 63)
88                 return -EINVAL;
89
90         ret = pmic_reg_write(p, swxvolt_addr[sw], swxvolt);
91         if (ret)
92                 return ret;
93
94         return 0;
95 }