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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
// SPDX-License-Identifier: GPL-2.0
//
// Copyright (C) 2018 BayLibre SAS
// Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
//
// Regulator driver for MAXIM 77650/77651 charger/power-supply.
#include <linux/of.h>
#include <linux/mfd/max77650.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#define MAX77650_REGULATOR_EN_CTRL_MASK GENMASK(3, 0)
#define MAX77650_REGULATOR_EN_CTRL_BITS(_reg) \
((_reg) & MAX77650_REGULATOR_EN_CTRL_MASK)
#define MAX77650_REGULATOR_ENABLED GENMASK(2, 1)
#define MAX77650_REGULATOR_DISABLED BIT(2)
#define MAX77650_REGULATOR_V_LDO_MASK GENMASK(6, 0)
#define MAX77650_REGULATOR_V_SBB_MASK GENMASK(5, 0)
#define MAX77650_REGULATOR_AD_MASK BIT(3)
#define MAX77650_REGULATOR_AD_DISABLED 0x00
#define MAX77650_REGULATOR_AD_ENABLED BIT(3)
#define MAX77650_REGULATOR_CURR_LIM_MASK GENMASK(7, 6)
#define MAX77650_REGULATOR_CURR_LIM_BITS(_reg) \
(((_reg) & MAX77650_REGULATOR_CURR_LIM_MASK) >> 6)
#define MAX77650_REGULATOR_CURR_LIM_SHIFT(_val) ((_val) << 6)
enum {
MAX77650_REGULATOR_ID_LDO = 0,
MAX77650_REGULATOR_ID_SBB0,
MAX77650_REGULATOR_ID_SBB1,
MAX77650_REGULATOR_ID_SBB2,
MAX77650_REGULATOR_NUM_REGULATORS,
};
struct max77650_regulator_desc {
struct regulator_desc desc;
unsigned int regA;
unsigned int regB;
};
static const u32 max77651_sbb1_regulator_volt_table[] = {
2400000, 3200000, 4000000, 4800000,
2450000, 3250000, 4050000, 4850000,
2500000, 3300000, 4100000, 4900000,
2550000, 3350000, 4150000, 4950000,
2600000, 3400000, 4200000, 5000000,
2650000, 3450000, 4250000, 5050000,
2700000, 3500000, 4300000, 5100000,
2750000, 3550000, 4350000, 5150000,
2800000, 3600000, 4400000, 5200000,
2850000, 3650000, 4450000, 5250000,
2900000, 3700000, 4500000, 0,
2950000, 3750000, 4550000, 0,
3000000, 3800000, 4600000, 0,
3050000, 3850000, 4650000, 0,
3100000, 3900000, 4700000, 0,
3150000, 3950000, 4750000, 0,
};
#define MAX77651_REGULATOR_SBB1_SEL_DEC(_val) \
(((_val & 0x3c) >> 2) | ((_val & 0x03) << 4))
#define MAX77651_REGULATOR_SBB1_SEL_ENC(_val) \
(((_val & 0x30) >> 4) | ((_val & 0x0f) << 2))
#define MAX77650_REGULATOR_SBB1_SEL_DECR(_val) \
do { \
_val = MAX77651_REGULATOR_SBB1_SEL_DEC(_val); \
_val--; \
_val = MAX77651_REGULATOR_SBB1_SEL_ENC(_val); \
} while (0)
#define MAX77650_REGULATOR_SBB1_SEL_INCR(_val) \
do { \
_val = MAX77651_REGULATOR_SBB1_SEL_DEC(_val); \
_val++; \
_val = MAX77651_REGULATOR_SBB1_SEL_ENC(_val); \
} while (0)
static const int max77650_current_limit_table[] = {
1000000, 866000, 707000, 500000,
};
static int max77650_regulator_is_enabled(struct regulator_dev *rdev)
{
struct max77650_regulator_desc *rdesc;
struct regmap *map;
int val, rv, en;
rdesc = rdev_get_drvdata(rdev);
map = rdev_get_regmap(rdev);
rv = regmap_read(map, rdesc->regB, &val);
if (rv)
return rv;
en = MAX77650_REGULATOR_EN_CTRL_BITS(val);
return en != MAX77650_REGULATOR_DISABLED;
}
static int max77650_regulator_enable(struct regulator_dev *rdev)
{
struct max77650_regulator_desc *rdesc;
struct regmap *map;
rdesc = rdev_get_drvdata(rdev);
map = rdev_get_regmap(rdev);
return regmap_update_bits(map, rdesc->regB,
MAX77650_REGULATOR_EN_CTRL_MASK,
MAX77650_REGULATOR_ENABLED);
}
static int max77650_regulator_disable(struct regulator_dev *rdev)
{
struct max77650_regulator_desc *rdesc;
struct regmap *map;
rdesc = rdev_get_drvdata(rdev);
map = rdev_get_regmap(rdev);
return regmap_update_bits(map, rdesc->regB,
MAX77650_REGULATOR_EN_CTRL_MASK,
MAX77650_REGULATOR_DISABLED);
}
static int max77650_regulator_set_voltage_sel(struct regulator_dev *rdev,
unsigned int sel)
{
int rv = 0, curr, diff;
bool ascending;
/*
* If the regulator is disabled, we can program the desired
* voltage right away.
*/
if (!max77650_regulator_is_enabled(rdev))
return regulator_set_voltage_sel_regmap(rdev, sel);
/*
* Otherwise we need to manually ramp the output voltage up/down
* one step at a time.
*/
curr = regulator_get_voltage_sel_regmap(rdev);
if (curr < 0)
return curr;
diff = curr - sel;
if (diff == 0)
return 0; /* Already there. */
else if (diff > 0)
ascending = false;
else
ascending = true;
/*
* Make sure we'll get to the right voltage and break the loop even if
* the selector equals 0.
*/
for (ascending ? curr++ : curr--;; ascending ? curr++ : curr--) {
rv = regulator_set_voltage_sel_regmap(rdev, curr);
if (rv)
return rv;
if (curr == sel)
break;
}
return 0;
}
/*
* Special case: non-linear voltage table for max77651 SBB1 - software
* must ensure the voltage is ramped in 50mV increments.
*/
static int max77651_regulator_sbb1_set_voltage_sel(struct regulator_dev *rdev,
unsigned int sel)
{
int rv = 0, curr, vcurr, vdest, vdiff;
/*
* If the regulator is disabled, we can program the desired
* voltage right away.
*/
if (!max77650_regulator_is_enabled(rdev))
return regulator_set_voltage_sel_regmap(rdev, sel);
curr = regulator_get_voltage_sel_regmap(rdev);
if (curr < 0)
return curr;
if (curr == sel)
return 0; /* Already there. */
vcurr = max77651_sbb1_regulator_volt_table[curr];
vdest = max77651_sbb1_regulator_volt_table[sel];
vdiff = vcurr - vdest;
for (;;) {
if (vdiff > 0)
MAX77650_REGULATOR_SBB1_SEL_DECR(curr);
else
MAX77650_REGULATOR_SBB1_SEL_INCR(curr);
rv = regulator_set_voltage_sel_regmap(rdev, curr);
if (rv)
return rv;
if (curr == sel)
break;
};
return 0;
}
static int max77650_regulator_get_current_limit(struct regulator_dev *rdev)
{
struct max77650_regulator_desc *rdesc;
struct regmap *map;
int val, rv, limit;
rdesc = rdev_get_drvdata(rdev);
map = rdev_get_regmap(rdev);
rv = regmap_read(map, rdesc->regA, &val);
if (rv)
return rv;
limit = MAX77650_REGULATOR_CURR_LIM_BITS(val);
return max77650_current_limit_table[limit];
}
static int max77650_regulator_set_current_limit(struct regulator_dev *rdev,
int min_uA, int max_uA)
{
struct max77650_regulator_desc *rdesc;
struct regmap *map;
int rv, i, limit;
rdesc = rdev_get_drvdata(rdev);
map = rdev_get_regmap(rdev);
for (i = 0; i < ARRAY_SIZE(max77650_current_limit_table); i++) {
limit = max77650_current_limit_table[i];
if (limit >= min_uA && limit <= max_uA) {
rv = regmap_update_bits(map, rdesc->regA,
MAX77650_REGULATOR_CURR_LIM_MASK,
MAX77650_REGULATOR_CURR_LIM_SHIFT(i));
if (rv)
return rv;
}
}
return -EINVAL;
}
static const struct regulator_ops max77650_regulator_LDO_ops = {
.is_enabled = max77650_regulator_is_enabled,
.enable = max77650_regulator_enable,
.disable = max77650_regulator_disable,
.list_voltage = regulator_list_voltage_linear,
.map_voltage = regulator_map_voltage_linear,
.get_voltage_sel = regulator_get_voltage_sel_regmap,
.set_voltage_sel = max77650_regulator_set_voltage_sel,
.set_active_discharge =
|