1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2019
* Texas Instruments Incorporated, <www.ti.com>
*
* Keerthy <j-keerthy@ti.com>
*/
#include <common.h>
#include <fdtdec.h>
#include <errno.h>
#include <dm.h>
#include <i2c.h>
#include <log.h>
#include <linux/delay.h>
#include <power/pmic.h>
#include <power/regulator.h>
#include <power/tps65941.h>
static const char tps65941_buck_ctrl[TPS65941_BUCK_NUM] = {0x4, 0x6, 0x8, 0xA,
0xC};
static const char tps65941_buck_vout[TPS65941_BUCK_NUM] = {0xE, 0x10, 0x12,
0x14, 0x16};
static const char tps65941_ldo_ctrl[TPS65941_BUCK_NUM] = {0x1D, 0x1E, 0x1F,
0x20};
static const char tps65941_ldo_vout[TPS65941_BUCK_NUM] = {0x23, 0x24, 0x25,
0x26};
static int tps65941_buck_enable(struct udevice *dev, int op, bool *enable)
{
int ret;
unsigned int adr;
struct dm_regulator_uclass_plat *uc_pdata;
uc_pdata = dev_get_uclass_plat(dev);
adr = uc_pdata->ctrl_reg;
ret = pmic_reg_read(dev->parent, adr);
if (ret < 0)
return ret;
if (op == PMIC_OP_GET) {
ret &= TPS65941_BUCK_MODE_MASK;
if (ret)
*enable = true;
else
*enable = false;
return 0;
} else if (op == PMIC_OP_SET) {
if (*enable)
ret |= TPS65941_BUCK_MODE_MASK;
else
ret &= ~TPS65941_BUCK_MODE_MASK;
ret = pmic_reg_write(dev->parent, adr, ret);
if (ret)
return ret;
}
return 0;
}
static int tps65941_buck_volt2val(int uV)
{
if (uV > TPS65941_BUCK_VOLT_MAX)
return -EINVAL;
else if (uV > 1650000)
return (uV - 1660000) / 20000 + 0xAB;
else if (uV > 1110000)
return (uV - 1110000) / 10000 + 0x73;
else if (uV > 600000)
return (uV - 600000) / 5000 + 0x0F;
else if (uV >= 300000)
return (uV - 300000) / 20000 + 0x00;
else
return -EINVAL;
}
static int tps65941_buck_val2volt(int val)
{
if (val > TPS65941_BUCK_VOLT_MAX_HEX)
return -EINVAL;
else if (val > 0xAB)
return 1660000 + (val - 0xAB) * 20000;
else if (val > 0x73)
return 1100000 + (val - 0x73) * 10000;
else if (val > 0xF)
return 600000 + (val - 0xF) * 5000;
else if (val >= 0x0)
return 300000 + val * 5000;
else
return -EINVAL;
}
int tps65941_lookup_slew(int id)
{
switch (id) {
case 0:
return 33000;
case 1:
return 20000;
case 2:
return 10000;
case 3:
return 5000;
case 4:
return 2500;
case 5:
return 1300;
case 6:
return 630;
case 7:
return 310;
default:
return -1;
}
}
static int tps65941_buck_val(struct udevice *dev, int op, int *uV)
{
unsigned int hex, adr;
int ret, delta, uwait, slew;
struct dm_regulator_uclass_plat *uc_pdata;
uc_pdata = dev_get_uclass_plat(dev);
if (op == PMIC_OP_GET)
*uV = 0;
adr = uc_pdata->volt_reg;
ret = pmic_reg_read(dev->parent, adr);
if (ret < 0)
return ret;
ret &= TPS65941_BUCK_VOLT_MASK;
ret = tps65941_buck_val2volt(ret);
if (ret < 0)
return ret;
if (op == PMIC_OP_GET) {
*uV = ret;
return 0;
}
/*
* Compute the delta voltage, find the slew rate and wait
* for the appropriate amount of time after voltage switch
*/
if (*uV > ret)
delta = *uV - ret;
else
delta = ret - *uV;
slew = pmic_reg_read(dev->parent, uc_pdata->ctrl_reg + 1);
if (slew < 0)
return ret;
slew &= TP65941_BUCK_CONF_SLEW_MASK;
slew = tps65941_lookup_slew(slew);
if (slew <= 0)
return ret;
uwait = delta / slew;
hex = tps65941_buck_volt2val(*uV);
if (hex < 0)
return hex;
ret &= 0x0;
ret = hex;
ret = pmic_reg_write(dev->parent, adr, ret);
udelay(uwait);
return ret;
}
static int tps65941_ldo_enable(struct udevice *dev, int op, bool *enable)
{
int ret;
unsigned int adr;
struct dm_regulator_uclass_plat *uc_pdata;
uc_pdata = dev_get_uclass_plat(dev);
adr = uc_pdata->ctrl_reg;
ret = pmic_reg_read(dev->parent, adr);
if (ret < 0)
return ret;
if (op == PMIC_OP_GET) {
ret &= TPS65941_LDO_MODE_MASK;
if (ret)
*enable = true;
else
*enable = false;
return 0;
} else if (op == PMIC_OP_SET) {
if (*enable)
ret |= TPS65941_LDO_MODE_MASK;
else
ret &= ~TPS65941_LDO_MODE_MASK;
ret = pmic_reg_write(dev->parent, adr, ret);
if (ret)
return ret;
}
return 0;
}
static int tps65941_ldo_val2volt(int val)
{
if (val > TPS65941_LDO_VOLT_MAX_HEX || val < TPS65941_LDO_VOLT_MIN_HEX)
return -EINVAL;
else if (val >= TPS65941_LDO_VOLT_MIN_HEX)
return 600000 + (val - TPS65941_LDO_VOLT_MIN_HEX) * 50000;
else
return -EINVAL;
}
static int tps65941_ldo_val(struct udevice *dev, int op, int *uV)
{
unsigned int hex, adr;
int ret;
struct dm_regulator_uclass_plat *uc_pdata;
uc_pdata = dev_get_uclass_plat(dev);
if (op == PMIC_OP_GET)
*uV = 0;
adr = uc_pdata->volt_reg;
ret = pmic_reg_read(dev->parent, adr);
if (ret < 0)
return ret;
ret &= TPS65941_LDO_VOLT_MASK;
ret = tps65941_ldo_val2volt(ret);
if (ret < 0)
return ret;
if (op == PMIC_OP_GET) {
*uV = ret;
return 0;
}
hex = tps65941_buck_volt2val(*uV);
if (hex < 0)
return hex;
ret &= 0x0;
ret = hex;
ret = pmic_reg_write(dev->parent, adr, ret);
return ret;
}
static int tps65941_ldo_probe(struct udevice *dev)
{
struct dm_regulator_uclass_plat *uc_pdata;
int idx;
uc_pdata = dev_get_uclass_plat(dev);
uc_pdata->type = REGULATOR_TYPE_LDO;
idx = dev->driver_data;
if (idx == 1 || idx == 2 || idx == 3 || idx == 4) {
debug("Single phase regulator\n");
} else {
printf("Wrong ID for regulator\n");
return -EINVAL;
}
uc_pdata->ctrl_reg = tps65941_ldo_ctrl[idx - 1];
uc_pdata->volt_reg = tps65941_ldo_vout[idx - 1];
return 0;
}
static int tps65941_buck_probe(struct udevice *dev)
{
struct dm_regulator_uclass_plat *uc_pdata;
int idx;
uc_pdata = dev_get_uclass_plat(dev);
uc_pdata->type = REGULATOR_TYPE_BUCK;
idx = dev->driver_data;
if (idx == 1 || idx == 2 || idx == 3 || idx == 4 || idx == 5) {
debug("Single phase regulator\n");
} else if (idx == 12) {
idx = 1;
} else if (idx == 34) {
idx = 3;
} else if (idx == 1234) {
idx = 1;
} else {
printf("Wrong ID for regulator\n");
return -EINVAL;
}
uc_pdata->ctrl_reg = tps65941_buck_ctrl[idx - 1];
uc_pdata->volt_reg = tps65941_buck_vout[idx - 1];
return 0;
}
static int ldo_get_value(struct udevice *dev)
{
int uV;
int ret;
ret = tps65941_ldo_val(dev, PMIC_OP_GET, &uV);
if (ret)
return ret;
return uV;
}
static int ldo_set_value(struct udevice *dev, int uV)
{
return tps65941_ldo_val(dev, PMIC_OP_SET, &uV);
}
static int ldo_get_enable(struct udevice *dev)
{
bool enable = false;
int ret;
ret = tps65941_ldo_enable(dev, PMIC_OP_GET, &enable);
if (ret)
return ret;
return enable;
}
static int ldo_set_enable(struct udevice *dev, bool enable)
{
return tps65941_ldo_enable(dev, PMIC_OP_SET, &enable);
}
static int buck_get_value(struct udevice *dev)
{
int uV;
int ret;
ret = tps65941_buck_val(dev, PMIC_OP_GET, &uV);
if (ret)
return ret;
return uV;
}
static int buck_set_value(struct udevice *dev, int uV)
{
return tps65941_buck_val(dev, PMIC_OP_SET, &uV);
}
static int buck_get_enable(struct udevice *dev)
{
bool enable = false;
int ret;
ret = tps65941_buck_enable(dev, PMIC_OP_GET, &enable);
if (ret)
return ret;
return enable;
}
static int buck_set_enable(struct udevice *dev, bool enable)
{
return tps65941_buck_enable(dev, PMIC_OP_SET, &enable);
}
static const struct dm_regulator_ops tps65941_ldo_ops = {
.get_value = ldo_get_value,
.set_value = ldo_set_value,
.get_enable = ldo_get_enable,
.set_enable = ldo_set_enable,
};
U_BOOT_DRIVER(tps65941_ldo) = {
.name = TPS65941_LDO_DRIVER,
.id = UCLASS_REGULATOR,
.ops = &tps65941_ldo_ops,
.probe = tps65941_ldo_probe,
};
static const struct dm_regulator_ops tps65941_buck_ops = {
.get_value = buck_get_value,
.set_value = buck_set_value,
.get_enable = buck_get_enable,
.set_enable = buck_set_enable,
};
U_BOOT_DRIVER(tps65941_buck) = {
.name = TPS65941_BUCK_DRIVER,
.id = UCLASS_REGULATOR,
.ops = &tps65941_buck_ops,
.probe = tps65941_buck_probe,
};
|