blob: 2212cb5c651bc0ecece55d78746a52e0b5050bbf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Keerthy22831662017-06-07 19:08:29 +05302/*
3 * (C) Copyright 2017
4 * Texas Instruments Incorporated, <www.ti.com>
5 *
6 * Keerthy <j-keerthy@ti.com>
Keerthy22831662017-06-07 19:08:29 +05307 */
8
Keerthy22831662017-06-07 19:08:29 +05309#include <fdtdec.h>
10#include <errno.h>
11#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Keerthy22831662017-06-07 19:08:29 +053013#include <power/pmic.h>
14#include <power/regulator.h>
15#include <power/lp87565.h>
16
Keerthy22831662017-06-07 19:08:29 +053017static const char lp87565_buck_ctrl1[LP87565_BUCK_NUM] = {0x2, 0x4, 0x6, 0x8, 0x2, 0x6};
18static const char lp87565_buck_vout[LP87565_BUCK_NUM] = {0xA, 0xC, 0xE, 0x10, 0xA, 0xE };
19
20static int lp87565_buck_enable(struct udevice *dev, int op, bool *enable)
21{
22 int ret;
23 unsigned int adr;
Simon Glass71fa5b42020-12-03 16:55:18 -070024 struct dm_regulator_uclass_plat *uc_pdata;
Keerthy22831662017-06-07 19:08:29 +053025
Simon Glass71fa5b42020-12-03 16:55:18 -070026 uc_pdata = dev_get_uclass_plat(dev);
Keerthy22831662017-06-07 19:08:29 +053027 adr = uc_pdata->ctrl_reg;
28
29 ret = pmic_reg_read(dev->parent, adr);
30 if (ret < 0)
31 return ret;
32
33 if (op == PMIC_OP_GET) {
34 ret &= LP87565_BUCK_MODE_MASK;
35
36 if (ret)
37 *enable = true;
38 else
39 *enable = false;
40
41 return 0;
42 } else if (op == PMIC_OP_SET) {
43 if (*enable)
44 ret |= LP87565_BUCK_MODE_MASK;
45 else
46 ret &= ~LP87565_BUCK_MODE_MASK;
47 ret = pmic_reg_write(dev->parent, adr, ret);
48 if (ret)
49 return ret;
50 }
51
52 return 0;
53}
54
55static int lp87565_buck_volt2val(int uV)
56{
57 if (uV > LP87565_BUCK_VOLT_MAX)
58 return -EINVAL;
59 else if (uV > 1400000)
60 return (uV - 1420000) / 20000 + 0x9E;
61 else if (uV > 730000)
62 return (uV - 735000) / 5000 + 0x18;
63 else if (uV >= 500000)
64 return (uV - 500000) / 10000;
65 else
66 return -EINVAL;
67}
68
69static int lp87565_buck_val2volt(int val)
70{
71 if (val > LP87565_BUCK_VOLT_MAX_HEX)
72 return -EINVAL;
73 else if (val > 0x9D)
74 return 1400000 + (val - 0x9D) * 20000;
75 else if (val > 0x17)
76 return 730000 + (val - 0x17) * 5000;
77 else if (val >= 0x0)
78 return 500000 + val * 10000;
79 else
80 return -EINVAL;
81}
82
83static int lp87565_buck_val(struct udevice *dev, int op, int *uV)
84{
85 unsigned int hex, adr;
86 int ret;
Simon Glass71fa5b42020-12-03 16:55:18 -070087 struct dm_regulator_uclass_plat *uc_pdata;
Keerthy22831662017-06-07 19:08:29 +053088
Simon Glass71fa5b42020-12-03 16:55:18 -070089 uc_pdata = dev_get_uclass_plat(dev);
Keerthy22831662017-06-07 19:08:29 +053090
91 if (op == PMIC_OP_GET)
92 *uV = 0;
93
94 adr = uc_pdata->volt_reg;
95
96 ret = pmic_reg_read(dev->parent, adr);
97 if (ret < 0)
98 return ret;
99
100 if (op == PMIC_OP_GET) {
101 ret &= LP87565_BUCK_VOLT_MASK;
102 ret = lp87565_buck_val2volt(ret);
103 if (ret < 0)
104 return ret;
105 *uV = ret;
106
107 return 0;
108 }
109
110 hex = lp87565_buck_volt2val(*uV);
111 if (hex < 0)
112 return hex;
113
114 ret &= 0x0;
115 ret = hex;
116
117 ret = pmic_reg_write(dev->parent, adr, ret);
118
119 return ret;
120}
121
122static int lp87565_buck_probe(struct udevice *dev)
123{
Simon Glass71fa5b42020-12-03 16:55:18 -0700124 struct dm_regulator_uclass_plat *uc_pdata;
Keerthy22831662017-06-07 19:08:29 +0530125 int idx;
126
Simon Glass71fa5b42020-12-03 16:55:18 -0700127 uc_pdata = dev_get_uclass_plat(dev);
Keerthy22831662017-06-07 19:08:29 +0530128 uc_pdata->type = REGULATOR_TYPE_BUCK;
129
130 idx = dev->driver_data;
131 if (idx == 0 || idx == 1 || idx == 2 || idx == 3) {
132 debug("Single phase regulator\n");
133 } else if (idx == 23) {
134 idx = 5;
135 } else if (idx == 10) {
136 idx = 4;
137 } else {
138 printf("Wrong ID for regulator\n");
139 return -EINVAL;
140 }
141
142 uc_pdata->ctrl_reg = lp87565_buck_ctrl1[idx];
143 uc_pdata->volt_reg = lp87565_buck_vout[idx];
144
145 return 0;
146}
147
148static int buck_get_value(struct udevice *dev)
149{
150 int uV;
151 int ret;
152
153 ret = lp87565_buck_val(dev, PMIC_OP_GET, &uV);
154 if (ret)
155 return ret;
156
157 return uV;
158}
159
160static int buck_set_value(struct udevice *dev, int uV)
161{
162 return lp87565_buck_val(dev, PMIC_OP_SET, &uV);
163}
164
Keerthy968771a2017-06-13 09:53:56 +0530165static int buck_get_enable(struct udevice *dev)
Keerthy22831662017-06-07 19:08:29 +0530166{
167 bool enable = false;
168 int ret;
169
Keerthy22831662017-06-07 19:08:29 +0530170 ret = lp87565_buck_enable(dev, PMIC_OP_GET, &enable);
171 if (ret)
172 return ret;
173
174 return enable;
175}
176
177static int buck_set_enable(struct udevice *dev, bool enable)
178{
179 return lp87565_buck_enable(dev, PMIC_OP_SET, &enable);
180}
181
182static const struct dm_regulator_ops lp87565_buck_ops = {
183 .get_value = buck_get_value,
184 .set_value = buck_set_value,
185 .get_enable = buck_get_enable,
186 .set_enable = buck_set_enable,
187};
188
189U_BOOT_DRIVER(lp87565_buck) = {
190 .name = LP87565_BUCK_DRIVER,
191 .id = UCLASS_REGULATOR,
192 .ops = &lp87565_buck_ops,
193 .probe = lp87565_buck_probe,
194};