Loading...
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 ROHM Semiconductors
3// bd71837-regulator.c ROHM BD71837MWV/BD71847MWV regulator driver
4
5#include <linux/cleanup.h>
6#include <linux/delay.h>
7#include <linux/err.h>
8#include <linux/interrupt.h>
9#include <linux/kernel.h>
10#include <linux/mfd/rohm-bd718x7.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/platform_device.h>
14#include <linux/regulator/driver.h>
15#include <linux/regulator/machine.h>
16#include <linux/regulator/of_regulator.h>
17#include <linux/slab.h>
18
19/* Typical regulator startup times as per data sheet in uS */
20#define BD71847_BUCK1_STARTUP_TIME 144
21#define BD71847_BUCK2_STARTUP_TIME 162
22#define BD71847_BUCK3_STARTUP_TIME 162
23#define BD71847_BUCK4_STARTUP_TIME 240
24#define BD71847_BUCK5_STARTUP_TIME 270
25#define BD71847_BUCK6_STARTUP_TIME 200
26#define BD71847_LDO1_STARTUP_TIME 440
27#define BD71847_LDO2_STARTUP_TIME 370
28#define BD71847_LDO3_STARTUP_TIME 310
29#define BD71847_LDO4_STARTUP_TIME 400
30#define BD71847_LDO5_STARTUP_TIME 530
31#define BD71847_LDO6_STARTUP_TIME 400
32
33#define BD71837_BUCK1_STARTUP_TIME 160
34#define BD71837_BUCK2_STARTUP_TIME 180
35#define BD71837_BUCK3_STARTUP_TIME 180
36#define BD71837_BUCK4_STARTUP_TIME 180
37#define BD71837_BUCK5_STARTUP_TIME 160
38#define BD71837_BUCK6_STARTUP_TIME 240
39#define BD71837_BUCK7_STARTUP_TIME 220
40#define BD71837_BUCK8_STARTUP_TIME 200
41#define BD71837_LDO1_STARTUP_TIME 440
42#define BD71837_LDO2_STARTUP_TIME 370
43#define BD71837_LDO3_STARTUP_TIME 310
44#define BD71837_LDO4_STARTUP_TIME 400
45#define BD71837_LDO5_STARTUP_TIME 310
46#define BD71837_LDO6_STARTUP_TIME 400
47#define BD71837_LDO7_STARTUP_TIME 530
48
49/*
50 * BD718(37/47/50) have two "enable control modes". ON/OFF can either be
51 * controlled by software - or by PMIC internal HW state machine. Whether
52 * regulator should be under SW or HW control can be defined from device-tree.
53 * Let's provide separate ops for regulators to use depending on the "enable
54 * control mode".
55 */
56#define BD718XX_HWOPNAME(swopname) swopname##_hwcontrol
57
58#define BD718XX_OPS(name, _list_voltage, _map_voltage, _set_voltage_sel, \
59 _get_voltage_sel, _set_voltage_time_sel, _set_ramp_delay, \
60 _set_uvp, _set_ovp) \
61static const struct regulator_ops name = { \
62 .enable = regulator_enable_regmap, \
63 .disable = regulator_disable_regmap, \
64 .is_enabled = regulator_is_enabled_regmap, \
65 .list_voltage = (_list_voltage), \
66 .map_voltage = (_map_voltage), \
67 .set_voltage_sel = (_set_voltage_sel), \
68 .get_voltage_sel = (_get_voltage_sel), \
69 .set_voltage_time_sel = (_set_voltage_time_sel), \
70 .set_ramp_delay = (_set_ramp_delay), \
71 .set_under_voltage_protection = (_set_uvp), \
72 .set_over_voltage_protection = (_set_ovp), \
73}; \
74 \
75static const struct regulator_ops BD718XX_HWOPNAME(name) = { \
76 .is_enabled = always_enabled_by_hwstate, \
77 .list_voltage = (_list_voltage), \
78 .map_voltage = (_map_voltage), \
79 .set_voltage_sel = (_set_voltage_sel), \
80 .get_voltage_sel = (_get_voltage_sel), \
81 .set_voltage_time_sel = (_set_voltage_time_sel), \
82 .set_ramp_delay = (_set_ramp_delay), \
83 .set_under_voltage_protection = (_set_uvp), \
84 .set_over_voltage_protection = (_set_ovp), \
85} \
86
87/*
88 * BUCK1/2/3/4
89 * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
90 * 00: 10.00mV/usec 10mV 1uS
91 * 01: 5.00mV/usec 10mV 2uS
92 * 10: 2.50mV/usec 10mV 4uS
93 * 11: 1.25mV/usec 10mV 8uS
94 */
95static const unsigned int bd718xx_ramp_delay[] = { 10000, 5000, 2500, 1250 };
96
97/* These functions are used when regulators are under HW state machine control.
98 * We assume PMIC is in RUN state because SW running and able to query the
99 * status. Most of the regulators have fixed ON or OFF state at RUN/IDLE so for
100 * them we just return a constant. BD71837 BUCK3 and BUCK4 are exceptions as
101 * they support configuring the ON/OFF state for RUN.
102 *
103 * Note for next hacker - these PMICs have a register where the HW state can be
104 * read. If assuming RUN appears to be false in your use-case - you can
105 * implement state reading (although that is not going to be atomic) before
106 * returning the enable state.
107 */
108static int always_enabled_by_hwstate(struct regulator_dev *rdev)
109{
110 return 1;
111}
112
113static int never_enabled_by_hwstate(struct regulator_dev *rdev)
114{
115 return 0;
116}
117
118static int bd71837_get_buck34_enable_hwctrl(struct regulator_dev *rdev)
119{
120 int ret;
121 unsigned int val;
122
123 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
124 if (ret)
125 return ret;
126
127 return !!(BD718XX_BUCK_RUN_ON & val);
128}
129
130static void voltage_change_done(struct regulator_dev *rdev, unsigned int sel,
131 unsigned int *mask)
132{
133 int ret;
134
135 if (*mask) {
136 /*
137 * Let's allow scheduling as we use I2C anyways. We just need to
138 * guarantee minimum of 1ms sleep - it shouldn't matter if we
139 * exceed it due to the scheduling.
140 */
141 msleep(1);
142
143 ret = regmap_clear_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
144 *mask);
145 if (ret)
146 dev_err(&rdev->dev,
147 "Failed to re-enable voltage monitoring (%d)\n",
148 ret);
149 }
150}
151
152static int voltage_change_prepare(struct regulator_dev *rdev, unsigned int sel,
153 unsigned int *mask)
154{
155 int ret;
156
157 *mask = 0;
158 if (rdev->desc->ops->is_enabled(rdev)) {
159 int now, new;
160
161 now = rdev->desc->ops->get_voltage_sel(rdev);
162 if (now < 0)
163 return now;
164
165 now = rdev->desc->ops->list_voltage(rdev, now);
166 if (now < 0)
167 return now;
168
169 new = rdev->desc->ops->list_voltage(rdev, sel);
170 if (new < 0)
171 return new;
172
173 /*
174 * If we increase LDO voltage when LDO is enabled we need to
175 * disable the power-good detection until voltage has reached
176 * the new level. According to HW colleagues the maximum time
177 * it takes is 1000us. I assume that on systems with light load
178 * this might be less - and we could probably use DT to give
179 * system specific delay value if performance matters.
180 *
181 * Well, knowing we use I2C here and can add scheduling delays
182 * I don't think it is worth the hassle and I just add fixed
183 * 1ms sleep here (and allow scheduling). If this turns out to
184 * be a problem we can change it to delay and make the delay
185 * time configurable.
186 */
187 if (new > now) {
188 int tmp;
189 int prot_bit;
190 int ldo_offset = rdev->desc->id - BD718XX_LDO1;
191
192 prot_bit = BD718XX_LDO1_VRMON80 << ldo_offset;
193 ret = regmap_read(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
194 &tmp);
195 if (ret) {
196 dev_err(&rdev->dev,
197 "Failed to read voltage monitoring state\n");
198 return ret;
199 }
200
201 if (!(tmp & prot_bit)) {
202 /* We disable protection if it was enabled... */
203 ret = regmap_set_bits(rdev->regmap,
204 BD718XX_REG_MVRFLTMASK2,
205 prot_bit);
206 /* ...and we also want to re-enable it */
207 *mask = prot_bit;
208 }
209 if (ret) {
210 dev_err(&rdev->dev,
211 "Failed to stop voltage monitoring\n");
212 return ret;
213 }
214 }
215 }
216
217 return 0;
218}
219
220static int bd718xx_set_voltage_sel_restricted(struct regulator_dev *rdev,
221 unsigned int sel)
222{
223 int ret;
224 int mask;
225
226 ret = voltage_change_prepare(rdev, sel, &mask);
227 if (ret)
228 return ret;
229
230 ret = regulator_set_voltage_sel_regmap(rdev, sel);
231 voltage_change_done(rdev, sel, &mask);
232
233 return ret;
234}
235
236static int bd718xx_set_voltage_sel_pickable_restricted(
237 struct regulator_dev *rdev, unsigned int sel)
238{
239 int ret;
240 int mask;
241
242 ret = voltage_change_prepare(rdev, sel, &mask);
243 if (ret)
244 return ret;
245
246 ret = regulator_set_voltage_sel_pickable_regmap(rdev, sel);
247 voltage_change_done(rdev, sel, &mask);
248
249 return ret;
250}
251
252static int bd71837_set_voltage_sel_pickable_restricted(
253 struct regulator_dev *rdev, unsigned int sel)
254{
255 if (rdev->desc->ops->is_enabled(rdev))
256 return -EBUSY;
257
258 return regulator_set_voltage_sel_pickable_regmap(rdev, sel);
259}
260
261/*
262 * BD71837 BUCK1/2/3/4
263 * BD71847 BUCK1/2
264 * 0.70 to 1.30V (10mV step)
265 */
266static const struct linear_range bd718xx_dvs_buck_volts[] = {
267 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x3C, 10000),
268 REGULATOR_LINEAR_RANGE(1300000, 0x3D, 0x3F, 0),
269};
270
271/*
272 * BD71837 BUCK5
273 * 0.7V to 1.35V (range 0)
274 * and
275 * 0.675 to 1.325 (range 1)
276 */
277static const struct linear_range bd71837_buck5_volts[] = {
278 /* Ranges when VOLT_SEL bit is 0 */
279 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
280 REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
281 REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
282 /* Ranges when VOLT_SEL bit is 1 */
283 REGULATOR_LINEAR_RANGE(675000, 0x0, 0x3, 100000),
284 REGULATOR_LINEAR_RANGE(1025000, 0x4, 0x5, 50000),
285 REGULATOR_LINEAR_RANGE(1175000, 0x6, 0x7, 150000),
286};
287
288/*
289 * Range selector for first 3 linear ranges is 0x0
290 * and 0x1 for last 3 ranges.
291 */
292static const unsigned int bd71837_buck5_volt_range_sel[] = {
293 0x0, 0x0, 0x0, 0x1, 0x1, 0x1
294};
295
296/*
297 * BD71847 BUCK3
298 */
299static const struct linear_range bd71847_buck3_volts[] = {
300 /* Ranges when VOLT_SEL bits are 00 */
301 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
302 REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
303 REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
304 /* Ranges when VOLT_SEL bits are 01 */
305 REGULATOR_LINEAR_RANGE(550000, 0x0, 0x7, 50000),
306 /* Ranges when VOLT_SEL bits are 11 */
307 REGULATOR_LINEAR_RANGE(675000, 0x0, 0x3, 100000),
308 REGULATOR_LINEAR_RANGE(1025000, 0x4, 0x5, 50000),
309 REGULATOR_LINEAR_RANGE(1175000, 0x6, 0x7, 150000),
310};
311
312static const unsigned int bd71847_buck3_volt_range_sel[] = {
313 0x0, 0x0, 0x0, 0x1, 0x2, 0x2, 0x2
314};
315
316static const struct linear_range bd71847_buck4_volts[] = {
317 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
318 REGULATOR_LINEAR_RANGE(2600000, 0x00, 0x03, 100000),
319};
320
321static const unsigned int bd71847_buck4_volt_range_sel[] = { 0x0, 0x1 };
322
323/*
324 * BUCK6
325 * 3.0V to 3.3V (step 100mV)
326 */
327static const struct linear_range bd71837_buck6_volts[] = {
328 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
329};
330
331/*
332 * BD71837 BUCK7
333 * BD71847 BUCK5
334 * 000 = 1.605V
335 * 001 = 1.695V
336 * 010 = 1.755V
337 * 011 = 1.8V (Initial)
338 * 100 = 1.845V
339 * 101 = 1.905V
340 * 110 = 1.95V
341 * 111 = 1.995V
342 */
343static const unsigned int bd718xx_3rd_nodvs_buck_volts[] = {
344 1605000, 1695000, 1755000, 1800000, 1845000, 1905000, 1950000, 1995000
345};
346
347/*
348 * BUCK8
349 * 0.8V to 1.40V (step 10mV)
350 */
351static const struct linear_range bd718xx_4th_nodvs_buck_volts[] = {
352 REGULATOR_LINEAR_RANGE(800000, 0x00, 0x3C, 10000),
353};
354
355/*
356 * LDO1
357 * 3.0 to 3.3V (100mV step)
358 */
359static const struct linear_range bd718xx_ldo1_volts[] = {
360 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
361 REGULATOR_LINEAR_RANGE(1600000, 0x00, 0x03, 100000),
362};
363
364static const unsigned int bd718xx_ldo1_volt_range_sel[] = { 0x0, 0x1 };
365
366/*
367 * LDO2
368 * 0.8 or 0.9V
369 */
370static const unsigned int ldo_2_volts[] = {
371 900000, 800000
372};
373
374/*
375 * LDO3
376 * 1.8 to 3.3V (100mV step)
377 */
378static const struct linear_range bd718xx_ldo3_volts[] = {
379 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
380};
381
382/*
383 * LDO4
384 * 0.9 to 1.8V (100mV step)
385 */
386static const struct linear_range bd718xx_ldo4_volts[] = {
387 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
388};
389
390/*
391 * LDO5 for BD71837
392 * 1.8 to 3.3V (100mV step)
393 */
394static const struct linear_range bd71837_ldo5_volts[] = {
395 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
396};
397
398/*
399 * LDO5 for BD71837
400 * 1.8 to 3.3V (100mV step)
401 */
402static const struct linear_range bd71847_ldo5_volts[] = {
403 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
404 REGULATOR_LINEAR_RANGE(800000, 0x00, 0x0F, 100000),
405};
406
407static const unsigned int bd71847_ldo5_volt_range_sel[] = { 0x0, 0x1 };
408
409/*
410 * LDO6
411 * 0.9 to 1.8V (100mV step)
412 */
413static const struct linear_range bd718xx_ldo6_volts[] = {
414 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
415};
416
417/*
418 * LDO7
419 * 1.8 to 3.3V (100mV step)
420 */
421static const struct linear_range bd71837_ldo7_volts[] = {
422 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
423};
424
425struct reg_init {
426 unsigned int reg;
427 unsigned int mask;
428 unsigned int val;
429};
430struct bd718xx_regulator_data {
431 struct regulator_desc desc;
432 const struct rohm_dvs_config dvs;
433 const struct reg_init init;
434 const struct reg_init *additional_inits;
435 int additional_init_amnt;
436};
437
438static int bd718x7_xvp_sanity_check(struct regulator_dev *rdev, int lim_uV,
439 int severity)
440{
441 /*
442 * BD71837/47/50 ... (ICs supported by this driver) do not provide
443 * warnings, only protection
444 */
445 if (severity != REGULATOR_SEVERITY_PROT) {
446 dev_err(&rdev->dev,
447 "Unsupported Under Voltage protection level\n");
448 return -EINVAL;
449 }
450
451 /*
452 * And protection limit is not changeable. It can only be enabled
453 * or disabled
454 */
455 if (lim_uV)
456 return -EINVAL;
457
458 return 0;
459}
460
461static int bd718x7_set_ldo_uvp(struct regulator_dev *rdev, int lim_uV,
462 int severity, bool enable)
463{
464 int ldo_offset = rdev->desc->id - BD718XX_LDO1;
465 int prot_bit, ret;
466
467 ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
468 if (ret)
469 return ret;
470
471 prot_bit = BD718XX_LDO1_VRMON80 << ldo_offset;
472
473 if (enable)
474 return regmap_clear_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
475 prot_bit);
476
477 return regmap_set_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
478 prot_bit);
479}
480
481static int bd718x7_get_buck_prot_reg(int id, int *reg)
482{
483
484 if (id > BD718XX_BUCK8) {
485 WARN_ON(id > BD718XX_BUCK8);
486 return -EINVAL;
487 }
488
489 if (id > BD718XX_BUCK4)
490 *reg = BD718XX_REG_MVRFLTMASK0;
491 else
492 *reg = BD718XX_REG_MVRFLTMASK1;
493
494 return 0;
495}
496
497static int bd718x7_get_buck_ovp_info(int id, int *reg, int *bit)
498{
499 int ret;
500
501 ret = bd718x7_get_buck_prot_reg(id, reg);
502 if (ret)
503 return ret;
504
505 *bit = BIT((id % 4) * 2 + 1);
506
507 return 0;
508}
509
510static int bd718x7_get_buck_uvp_info(int id, int *reg, int *bit)
511{
512 int ret;
513
514 ret = bd718x7_get_buck_prot_reg(id, reg);
515 if (ret)
516 return ret;
517
518 *bit = BIT((id % 4) * 2);
519
520 return 0;
521}
522
523static int bd718x7_set_buck_uvp(struct regulator_dev *rdev, int lim_uV,
524 int severity, bool enable)
525{
526 int bit, reg, ret;
527
528 ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
529 if (ret)
530 return ret;
531
532 ret = bd718x7_get_buck_uvp_info(rdev->desc->id, ®, &bit);
533 if (ret)
534 return ret;
535
536 if (enable)
537 return regmap_clear_bits(rdev->regmap, reg, bit);
538
539 return regmap_set_bits(rdev->regmap, reg, bit);
540
541}
542
543static int bd718x7_set_buck_ovp(struct regulator_dev *rdev, int lim_uV,
544 int severity,
545 bool enable)
546{
547 int bit, reg, ret;
548
549 ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
550 if (ret)
551 return ret;
552
553 ret = bd718x7_get_buck_ovp_info(rdev->desc->id, ®, &bit);
554 if (ret)
555 return ret;
556
557 if (enable)
558 return regmap_clear_bits(rdev->regmap, reg, bit);
559
560 return regmap_set_bits(rdev->regmap, reg, bit);
561}
562
563/*
564 * OPS common for BD71847 and BD71850
565 */
566BD718XX_OPS(bd718xx_pickable_range_ldo_ops,
567 regulator_list_voltage_pickable_linear_range, NULL,
568 bd718xx_set_voltage_sel_pickable_restricted,
569 regulator_get_voltage_sel_pickable_regmap, NULL, NULL,
570 bd718x7_set_ldo_uvp, NULL);
571
572/* BD71847 and BD71850 LDO 5 is by default OFF at RUN state */
573static const struct regulator_ops bd718xx_ldo5_ops_hwstate = {
574 .is_enabled = never_enabled_by_hwstate,
575 .list_voltage = regulator_list_voltage_pickable_linear_range,
576 .set_voltage_sel = bd718xx_set_voltage_sel_pickable_restricted,
577 .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
578 .set_under_voltage_protection = bd718x7_set_ldo_uvp,
579};
580
581BD718XX_OPS(bd718xx_pickable_range_buck_ops,
582 regulator_list_voltage_pickable_linear_range, NULL,
583 regulator_set_voltage_sel_pickable_regmap,
584 regulator_get_voltage_sel_pickable_regmap,
585 regulator_set_voltage_time_sel, NULL, bd718x7_set_buck_uvp,
586 bd718x7_set_buck_ovp);
587
588BD718XX_OPS(bd718xx_ldo_regulator_ops, regulator_list_voltage_linear_range,
589 NULL, bd718xx_set_voltage_sel_restricted,
590 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
591 NULL);
592
593BD718XX_OPS(bd718xx_ldo_regulator_nolinear_ops, regulator_list_voltage_table,
594 NULL, bd718xx_set_voltage_sel_restricted,
595 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
596 NULL);
597
598BD718XX_OPS(bd718xx_buck_regulator_ops, regulator_list_voltage_linear_range,
599 NULL, regulator_set_voltage_sel_regmap,
600 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
601 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
602
603BD718XX_OPS(bd718xx_buck_regulator_nolinear_ops, regulator_list_voltage_table,
604 regulator_map_voltage_ascend, regulator_set_voltage_sel_regmap,
605 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
606 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
607
608/*
609 * OPS for BD71837
610 */
611BD718XX_OPS(bd71837_pickable_range_ldo_ops,
612 regulator_list_voltage_pickable_linear_range, NULL,
613 bd71837_set_voltage_sel_pickable_restricted,
614 regulator_get_voltage_sel_pickable_regmap, NULL, NULL,
615 bd718x7_set_ldo_uvp, NULL);
616
617BD718XX_OPS(bd71837_pickable_range_buck_ops,
618 regulator_list_voltage_pickable_linear_range, NULL,
619 bd71837_set_voltage_sel_pickable_restricted,
620 regulator_get_voltage_sel_pickable_regmap,
621 regulator_set_voltage_time_sel, NULL, bd718x7_set_buck_uvp,
622 bd718x7_set_buck_ovp);
623
624BD718XX_OPS(bd71837_ldo_regulator_ops, regulator_list_voltage_linear_range,
625 NULL, rohm_regulator_set_voltage_sel_restricted,
626 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
627 NULL);
628
629BD718XX_OPS(bd71837_ldo_regulator_nolinear_ops, regulator_list_voltage_table,
630 NULL, rohm_regulator_set_voltage_sel_restricted,
631 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
632 NULL);
633
634BD718XX_OPS(bd71837_buck_regulator_ops, regulator_list_voltage_linear_range,
635 NULL, rohm_regulator_set_voltage_sel_restricted,
636 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
637 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
638
639BD718XX_OPS(bd71837_buck_regulator_nolinear_ops, regulator_list_voltage_table,
640 regulator_map_voltage_ascend, rohm_regulator_set_voltage_sel_restricted,
641 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
642 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
643/*
644 * BD71837 bucks 3 and 4 support defining their enable/disable state also
645 * when buck enable state is under HW state machine control. In that case the
646 * bit [2] in CTRL register is used to indicate if regulator should be ON.
647 */
648static const struct regulator_ops bd71837_buck34_ops_hwctrl = {
649 .is_enabled = bd71837_get_buck34_enable_hwctrl,
650 .list_voltage = regulator_list_voltage_linear_range,
651 .set_voltage_sel = regulator_set_voltage_sel_regmap,
652 .get_voltage_sel = regulator_get_voltage_sel_regmap,
653 .set_voltage_time_sel = regulator_set_voltage_time_sel,
654 .set_ramp_delay = regulator_set_ramp_delay_regmap,
655 .set_under_voltage_protection = bd718x7_set_buck_uvp,
656 .set_over_voltage_protection = bd718x7_set_buck_ovp,
657};
658
659/*
660 * OPS for all of the ICs - BD718(37/47/50)
661 */
662BD718XX_OPS(bd718xx_dvs_buck_regulator_ops, regulator_list_voltage_linear_range,
663 NULL, regulator_set_voltage_sel_regmap,
664 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
665 regulator_set_ramp_delay_regmap, bd718x7_set_buck_uvp,
666 bd718x7_set_buck_ovp);
667
668
669
670/*
671 * There is a HW quirk in BD71837. The shutdown sequence timings for
672 * bucks/LDOs which are controlled via register interface are changed.
673 * At PMIC poweroff the voltage for BUCK6/7 is cut immediately at the
674 * beginning of shut-down sequence. As bucks 6 and 7 are parent
675 * supplies for LDO5 and LDO6 - this causes LDO5/6 voltage
676 * monitoring to errorneously detect under voltage and force PMIC to
677 * emergency state instead of poweroff. In order to avoid this we
678 * disable voltage monitoring for LDO5 and LDO6
679 */
680static const struct reg_init bd71837_ldo5_inits[] = {
681 {
682 .reg = BD718XX_REG_MVRFLTMASK2,
683 .mask = BD718XX_LDO5_VRMON80,
684 .val = BD718XX_LDO5_VRMON80,
685 },
686};
687
688static const struct reg_init bd71837_ldo6_inits[] = {
689 {
690 .reg = BD718XX_REG_MVRFLTMASK2,
691 .mask = BD718XX_LDO6_VRMON80,
692 .val = BD718XX_LDO6_VRMON80,
693 },
694};
695
696static int buck_set_hw_dvs_levels(struct device_node *np,
697 const struct regulator_desc *desc,
698 struct regulator_config *cfg)
699{
700 struct bd718xx_regulator_data *data;
701
702 data = container_of(desc, struct bd718xx_regulator_data, desc);
703
704 return rohm_regulator_set_dvs_levels(&data->dvs, np, desc, cfg->regmap);
705}
706
707static const struct regulator_ops *bd71847_swcontrol_ops[] = {
708 &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
709 &bd718xx_pickable_range_buck_ops, &bd718xx_pickable_range_buck_ops,
710 &bd718xx_buck_regulator_nolinear_ops, &bd718xx_buck_regulator_ops,
711 &bd718xx_pickable_range_ldo_ops, &bd718xx_ldo_regulator_nolinear_ops,
712 &bd718xx_ldo_regulator_ops, &bd718xx_ldo_regulator_ops,
713 &bd718xx_pickable_range_ldo_ops, &bd718xx_ldo_regulator_ops,
714};
715
716static const struct regulator_ops *bd71847_hwcontrol_ops[] = {
717 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
718 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
719 &BD718XX_HWOPNAME(bd718xx_pickable_range_buck_ops),
720 &BD718XX_HWOPNAME(bd718xx_pickable_range_buck_ops),
721 &BD718XX_HWOPNAME(bd718xx_buck_regulator_nolinear_ops),
722 &BD718XX_HWOPNAME(bd718xx_buck_regulator_ops),
723 &BD718XX_HWOPNAME(bd718xx_pickable_range_ldo_ops),
724 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_nolinear_ops),
725 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
726 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
727 &bd718xx_ldo5_ops_hwstate,
728 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
729};
730
731static struct bd718xx_regulator_data bd71847_regulators[] = {
732 {
733 .desc = {
734 .name = "buck1",
735 .of_match = of_match_ptr("BUCK1"),
736 .regulators_node = of_match_ptr("regulators"),
737 .id = BD718XX_BUCK1,
738 .type = REGULATOR_VOLTAGE,
739 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
740 .linear_ranges = bd718xx_dvs_buck_volts,
741 .n_linear_ranges =
742 ARRAY_SIZE(bd718xx_dvs_buck_volts),
743 .vsel_reg = BD718XX_REG_BUCK1_VOLT_RUN,
744 .vsel_mask = DVS_BUCK_RUN_MASK,
745 .enable_reg = BD718XX_REG_BUCK1_CTRL,
746 .enable_mask = BD718XX_BUCK_EN,
747 .enable_time = BD71847_BUCK1_STARTUP_TIME,
748 .owner = THIS_MODULE,
749 .ramp_delay_table = bd718xx_ramp_delay,
750 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
751 .ramp_reg = BD718XX_REG_BUCK1_CTRL,
752 .ramp_mask = BUCK_RAMPRATE_MASK,
753 .of_parse_cb = buck_set_hw_dvs_levels,
754 },
755 .dvs = {
756 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE |
757 ROHM_DVS_LEVEL_SUSPEND,
758 .run_reg = BD718XX_REG_BUCK1_VOLT_RUN,
759 .run_mask = DVS_BUCK_RUN_MASK,
760 .idle_reg = BD718XX_REG_BUCK1_VOLT_IDLE,
761 .idle_mask = DVS_BUCK_RUN_MASK,
762 .suspend_reg = BD718XX_REG_BUCK1_VOLT_SUSP,
763 .suspend_mask = DVS_BUCK_RUN_MASK,
764 },
765 .init = {
766 .reg = BD718XX_REG_BUCK1_CTRL,
767 .mask = BD718XX_BUCK_SEL,
768 .val = BD718XX_BUCK_SEL,
769 },
770 },
771 {
772 .desc = {
773 .name = "buck2",
774 .of_match = of_match_ptr("BUCK2"),
775 .regulators_node = of_match_ptr("regulators"),
776 .id = BD718XX_BUCK2,
777 .type = REGULATOR_VOLTAGE,
778 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
779 .linear_ranges = bd718xx_dvs_buck_volts,
780 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
781 .vsel_reg = BD718XX_REG_BUCK2_VOLT_RUN,
782 .vsel_mask = DVS_BUCK_RUN_MASK,
783 .enable_reg = BD718XX_REG_BUCK2_CTRL,
784 .enable_mask = BD718XX_BUCK_EN,
785 .enable_time = BD71847_BUCK2_STARTUP_TIME,
786 .ramp_delay_table = bd718xx_ramp_delay,
787 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
788 .ramp_reg = BD718XX_REG_BUCK2_CTRL,
789 .ramp_mask = BUCK_RAMPRATE_MASK,
790 .owner = THIS_MODULE,
791 .of_parse_cb = buck_set_hw_dvs_levels,
792 },
793 .dvs = {
794 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE,
795 .run_reg = BD718XX_REG_BUCK2_VOLT_RUN,
796 .run_mask = DVS_BUCK_RUN_MASK,
797 .idle_reg = BD718XX_REG_BUCK2_VOLT_IDLE,
798 .idle_mask = DVS_BUCK_RUN_MASK,
799 },
800 .init = {
801 .reg = BD718XX_REG_BUCK2_CTRL,
802 .mask = BD718XX_BUCK_SEL,
803 .val = BD718XX_BUCK_SEL,
804 },
805 },
806 {
807 .desc = {
808 .name = "buck3",
809 .of_match = of_match_ptr("BUCK3"),
810 .regulators_node = of_match_ptr("regulators"),
811 .id = BD718XX_BUCK3,
812 .type = REGULATOR_VOLTAGE,
813 .n_voltages = BD71847_BUCK3_VOLTAGE_NUM,
814 .linear_ranges = bd71847_buck3_volts,
815 .n_linear_ranges =
816 ARRAY_SIZE(bd71847_buck3_volts),
817 .vsel_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
818 .vsel_mask = BD718XX_1ST_NODVS_BUCK_MASK,
819 .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
820 .vsel_range_mask = BD71847_BUCK3_RANGE_MASK,
821 .linear_range_selectors_bitfield = bd71847_buck3_volt_range_sel,
822 .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
823 .enable_mask = BD718XX_BUCK_EN,
824 .enable_time = BD71847_BUCK3_STARTUP_TIME,
825 .owner = THIS_MODULE,
826 },
827 .init = {
828 .reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
829 .mask = BD718XX_BUCK_SEL,
830 .val = BD718XX_BUCK_SEL,
831 },
832 },
833 {
834 .desc = {
835 .name = "buck4",
836 .of_match = of_match_ptr("BUCK4"),
837 .regulators_node = of_match_ptr("regulators"),
838 .id = BD718XX_BUCK4,
839 .type = REGULATOR_VOLTAGE,
840 .n_voltages = BD71847_BUCK4_VOLTAGE_NUM,
841 .linear_ranges = bd71847_buck4_volts,
842 .n_linear_ranges =
843 ARRAY_SIZE(bd71847_buck4_volts),
844 .enable_reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
845 .vsel_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
846 .vsel_mask = BD71847_BUCK4_MASK,
847 .vsel_range_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
848 .vsel_range_mask = BD71847_BUCK4_RANGE_MASK,
849 .linear_range_selectors_bitfield = bd71847_buck4_volt_range_sel,
850 .enable_mask = BD718XX_BUCK_EN,
851 .enable_time = BD71847_BUCK4_STARTUP_TIME,
852 .owner = THIS_MODULE,
853 },
854 .init = {
855 .reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
856 .mask = BD718XX_BUCK_SEL,
857 .val = BD718XX_BUCK_SEL,
858 },
859 },
860 {
861 .desc = {
862 .name = "buck5",
863 .of_match = of_match_ptr("BUCK5"),
864 .regulators_node = of_match_ptr("regulators"),
865 .id = BD718XX_BUCK5,
866 .type = REGULATOR_VOLTAGE,
867 .volt_table = &bd718xx_3rd_nodvs_buck_volts[0],
868 .n_voltages = ARRAY_SIZE(bd718xx_3rd_nodvs_buck_volts),
869 .vsel_reg = BD718XX_REG_3RD_NODVS_BUCK_VOLT,
870 .vsel_mask = BD718XX_3RD_NODVS_BUCK_MASK,
871 .enable_reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
872 .enable_mask = BD718XX_BUCK_EN,
873 .enable_time = BD71847_BUCK5_STARTUP_TIME,
874 .owner = THIS_MODULE,
875 },
876 .init = {
877 .reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
878 .mask = BD718XX_BUCK_SEL,
879 .val = BD718XX_BUCK_SEL,
880 },
881 },
882 {
883 .desc = {
884 .name = "buck6",
885 .of_match = of_match_ptr("BUCK6"),
886 .regulators_node = of_match_ptr("regulators"),
887 .id = BD718XX_BUCK6,
888 .type = REGULATOR_VOLTAGE,
889 .n_voltages = BD718XX_4TH_NODVS_BUCK_VOLTAGE_NUM,
890 .linear_ranges = bd718xx_4th_nodvs_buck_volts,
891 .n_linear_ranges =
892 ARRAY_SIZE(bd718xx_4th_nodvs_buck_volts),
893 .vsel_reg = BD718XX_REG_4TH_NODVS_BUCK_VOLT,
894 .vsel_mask = BD718XX_4TH_NODVS_BUCK_MASK,
895 .enable_reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
896 .enable_mask = BD718XX_BUCK_EN,
897 .enable_time = BD71847_BUCK6_STARTUP_TIME,
898 .owner = THIS_MODULE,
899 },
900 .init = {
901 .reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
902 .mask = BD718XX_BUCK_SEL,
903 .val = BD718XX_BUCK_SEL,
904 },
905 },
906 {
907 .desc = {
908 .name = "ldo1",
909 .of_match = of_match_ptr("LDO1"),
910 .regulators_node = of_match_ptr("regulators"),
911 .id = BD718XX_LDO1,
912 .type = REGULATOR_VOLTAGE,
913 .n_voltages = BD718XX_LDO1_VOLTAGE_NUM,
914 .linear_ranges = bd718xx_ldo1_volts,
915 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo1_volts),
916 .vsel_reg = BD718XX_REG_LDO1_VOLT,
917 .vsel_mask = BD718XX_LDO1_MASK,
918 .vsel_range_reg = BD718XX_REG_LDO1_VOLT,
919 .vsel_range_mask = BD718XX_LDO1_RANGE_MASK,
920 .linear_range_selectors_bitfield = bd718xx_ldo1_volt_range_sel,
921 .enable_reg = BD718XX_REG_LDO1_VOLT,
922 .enable_mask = BD718XX_LDO_EN,
923 .enable_time = BD71847_LDO1_STARTUP_TIME,
924 .owner = THIS_MODULE,
925 },
926 .init = {
927 .reg = BD718XX_REG_LDO1_VOLT,
928 .mask = BD718XX_LDO_SEL,
929 .val = BD718XX_LDO_SEL,
930 },
931 },
932 {
933 .desc = {
934 .name = "ldo2",
935 .of_match = of_match_ptr("LDO2"),
936 .regulators_node = of_match_ptr("regulators"),
937 .id = BD718XX_LDO2,
938 .type = REGULATOR_VOLTAGE,
939 .volt_table = &ldo_2_volts[0],
940 .vsel_reg = BD718XX_REG_LDO2_VOLT,
941 .vsel_mask = BD718XX_LDO2_MASK,
942 .n_voltages = ARRAY_SIZE(ldo_2_volts),
943 .enable_reg = BD718XX_REG_LDO2_VOLT,
944 .enable_mask = BD718XX_LDO_EN,
945 .enable_time = BD71847_LDO2_STARTUP_TIME,
946 .owner = THIS_MODULE,
947 },
948 .init = {
949 .reg = BD718XX_REG_LDO2_VOLT,
950 .mask = BD718XX_LDO_SEL,
951 .val = BD718XX_LDO_SEL,
952 },
953 },
954 {
955 .desc = {
956 .name = "ldo3",
957 .of_match = of_match_ptr("LDO3"),
958 .regulators_node = of_match_ptr("regulators"),
959 .id = BD718XX_LDO3,
960 .type = REGULATOR_VOLTAGE,
961 .n_voltages = BD718XX_LDO3_VOLTAGE_NUM,
962 .linear_ranges = bd718xx_ldo3_volts,
963 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo3_volts),
964 .vsel_reg = BD718XX_REG_LDO3_VOLT,
965 .vsel_mask = BD718XX_LDO3_MASK,
966 .enable_reg = BD718XX_REG_LDO3_VOLT,
967 .enable_mask = BD718XX_LDO_EN,
968 .enable_time = BD71847_LDO3_STARTUP_TIME,
969 .owner = THIS_MODULE,
970 },
971 .init = {
972 .reg = BD718XX_REG_LDO3_VOLT,
973 .mask = BD718XX_LDO_SEL,
974 .val = BD718XX_LDO_SEL,
975 },
976 },
977 {
978 .desc = {
979 .name = "ldo4",
980 .of_match = of_match_ptr("LDO4"),
981 .regulators_node = of_match_ptr("regulators"),
982 .id = BD718XX_LDO4,
983 .type = REGULATOR_VOLTAGE,
984 .n_voltages = BD718XX_LDO4_VOLTAGE_NUM,
985 .linear_ranges = bd718xx_ldo4_volts,
986 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo4_volts),
987 .vsel_reg = BD718XX_REG_LDO4_VOLT,
988 .vsel_mask = BD718XX_LDO4_MASK,
989 .enable_reg = BD718XX_REG_LDO4_VOLT,
990 .enable_mask = BD718XX_LDO_EN,
991 .enable_time = BD71847_LDO4_STARTUP_TIME,
992 .owner = THIS_MODULE,
993 },
994 .init = {
995 .reg = BD718XX_REG_LDO4_VOLT,
996 .mask = BD718XX_LDO_SEL,
997 .val = BD718XX_LDO_SEL,
998 },
999 },
1000 {
1001 .desc = {
1002 .name = "ldo5",
1003 .of_match = of_match_ptr("LDO5"),
1004 .regulators_node = of_match_ptr("regulators"),
1005 .id = BD718XX_LDO5,
1006 .type = REGULATOR_VOLTAGE,
1007 .n_voltages = BD71847_LDO5_VOLTAGE_NUM,
1008 .linear_ranges = bd71847_ldo5_volts,
1009 .n_linear_ranges = ARRAY_SIZE(bd71847_ldo5_volts),
1010 .vsel_reg = BD718XX_REG_LDO5_VOLT,
1011 .vsel_mask = BD71847_LDO5_MASK,
1012 .vsel_range_reg = BD718XX_REG_LDO5_VOLT,
1013 .vsel_range_mask = BD71847_LDO5_RANGE_MASK,
1014 .linear_range_selectors_bitfield = bd71847_ldo5_volt_range_sel,
1015 .enable_reg = BD718XX_REG_LDO5_VOLT,
1016 .enable_mask = BD718XX_LDO_EN,
1017 .enable_time = BD71847_LDO5_STARTUP_TIME,
1018 .owner = THIS_MODULE,
1019 },
1020 .init = {
1021 .reg = BD718XX_REG_LDO5_VOLT,
1022 .mask = BD718XX_LDO_SEL,
1023 .val = BD718XX_LDO_SEL,
1024 },
1025 },
1026 {
1027 .desc = {
1028 .name = "ldo6",
1029 .of_match = of_match_ptr("LDO6"),
1030 .regulators_node = of_match_ptr("regulators"),
1031 .id = BD718XX_LDO6,
1032 .type = REGULATOR_VOLTAGE,
1033 .n_voltages = BD718XX_LDO6_VOLTAGE_NUM,
1034 .linear_ranges = bd718xx_ldo6_volts,
1035 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo6_volts),
1036 /* LDO6 is supplied by buck5 */
1037 .supply_name = "buck5",
1038 .vsel_reg = BD718XX_REG_LDO6_VOLT,
1039 .vsel_mask = BD718XX_LDO6_MASK,
1040 .enable_reg = BD718XX_REG_LDO6_VOLT,
1041 .enable_mask = BD718XX_LDO_EN,
1042 .enable_time = BD71847_LDO6_STARTUP_TIME,
1043 .owner = THIS_MODULE,
1044 },
1045 .init = {
1046 .reg = BD718XX_REG_LDO6_VOLT,
1047 .mask = BD718XX_LDO_SEL,
1048 .val = BD718XX_LDO_SEL,
1049 },
1050 },
1051};
1052
1053static const struct regulator_ops *bd71837_swcontrol_ops[] = {
1054 &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
1055 &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
1056 &bd71837_pickable_range_buck_ops, &bd71837_buck_regulator_ops,
1057 &bd71837_buck_regulator_nolinear_ops, &bd71837_buck_regulator_ops,
1058 &bd71837_pickable_range_ldo_ops, &bd71837_ldo_regulator_nolinear_ops,
1059 &bd71837_ldo_regulator_ops, &bd71837_ldo_regulator_ops,
1060 &bd71837_ldo_regulator_ops, &bd71837_ldo_regulator_ops,
1061 &bd71837_ldo_regulator_ops,
1062};
1063
1064static const struct regulator_ops *bd71837_hwcontrol_ops[] = {
1065 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
1066 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
1067 &bd71837_buck34_ops_hwctrl, &bd71837_buck34_ops_hwctrl,
1068 &BD718XX_HWOPNAME(bd71837_pickable_range_buck_ops),
1069 &BD718XX_HWOPNAME(bd71837_buck_regulator_ops),
1070 &BD718XX_HWOPNAME(bd71837_buck_regulator_nolinear_ops),
1071 &BD718XX_HWOPNAME(bd71837_buck_regulator_ops),
1072 &BD718XX_HWOPNAME(bd71837_pickable_range_ldo_ops),
1073 &BD718XX_HWOPNAME(bd71837_ldo_regulator_nolinear_ops),
1074 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1075 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1076 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1077 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1078 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1079};
1080
1081static struct bd718xx_regulator_data bd71837_regulators[] = {
1082 {
1083 .desc = {
1084 .name = "buck1",
1085 .of_match = of_match_ptr("BUCK1"),
1086 .regulators_node = of_match_ptr("regulators"),
1087 .id = BD718XX_BUCK1,
1088 .type = REGULATOR_VOLTAGE,
1089 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1090 .linear_ranges = bd718xx_dvs_buck_volts,
1091 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1092 .vsel_reg = BD718XX_REG_BUCK1_VOLT_RUN,
1093 .vsel_mask = DVS_BUCK_RUN_MASK,
1094 .enable_reg = BD718XX_REG_BUCK1_CTRL,
1095 .enable_mask = BD718XX_BUCK_EN,
1096 .enable_time = BD71837_BUCK1_STARTUP_TIME,
1097 .ramp_delay_table = bd718xx_ramp_delay,
1098 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1099 .ramp_reg = BD718XX_REG_BUCK1_CTRL,
1100 .ramp_mask = BUCK_RAMPRATE_MASK,
1101 .owner = THIS_MODULE,
1102 .of_parse_cb = buck_set_hw_dvs_levels,
1103 },
1104 .dvs = {
1105 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE |
1106 ROHM_DVS_LEVEL_SUSPEND,
1107 .run_reg = BD718XX_REG_BUCK1_VOLT_RUN,
1108 .run_mask = DVS_BUCK_RUN_MASK,
1109 .idle_reg = BD718XX_REG_BUCK1_VOLT_IDLE,
1110 .idle_mask = DVS_BUCK_RUN_MASK,
1111 .suspend_reg = BD718XX_REG_BUCK1_VOLT_SUSP,
1112 .suspend_mask = DVS_BUCK_RUN_MASK,
1113 },
1114 .init = {
1115 .reg = BD718XX_REG_BUCK1_CTRL,
1116 .mask = BD718XX_BUCK_SEL,
1117 .val = BD718XX_BUCK_SEL,
1118 },
1119 },
1120 {
1121 .desc = {
1122 .name = "buck2",
1123 .of_match = of_match_ptr("BUCK2"),
1124 .regulators_node = of_match_ptr("regulators"),
1125 .id = BD718XX_BUCK2,
1126 .type = REGULATOR_VOLTAGE,
1127 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1128 .linear_ranges = bd718xx_dvs_buck_volts,
1129 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1130 .vsel_reg = BD718XX_REG_BUCK2_VOLT_RUN,
1131 .vsel_mask = DVS_BUCK_RUN_MASK,
1132 .enable_reg = BD718XX_REG_BUCK2_CTRL,
1133 .enable_mask = BD718XX_BUCK_EN,
1134 .enable_time = BD71837_BUCK2_STARTUP_TIME,
1135 .ramp_delay_table = bd718xx_ramp_delay,
1136 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1137 .ramp_reg = BD718XX_REG_BUCK2_CTRL,
1138 .ramp_mask = BUCK_RAMPRATE_MASK,
1139 .owner = THIS_MODULE,
1140 .of_parse_cb = buck_set_hw_dvs_levels,
1141 },
1142 .dvs = {
1143 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE,
1144 .run_reg = BD718XX_REG_BUCK2_VOLT_RUN,
1145 .run_mask = DVS_BUCK_RUN_MASK,
1146 .idle_reg = BD718XX_REG_BUCK2_VOLT_IDLE,
1147 .idle_mask = DVS_BUCK_RUN_MASK,
1148 },
1149 .init = {
1150 .reg = BD718XX_REG_BUCK2_CTRL,
1151 .mask = BD718XX_BUCK_SEL,
1152 .val = BD718XX_BUCK_SEL,
1153 },
1154 },
1155 {
1156 .desc = {
1157 .name = "buck3",
1158 .of_match = of_match_ptr("BUCK3"),
1159 .regulators_node = of_match_ptr("regulators"),
1160 .id = BD718XX_BUCK3,
1161 .type = REGULATOR_VOLTAGE,
1162 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1163 .linear_ranges = bd718xx_dvs_buck_volts,
1164 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1165 .vsel_reg = BD71837_REG_BUCK3_VOLT_RUN,
1166 .vsel_mask = DVS_BUCK_RUN_MASK,
1167 .enable_reg = BD71837_REG_BUCK3_CTRL,
1168 .enable_mask = BD718XX_BUCK_EN,
1169 .enable_time = BD71837_BUCK3_STARTUP_TIME,
1170 .ramp_delay_table = bd718xx_ramp_delay,
1171 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1172 .ramp_reg = BD71837_REG_BUCK3_CTRL,
1173 .ramp_mask = BUCK_RAMPRATE_MASK,
1174 .owner = THIS_MODULE,
1175 .of_parse_cb = buck_set_hw_dvs_levels,
1176 },
1177 .dvs = {
1178 .level_map = ROHM_DVS_LEVEL_RUN,
1179 .run_reg = BD71837_REG_BUCK3_VOLT_RUN,
1180 .run_mask = DVS_BUCK_RUN_MASK,
1181 },
1182 .init = {
1183 .reg = BD71837_REG_BUCK3_CTRL,
1184 .mask = BD718XX_BUCK_SEL,
1185 .val = BD718XX_BUCK_SEL,
1186 },
1187 },
1188 {
1189 .desc = {
1190 .name = "buck4",
1191 .of_match = of_match_ptr("BUCK4"),
1192 .regulators_node = of_match_ptr("regulators"),
1193 .id = BD718XX_BUCK4,
1194 .type = REGULATOR_VOLTAGE,
1195 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1196 .linear_ranges = bd718xx_dvs_buck_volts,
1197 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1198 .vsel_reg = BD71837_REG_BUCK4_VOLT_RUN,
1199 .vsel_mask = DVS_BUCK_RUN_MASK,
1200 .enable_reg = BD71837_REG_BUCK4_CTRL,
1201 .enable_mask = BD718XX_BUCK_EN,
1202 .enable_time = BD71837_BUCK4_STARTUP_TIME,
1203 .ramp_delay_table = bd718xx_ramp_delay,
1204 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1205 .ramp_reg = BD71837_REG_BUCK4_CTRL,
1206 .ramp_mask = BUCK_RAMPRATE_MASK,
1207 .owner = THIS_MODULE,
1208 .of_parse_cb = buck_set_hw_dvs_levels,
1209 },
1210 .dvs = {
1211 .level_map = ROHM_DVS_LEVEL_RUN,
1212 .run_reg = BD71837_REG_BUCK4_VOLT_RUN,
1213 .run_mask = DVS_BUCK_RUN_MASK,
1214 },
1215 .init = {
1216 .reg = BD71837_REG_BUCK4_CTRL,
1217 .mask = BD718XX_BUCK_SEL,
1218 .val = BD718XX_BUCK_SEL,
1219 },
1220 },
1221 {
1222 .desc = {
1223 .name = "buck5",
1224 .of_match = of_match_ptr("BUCK5"),
1225 .regulators_node = of_match_ptr("regulators"),
1226 .id = BD718XX_BUCK5,
1227 .type = REGULATOR_VOLTAGE,
1228 .n_voltages = BD71837_BUCK5_VOLTAGE_NUM,
1229 .linear_ranges = bd71837_buck5_volts,
1230 .n_linear_ranges =
1231 ARRAY_SIZE(bd71837_buck5_volts),
1232 .vsel_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
1233 .vsel_mask = BD71837_BUCK5_MASK,
1234 .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
1235 .vsel_range_mask = BD71837_BUCK5_RANGE_MASK,
1236 .linear_range_selectors_bitfield = bd71837_buck5_volt_range_sel,
1237 .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
1238 .enable_mask = BD718XX_BUCK_EN,
1239 .enable_time = BD71837_BUCK5_STARTUP_TIME,
1240 .owner = THIS_MODULE,
1241 },
1242 .init = {
1243 .reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
1244 .mask = BD718XX_BUCK_SEL,
1245 .val = BD718XX_BUCK_SEL,
1246 },
1247 },
1248 {
1249 .desc = {
1250 .name = "buck6",
1251 .of_match = of_match_ptr("BUCK6"),
1252 .regulators_node = of_match_ptr("regulators"),
1253 .id = BD718XX_BUCK6,
1254 .type = REGULATOR_VOLTAGE,
1255 .n_voltages = BD71837_BUCK6_VOLTAGE_NUM,
1256 .linear_ranges = bd71837_buck6_volts,
1257 .n_linear_ranges =
1258 ARRAY_SIZE(bd71837_buck6_volts),
1259 .vsel_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
1260 .vsel_mask = BD71837_BUCK6_MASK,
1261 .enable_reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
1262 .enable_mask = BD718XX_BUCK_EN,
1263 .enable_time = BD71837_BUCK6_STARTUP_TIME,
1264 .owner = THIS_MODULE,
1265 },
1266 .init = {
1267 .reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
1268 .mask = BD718XX_BUCK_SEL,
1269 .val = BD718XX_BUCK_SEL,
1270 },
1271 },
1272 {
1273 .desc = {
1274 .name = "buck7",
1275 .of_match = of_match_ptr("BUCK7"),
1276 .regulators_node = of_match_ptr("regulators"),
1277 .id = BD718XX_BUCK7,
1278 .type = REGULATOR_VOLTAGE,
1279 .volt_table = &bd718xx_3rd_nodvs_buck_volts[0],
1280 .n_voltages = ARRAY_SIZE(bd718xx_3rd_nodvs_buck_volts),
1281 .vsel_reg = BD718XX_REG_3RD_NODVS_BUCK_VOLT,
1282 .vsel_mask = BD718XX_3RD_NODVS_BUCK_MASK,
1283 .enable_reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
1284 .enable_mask = BD718XX_BUCK_EN,
1285 .enable_time = BD71837_BUCK7_STARTUP_TIME,
1286 .owner = THIS_MODULE,
1287 },
1288 .init = {
1289 .reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
1290 .mask = BD718XX_BUCK_SEL,
1291 .val = BD718XX_BUCK_SEL,
1292 },
1293 },
1294 {
1295 .desc = {
1296 .name = "buck8",
1297 .of_match = of_match_ptr("BUCK8"),
1298 .regulators_node = of_match_ptr("regulators"),
1299 .id = BD718XX_BUCK8,
1300 .type = REGULATOR_VOLTAGE,
1301 .n_voltages = BD718XX_4TH_NODVS_BUCK_VOLTAGE_NUM,
1302 .linear_ranges = bd718xx_4th_nodvs_buck_volts,
1303 .n_linear_ranges =
1304 ARRAY_SIZE(bd718xx_4th_nodvs_buck_volts),
1305 .vsel_reg = BD718XX_REG_4TH_NODVS_BUCK_VOLT,
1306 .vsel_mask = BD718XX_4TH_NODVS_BUCK_MASK,
1307 .enable_reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
1308 .enable_mask = BD718XX_BUCK_EN,
1309 .enable_time = BD71837_BUCK8_STARTUP_TIME,
1310 .owner = THIS_MODULE,
1311 },
1312 .init = {
1313 .reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
1314 .mask = BD718XX_BUCK_SEL,
1315 .val = BD718XX_BUCK_SEL,
1316 },
1317 },
1318 {
1319 .desc = {
1320 .name = "ldo1",
1321 .of_match = of_match_ptr("LDO1"),
1322 .regulators_node = of_match_ptr("regulators"),
1323 .id = BD718XX_LDO1,
1324 .type = REGULATOR_VOLTAGE,
1325 .n_voltages = BD718XX_LDO1_VOLTAGE_NUM,
1326 .linear_ranges = bd718xx_ldo1_volts,
1327 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo1_volts),
1328 .vsel_reg = BD718XX_REG_LDO1_VOLT,
1329 .vsel_mask = BD718XX_LDO1_MASK,
1330 .vsel_range_reg = BD718XX_REG_LDO1_VOLT,
1331 .vsel_range_mask = BD718XX_LDO1_RANGE_MASK,
1332 .linear_range_selectors_bitfield = bd718xx_ldo1_volt_range_sel,
1333 .enable_reg = BD718XX_REG_LDO1_VOLT,
1334 .enable_mask = BD718XX_LDO_EN,
1335 .enable_time = BD71837_LDO1_STARTUP_TIME,
1336 .owner = THIS_MODULE,
1337 },
1338 .init = {
1339 .reg = BD718XX_REG_LDO1_VOLT,
1340 .mask = BD718XX_LDO_SEL,
1341 .val = BD718XX_LDO_SEL,
1342 },
1343 },
1344 {
1345 .desc = {
1346 .name = "ldo2",
1347 .of_match = of_match_ptr("LDO2"),
1348 .regulators_node = of_match_ptr("regulators"),
1349 .id = BD718XX_LDO2,
1350 .type = REGULATOR_VOLTAGE,
1351 .volt_table = &ldo_2_volts[0],
1352 .vsel_reg = BD718XX_REG_LDO2_VOLT,
1353 .vsel_mask = BD718XX_LDO2_MASK,
1354 .n_voltages = ARRAY_SIZE(ldo_2_volts),
1355 .enable_reg = BD718XX_REG_LDO2_VOLT,
1356 .enable_mask = BD718XX_LDO_EN,
1357 .enable_time = BD71837_LDO2_STARTUP_TIME,
1358 .owner = THIS_MODULE,
1359 },
1360 .init = {
1361 .reg = BD718XX_REG_LDO2_VOLT,
1362 .mask = BD718XX_LDO_SEL,
1363 .val = BD718XX_LDO_SEL,
1364 },
1365 },
1366 {
1367 .desc = {
1368 .name = "ldo3",
1369 .of_match = of_match_ptr("LDO3"),
1370 .regulators_node = of_match_ptr("regulators"),
1371 .id = BD718XX_LDO3,
1372 .type = REGULATOR_VOLTAGE,
1373 .n_voltages = BD718XX_LDO3_VOLTAGE_NUM,
1374 .linear_ranges = bd718xx_ldo3_volts,
1375 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo3_volts),
1376 .vsel_reg = BD718XX_REG_LDO3_VOLT,
1377 .vsel_mask = BD718XX_LDO3_MASK,
1378 .enable_reg = BD718XX_REG_LDO3_VOLT,
1379 .enable_mask = BD718XX_LDO_EN,
1380 .enable_time = BD71837_LDO3_STARTUP_TIME,
1381 .owner = THIS_MODULE,
1382 },
1383 .init = {
1384 .reg = BD718XX_REG_LDO3_VOLT,
1385 .mask = BD718XX_LDO_SEL,
1386 .val = BD718XX_LDO_SEL,
1387 },
1388 },
1389 {
1390 .desc = {
1391 .name = "ldo4",
1392 .of_match = of_match_ptr("LDO4"),
1393 .regulators_node = of_match_ptr("regulators"),
1394 .id = BD718XX_LDO4,
1395 .type = REGULATOR_VOLTAGE,
1396 .n_voltages = BD718XX_LDO4_VOLTAGE_NUM,
1397 .linear_ranges = bd718xx_ldo4_volts,
1398 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo4_volts),
1399 .vsel_reg = BD718XX_REG_LDO4_VOLT,
1400 .vsel_mask = BD718XX_LDO4_MASK,
1401 .enable_reg = BD718XX_REG_LDO4_VOLT,
1402 .enable_mask = BD718XX_LDO_EN,
1403 .enable_time = BD71837_LDO4_STARTUP_TIME,
1404 .owner = THIS_MODULE,
1405 },
1406 .init = {
1407 .reg = BD718XX_REG_LDO4_VOLT,
1408 .mask = BD718XX_LDO_SEL,
1409 .val = BD718XX_LDO_SEL,
1410 },
1411 },
1412 {
1413 .desc = {
1414 .name = "ldo5",
1415 .of_match = of_match_ptr("LDO5"),
1416 .regulators_node = of_match_ptr("regulators"),
1417 .id = BD718XX_LDO5,
1418 .type = REGULATOR_VOLTAGE,
1419 .n_voltages = BD71837_LDO5_VOLTAGE_NUM,
1420 .linear_ranges = bd71837_ldo5_volts,
1421 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo5_volts),
1422 /* LDO5 is supplied by buck6 */
1423 .supply_name = "buck6",
1424 .vsel_reg = BD718XX_REG_LDO5_VOLT,
1425 .vsel_mask = BD71837_LDO5_MASK,
1426 .enable_reg = BD718XX_REG_LDO5_VOLT,
1427 .enable_mask = BD718XX_LDO_EN,
1428 .enable_time = BD71837_LDO5_STARTUP_TIME,
1429 .owner = THIS_MODULE,
1430 },
1431 .init = {
1432 .reg = BD718XX_REG_LDO5_VOLT,
1433 .mask = BD718XX_LDO_SEL,
1434 .val = BD718XX_LDO_SEL,
1435 },
1436 .additional_inits = bd71837_ldo5_inits,
1437 .additional_init_amnt = ARRAY_SIZE(bd71837_ldo5_inits),
1438 },
1439 {
1440 .desc = {
1441 .name = "ldo6",
1442 .of_match = of_match_ptr("LDO6"),
1443 .regulators_node = of_match_ptr("regulators"),
1444 .id = BD718XX_LDO6,
1445 .type = REGULATOR_VOLTAGE,
1446 .n_voltages = BD718XX_LDO6_VOLTAGE_NUM,
1447 .linear_ranges = bd718xx_ldo6_volts,
1448 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo6_volts),
1449 /* LDO6 is supplied by buck7 */
1450 .supply_name = "buck7",
1451 .vsel_reg = BD718XX_REG_LDO6_VOLT,
1452 .vsel_mask = BD718XX_LDO6_MASK,
1453 .enable_reg = BD718XX_REG_LDO6_VOLT,
1454 .enable_mask = BD718XX_LDO_EN,
1455 .enable_time = BD71837_LDO6_STARTUP_TIME,
1456 .owner = THIS_MODULE,
1457 },
1458 .init = {
1459 .reg = BD718XX_REG_LDO6_VOLT,
1460 .mask = BD718XX_LDO_SEL,
1461 .val = BD718XX_LDO_SEL,
1462 },
1463 .additional_inits = bd71837_ldo6_inits,
1464 .additional_init_amnt = ARRAY_SIZE(bd71837_ldo6_inits),
1465 },
1466 {
1467 .desc = {
1468 .name = "ldo7",
1469 .of_match = of_match_ptr("LDO7"),
1470 .regulators_node = of_match_ptr("regulators"),
1471 .id = BD718XX_LDO7,
1472 .type = REGULATOR_VOLTAGE,
1473 .n_voltages = BD71837_LDO7_VOLTAGE_NUM,
1474 .linear_ranges = bd71837_ldo7_volts,
1475 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo7_volts),
1476 .vsel_reg = BD71837_REG_LDO7_VOLT,
1477 .vsel_mask = BD71837_LDO7_MASK,
1478 .enable_reg = BD71837_REG_LDO7_VOLT,
1479 .enable_mask = BD718XX_LDO_EN,
1480 .enable_time = BD71837_LDO7_STARTUP_TIME,
1481 .owner = THIS_MODULE,
1482 },
1483 .init = {
1484 .reg = BD71837_REG_LDO7_VOLT,
1485 .mask = BD718XX_LDO_SEL,
1486 .val = BD718XX_LDO_SEL,
1487 },
1488 },
1489};
1490
1491static void mark_hw_controlled(struct device *dev, struct device_node *np,
1492 struct bd718xx_regulator_data *reg_data,
1493 unsigned int num_reg_data, int *info)
1494{
1495 int i;
1496
1497 for (i = 1; i <= num_reg_data; i++) {
1498 if (!of_node_name_eq(np, reg_data[i-1].desc.of_match))
1499 continue;
1500
1501 *info |= 1 << (i - 1);
1502 dev_dbg(dev, "regulator %d runlevel controlled\n", i);
1503 return;
1504 }
1505 dev_warn(dev, "Bad regulator node\n");
1506}
1507
1508/*
1509 * Setups where regulator (especially the buck8) output voltage is scaled
1510 * by adding external connection where some other regulator output is connected
1511 * to feedback-pin (over suitable resistors) is getting popular amongst users
1512 * of BD71837. (This allows for example scaling down the buck8 voltages to suit
1513 * lover GPU voltages for projects where buck8 is (ab)used to supply power
1514 * for GPU. Additionally some setups do allow DVS for buck8 but as this do
1515 * produce voltage spikes the HW must be evaluated to be able to survive this
1516 * - hence I keep the DVS disabled for non DVS bucks by default. I don't want
1517 * to help you burn your proto board)
1518 *
1519 * So we allow describing this external connection from DT and scale the
1520 * voltages accordingly. This is what the connection should look like:
1521 *
1522 * |------------|
1523 * | buck 8 |-------+----->Vout
1524 * | | |
1525 * |------------| |
1526 * | FB pin |
1527 * | |
1528 * +-------+--R2---+
1529 * |
1530 * R1
1531 * |
1532 * V FB-pull-up
1533 *
1534 * Here the buck output is sifted according to formula:
1535 *
1536 * Vout_o = Vo - (Vpu - Vo)*R2/R1
1537 * Linear_step = step_orig*(R1+R2)/R1
1538 *
1539 * where:
1540 * Vout_o is adjusted voltage output at vsel reg value 0
1541 * Vo is original voltage output at vsel reg value 0
1542 * Vpu is the pull-up voltage V FB-pull-up in the picture
1543 * R1 and R2 are resistor values.
1544 *
1545 * As a real world example for buck8 and a specific GPU:
1546 * VLDO = 1.6V (used as FB-pull-up)
1547 * R1 = 1000ohms
1548 * R2 = 150ohms
1549 * VSEL 0x0 => 0.8V – (VLDO – 0.8) * R2 / R1 = 0.68V
1550 * Linear Step = 10mV * (R1 + R2) / R1 = 11.5mV
1551 */
1552static int setup_feedback_loop(struct device *dev, struct device_node *np,
1553 struct bd718xx_regulator_data *reg_data,
1554 unsigned int num_reg_data, int fb_uv)
1555{
1556 int i, r1, r2, ret;
1557
1558 /*
1559 * We do adjust the values in the global desc based on DT settings.
1560 * This may not be best approach as it can cause problems if more than
1561 * one PMIC is controlled from same processor. I don't see such use-case
1562 * for BD718x7 now - so we spare some bits.
1563 *
1564 * If this will point out to be a problem - then we can allocate new
1565 * bd718xx_regulator_data array at probe and just use the global
1566 * array as a template where we copy initial values. Then we can
1567 * use allocated descs for regultor registration and do IC specific
1568 * modifications to this copy while leaving other PMICs untouched. But
1569 * that means allocating new array for each PMIC - and currently I see
1570 * no need for that.
1571 */
1572
1573 for (i = 0; i < num_reg_data; i++) {
1574 struct regulator_desc *desc = ®_data[i].desc;
1575 int j;
1576
1577 if (!of_node_name_eq(np, desc->of_match))
1578 continue;
1579
1580 /* The feedback loop connection does not make sense for LDOs */
1581 if (desc->id >= BD718XX_LDO1)
1582 return -EINVAL;
1583
1584 ret = of_property_read_u32(np, "rohm,feedback-pull-up-r1-ohms",
1585 &r1);
1586 if (ret)
1587 return ret;
1588
1589 if (!r1)
1590 return -EINVAL;
1591
1592 ret = of_property_read_u32(np, "rohm,feedback-pull-up-r2-ohms",
1593 &r2);
1594 if (ret)
1595 return ret;
1596
1597 if (desc->n_linear_ranges && desc->linear_ranges) {
1598 struct linear_range *new;
1599
1600 new = devm_kzalloc(dev, desc->n_linear_ranges *
1601 sizeof(struct linear_range),
1602 GFP_KERNEL);
1603 if (!new)
1604 return -ENOMEM;
1605
1606 for (j = 0; j < desc->n_linear_ranges; j++) {
1607 int min = desc->linear_ranges[j].min;
1608 int step = desc->linear_ranges[j].step;
1609
1610 min -= (fb_uv - min)*r2/r1;
1611 step = step * (r1 + r2);
1612 step /= r1;
1613
1614 new[j].min = min;
1615 new[j].step = step;
1616
1617 dev_dbg(dev, "%s: old range min %d, step %d\n",
1618 desc->name, desc->linear_ranges[j].min,
1619 desc->linear_ranges[j].step);
1620 dev_dbg(dev, "new range min %d, step %d\n", min,
1621 step);
1622 }
1623 desc->linear_ranges = new;
1624 }
1625 dev_dbg(dev, "regulator '%s' has FB pull-up configured\n",
1626 desc->name);
1627
1628 return 0;
1629 }
1630
1631 return -ENODEV;
1632}
1633
1634static int get_special_regulators(struct device *dev,
1635 struct bd718xx_regulator_data *reg_data,
1636 unsigned int num_reg_data, int *info)
1637{
1638 int ret;
1639 int uv;
1640
1641 *info = 0;
1642
1643 struct device_node *nproot __free(device_node) = of_get_child_by_name(dev->of_node,
1644 "regulators");
1645 if (!nproot) {
1646 dev_err(dev, "failed to find regulators node\n");
1647 return -ENODEV;
1648 }
1649 for_each_child_of_node_scoped(nproot, np) {
1650 if (of_property_read_bool(np, "rohm,no-regulator-enable-control"))
1651 mark_hw_controlled(dev, np, reg_data, num_reg_data,
1652 info);
1653 ret = of_property_read_u32(np, "rohm,fb-pull-up-microvolt",
1654 &uv);
1655 if (ret) {
1656 if (ret == -EINVAL)
1657 continue;
1658 else
1659 return ret;
1660 }
1661
1662 ret = setup_feedback_loop(dev, np, reg_data, num_reg_data, uv);
1663 if (ret)
1664 return ret;
1665 }
1666
1667 return 0;
1668}
1669
1670static int bd718xx_probe(struct platform_device *pdev)
1671{
1672 struct regmap *regmap;
1673 struct regulator_config config = { 0 };
1674 int i, j, err, omit_enable;
1675 bool use_snvs;
1676 struct bd718xx_regulator_data *reg_data;
1677 unsigned int num_reg_data;
1678 enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
1679 const struct regulator_ops **swops, **hwops;
1680
1681 regmap = dev_get_regmap(pdev->dev.parent, NULL);
1682 if (!regmap) {
1683 dev_err(&pdev->dev, "No MFD driver data\n");
1684 return -EINVAL;
1685 }
1686
1687 switch (chip) {
1688 case ROHM_CHIP_TYPE_BD71837:
1689 reg_data = bd71837_regulators;
1690 num_reg_data = ARRAY_SIZE(bd71837_regulators);
1691 swops = &bd71837_swcontrol_ops[0];
1692 hwops = &bd71837_hwcontrol_ops[0];
1693 break;
1694 case ROHM_CHIP_TYPE_BD71847:
1695 reg_data = bd71847_regulators;
1696 num_reg_data = ARRAY_SIZE(bd71847_regulators);
1697 swops = &bd71847_swcontrol_ops[0];
1698 hwops = &bd71847_hwcontrol_ops[0];
1699 break;
1700 default:
1701 dev_err(&pdev->dev, "Unsupported chip type\n");
1702 return -EINVAL;
1703 }
1704
1705 /* Register LOCK release */
1706 err = regmap_update_bits(regmap, BD718XX_REG_REGLOCK,
1707 (REGLOCK_PWRSEQ | REGLOCK_VREG), 0);
1708 if (err)
1709 return dev_err_probe(&pdev->dev, err, "Failed to unlock PMIC\n");
1710
1711 dev_dbg(&pdev->dev, "Unlocked lock register 0x%x\n",
1712 BD718XX_REG_REGLOCK);
1713
1714 use_snvs = of_property_read_bool(pdev->dev.parent->of_node,
1715 "rohm,reset-snvs-powered");
1716
1717 /*
1718 * Change the next stage from poweroff to be READY instead of SNVS
1719 * for all reset types because OTP loading at READY will clear SEL
1720 * bit allowing HW defaults for power rails to be used
1721 */
1722 if (!use_snvs) {
1723 err = regmap_update_bits(regmap, BD718XX_REG_TRANS_COND1,
1724 BD718XX_ON_REQ_POWEROFF_MASK |
1725 BD718XX_SWRESET_POWEROFF_MASK |
1726 BD718XX_WDOG_POWEROFF_MASK |
1727 BD718XX_KEY_L_POWEROFF_MASK,
1728 BD718XX_POWOFF_TO_RDY);
1729 if (err)
1730 return dev_err_probe(&pdev->dev, err,
1731 "Failed to change reset target\n");
1732
1733 dev_dbg(&pdev->dev, "Changed all resets from SVNS to READY\n");
1734 }
1735
1736 config.dev = pdev->dev.parent;
1737 config.regmap = regmap;
1738 /*
1739 * There are cases when we want to leave the enable-control for
1740 * the HW state machine and use this driver only for voltage control.
1741 * One special case is when we use PMIC_STBY_REQ line from SoC to PMIC
1742 * in order to set the system to SUSPEND state.
1743 *
1744 * If regulator is taken under SW control the regulator state will not
1745 * be affected by PMIC state machine - Eg. regulator is likely to stay
1746 * on even in SUSPEND
1747 */
1748 err = get_special_regulators(pdev->dev.parent, reg_data, num_reg_data,
1749 &omit_enable);
1750 if (err)
1751 return err;
1752
1753 for (i = 0; i < num_reg_data; i++) {
1754
1755 struct regulator_desc *desc;
1756 struct regulator_dev *rdev;
1757 struct bd718xx_regulator_data *r;
1758 int no_enable_control = omit_enable & (1 << i);
1759
1760 r = ®_data[i];
1761 desc = &r->desc;
1762
1763 if (no_enable_control)
1764 desc->ops = hwops[i];
1765 else
1766 desc->ops = swops[i];
1767
1768 rdev = devm_regulator_register(&pdev->dev, desc, &config);
1769 if (IS_ERR(rdev))
1770 return dev_err_probe(&pdev->dev, PTR_ERR(rdev),
1771 "failed to register %s regulator\n",
1772 desc->name);
1773
1774 /*
1775 * Regulator register gets the regulator constraints and
1776 * applies them (set_machine_constraints). This should have
1777 * turned the control register(s) to correct values and we
1778 * can now switch the control from PMIC state machine to the
1779 * register interface
1780 *
1781 * At poweroff transition PMIC HW disables EN bit for
1782 * regulators but leaves SEL bit untouched. So if state
1783 * transition from POWEROFF is done to SNVS - then all power
1784 * rails controlled by SW (having SEL bit set) stay disabled
1785 * as EN is cleared. This will result boot failure if any
1786 * crucial systems are powered by these rails. We don't
1787 * enable SW control for crucial regulators if snvs state is
1788 * used
1789 */
1790 if (!no_enable_control && (!use_snvs ||
1791 !rdev->constraints->always_on ||
1792 !rdev->constraints->boot_on)) {
1793 err = regmap_update_bits(regmap, r->init.reg,
1794 r->init.mask, r->init.val);
1795 if (err)
1796 return dev_err_probe(&pdev->dev, err,
1797 "Failed to take control for (%s)\n",
1798 desc->name);
1799 }
1800 for (j = 0; j < r->additional_init_amnt; j++) {
1801 err = regmap_update_bits(regmap,
1802 r->additional_inits[j].reg,
1803 r->additional_inits[j].mask,
1804 r->additional_inits[j].val);
1805 if (err)
1806 return dev_err_probe(&pdev->dev, err,
1807 "Buck (%s) initialization failed\n",
1808 desc->name);
1809 }
1810 }
1811
1812 return err;
1813}
1814
1815static const struct platform_device_id bd718x7_pmic_id[] = {
1816 { "bd71837-pmic", ROHM_CHIP_TYPE_BD71837 },
1817 { "bd71847-pmic", ROHM_CHIP_TYPE_BD71847 },
1818 { },
1819};
1820MODULE_DEVICE_TABLE(platform, bd718x7_pmic_id);
1821
1822static struct platform_driver bd718xx_regulator = {
1823 .driver = {
1824 .name = "bd718xx-pmic",
1825 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1826 },
1827 .probe = bd718xx_probe,
1828 .id_table = bd718x7_pmic_id,
1829};
1830
1831module_platform_driver(bd718xx_regulator);
1832
1833MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1834MODULE_DESCRIPTION("BD71837/BD71847 voltage regulator driver");
1835MODULE_LICENSE("GPL");
1836MODULE_ALIAS("platform:bd718xx-pmic");
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 ROHM Semiconductors
3// bd71837-regulator.c ROHM BD71837MWV/BD71847MWV regulator driver
4
5#include <linux/delay.h>
6#include <linux/err.h>
7#include <linux/interrupt.h>
8#include <linux/kernel.h>
9#include <linux/mfd/rohm-bd718x7.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/platform_device.h>
13#include <linux/regulator/driver.h>
14#include <linux/regulator/machine.h>
15#include <linux/regulator/of_regulator.h>
16#include <linux/slab.h>
17
18/* Typical regulator startup times as per data sheet in uS */
19#define BD71847_BUCK1_STARTUP_TIME 144
20#define BD71847_BUCK2_STARTUP_TIME 162
21#define BD71847_BUCK3_STARTUP_TIME 162
22#define BD71847_BUCK4_STARTUP_TIME 240
23#define BD71847_BUCK5_STARTUP_TIME 270
24#define BD71847_BUCK6_STARTUP_TIME 200
25#define BD71847_LDO1_STARTUP_TIME 440
26#define BD71847_LDO2_STARTUP_TIME 370
27#define BD71847_LDO3_STARTUP_TIME 310
28#define BD71847_LDO4_STARTUP_TIME 400
29#define BD71847_LDO5_STARTUP_TIME 530
30#define BD71847_LDO6_STARTUP_TIME 400
31
32#define BD71837_BUCK1_STARTUP_TIME 160
33#define BD71837_BUCK2_STARTUP_TIME 180
34#define BD71837_BUCK3_STARTUP_TIME 180
35#define BD71837_BUCK4_STARTUP_TIME 180
36#define BD71837_BUCK5_STARTUP_TIME 160
37#define BD71837_BUCK6_STARTUP_TIME 240
38#define BD71837_BUCK7_STARTUP_TIME 220
39#define BD71837_BUCK8_STARTUP_TIME 200
40#define BD71837_LDO1_STARTUP_TIME 440
41#define BD71837_LDO2_STARTUP_TIME 370
42#define BD71837_LDO3_STARTUP_TIME 310
43#define BD71837_LDO4_STARTUP_TIME 400
44#define BD71837_LDO5_STARTUP_TIME 310
45#define BD71837_LDO6_STARTUP_TIME 400
46#define BD71837_LDO7_STARTUP_TIME 530
47
48/*
49 * BD718(37/47/50) have two "enable control modes". ON/OFF can either be
50 * controlled by software - or by PMIC internal HW state machine. Whether
51 * regulator should be under SW or HW control can be defined from device-tree.
52 * Let's provide separate ops for regulators to use depending on the "enable
53 * control mode".
54 */
55#define BD718XX_HWOPNAME(swopname) swopname##_hwcontrol
56
57#define BD718XX_OPS(name, _list_voltage, _map_voltage, _set_voltage_sel, \
58 _get_voltage_sel, _set_voltage_time_sel, _set_ramp_delay, \
59 _set_uvp, _set_ovp) \
60static const struct regulator_ops name = { \
61 .enable = regulator_enable_regmap, \
62 .disable = regulator_disable_regmap, \
63 .is_enabled = regulator_is_enabled_regmap, \
64 .list_voltage = (_list_voltage), \
65 .map_voltage = (_map_voltage), \
66 .set_voltage_sel = (_set_voltage_sel), \
67 .get_voltage_sel = (_get_voltage_sel), \
68 .set_voltage_time_sel = (_set_voltage_time_sel), \
69 .set_ramp_delay = (_set_ramp_delay), \
70 .set_under_voltage_protection = (_set_uvp), \
71 .set_over_voltage_protection = (_set_ovp), \
72}; \
73 \
74static const struct regulator_ops BD718XX_HWOPNAME(name) = { \
75 .is_enabled = always_enabled_by_hwstate, \
76 .list_voltage = (_list_voltage), \
77 .map_voltage = (_map_voltage), \
78 .set_voltage_sel = (_set_voltage_sel), \
79 .get_voltage_sel = (_get_voltage_sel), \
80 .set_voltage_time_sel = (_set_voltage_time_sel), \
81 .set_ramp_delay = (_set_ramp_delay), \
82 .set_under_voltage_protection = (_set_uvp), \
83 .set_over_voltage_protection = (_set_ovp), \
84} \
85
86/*
87 * BUCK1/2/3/4
88 * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
89 * 00: 10.00mV/usec 10mV 1uS
90 * 01: 5.00mV/usec 10mV 2uS
91 * 10: 2.50mV/usec 10mV 4uS
92 * 11: 1.25mV/usec 10mV 8uS
93 */
94static const unsigned int bd718xx_ramp_delay[] = { 10000, 5000, 2500, 1250 };
95
96/* These functions are used when regulators are under HW state machine control.
97 * We assume PMIC is in RUN state because SW running and able to query the
98 * status. Most of the regulators have fixed ON or OFF state at RUN/IDLE so for
99 * them we just return a constant. BD71837 BUCK3 and BUCK4 are exceptions as
100 * they support configuring the ON/OFF state for RUN.
101 *
102 * Note for next hacker - these PMICs have a register where the HW state can be
103 * read. If assuming RUN appears to be false in your use-case - you can
104 * implement state reading (although that is not going to be atomic) before
105 * returning the enable state.
106 */
107static int always_enabled_by_hwstate(struct regulator_dev *rdev)
108{
109 return 1;
110}
111
112static int never_enabled_by_hwstate(struct regulator_dev *rdev)
113{
114 return 0;
115}
116
117static int bd71837_get_buck34_enable_hwctrl(struct regulator_dev *rdev)
118{
119 int ret;
120 unsigned int val;
121
122 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
123 if (ret)
124 return ret;
125
126 return !!(BD718XX_BUCK_RUN_ON & val);
127}
128
129static void voltage_change_done(struct regulator_dev *rdev, unsigned int sel,
130 unsigned int *mask)
131{
132 int ret;
133
134 if (*mask) {
135 /*
136 * Let's allow scheduling as we use I2C anyways. We just need to
137 * guarantee minimum of 1ms sleep - it shouldn't matter if we
138 * exceed it due to the scheduling.
139 */
140 msleep(1);
141
142 ret = regmap_clear_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
143 *mask);
144 if (ret)
145 dev_err(&rdev->dev,
146 "Failed to re-enable voltage monitoring (%d)\n",
147 ret);
148 }
149}
150
151static int voltage_change_prepare(struct regulator_dev *rdev, unsigned int sel,
152 unsigned int *mask)
153{
154 int ret;
155
156 *mask = 0;
157 if (rdev->desc->ops->is_enabled(rdev)) {
158 int now, new;
159
160 now = rdev->desc->ops->get_voltage_sel(rdev);
161 if (now < 0)
162 return now;
163
164 now = rdev->desc->ops->list_voltage(rdev, now);
165 if (now < 0)
166 return now;
167
168 new = rdev->desc->ops->list_voltage(rdev, sel);
169 if (new < 0)
170 return new;
171
172 /*
173 * If we increase LDO voltage when LDO is enabled we need to
174 * disable the power-good detection until voltage has reached
175 * the new level. According to HW colleagues the maximum time
176 * it takes is 1000us. I assume that on systems with light load
177 * this might be less - and we could probably use DT to give
178 * system specific delay value if performance matters.
179 *
180 * Well, knowing we use I2C here and can add scheduling delays
181 * I don't think it is worth the hassle and I just add fixed
182 * 1ms sleep here (and allow scheduling). If this turns out to
183 * be a problem we can change it to delay and make the delay
184 * time configurable.
185 */
186 if (new > now) {
187 int tmp;
188 int prot_bit;
189 int ldo_offset = rdev->desc->id - BD718XX_LDO1;
190
191 prot_bit = BD718XX_LDO1_VRMON80 << ldo_offset;
192 ret = regmap_read(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
193 &tmp);
194 if (ret) {
195 dev_err(&rdev->dev,
196 "Failed to read voltage monitoring state\n");
197 return ret;
198 }
199
200 if (!(tmp & prot_bit)) {
201 /* We disable protection if it was enabled... */
202 ret = regmap_set_bits(rdev->regmap,
203 BD718XX_REG_MVRFLTMASK2,
204 prot_bit);
205 /* ...and we also want to re-enable it */
206 *mask = prot_bit;
207 }
208 if (ret) {
209 dev_err(&rdev->dev,
210 "Failed to stop voltage monitoring\n");
211 return ret;
212 }
213 }
214 }
215
216 return 0;
217}
218
219static int bd718xx_set_voltage_sel_restricted(struct regulator_dev *rdev,
220 unsigned int sel)
221{
222 int ret;
223 int mask;
224
225 ret = voltage_change_prepare(rdev, sel, &mask);
226 if (ret)
227 return ret;
228
229 ret = regulator_set_voltage_sel_regmap(rdev, sel);
230 voltage_change_done(rdev, sel, &mask);
231
232 return ret;
233}
234
235static int bd718xx_set_voltage_sel_pickable_restricted(
236 struct regulator_dev *rdev, unsigned int sel)
237{
238 int ret;
239 int mask;
240
241 ret = voltage_change_prepare(rdev, sel, &mask);
242 if (ret)
243 return ret;
244
245 ret = regulator_set_voltage_sel_pickable_regmap(rdev, sel);
246 voltage_change_done(rdev, sel, &mask);
247
248 return ret;
249}
250
251static int bd71837_set_voltage_sel_pickable_restricted(
252 struct regulator_dev *rdev, unsigned int sel)
253{
254 if (rdev->desc->ops->is_enabled(rdev))
255 return -EBUSY;
256
257 return regulator_set_voltage_sel_pickable_regmap(rdev, sel);
258}
259
260/*
261 * BD71837 BUCK1/2/3/4
262 * BD71847 BUCK1/2
263 * 0.70 to 1.30V (10mV step)
264 */
265static const struct linear_range bd718xx_dvs_buck_volts[] = {
266 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x3C, 10000),
267 REGULATOR_LINEAR_RANGE(1300000, 0x3D, 0x3F, 0),
268};
269
270/*
271 * BD71837 BUCK5
272 * 0.7V to 1.35V (range 0)
273 * and
274 * 0.675 to 1.325 (range 1)
275 */
276static const struct linear_range bd71837_buck5_volts[] = {
277 /* Ranges when VOLT_SEL bit is 0 */
278 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
279 REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
280 REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
281 /* Ranges when VOLT_SEL bit is 1 */
282 REGULATOR_LINEAR_RANGE(675000, 0x0, 0x3, 100000),
283 REGULATOR_LINEAR_RANGE(1025000, 0x4, 0x5, 50000),
284 REGULATOR_LINEAR_RANGE(1175000, 0x6, 0x7, 150000),
285};
286
287/*
288 * Range selector for first 3 linear ranges is 0x0
289 * and 0x1 for last 3 ranges.
290 */
291static const unsigned int bd71837_buck5_volt_range_sel[] = {
292 0x0, 0x0, 0x0, 0x1, 0x1, 0x1
293};
294
295/*
296 * BD71847 BUCK3
297 */
298static const struct linear_range bd71847_buck3_volts[] = {
299 /* Ranges when VOLT_SEL bits are 00 */
300 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
301 REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
302 REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
303 /* Ranges when VOLT_SEL bits are 01 */
304 REGULATOR_LINEAR_RANGE(550000, 0x0, 0x7, 50000),
305 /* Ranges when VOLT_SEL bits are 11 */
306 REGULATOR_LINEAR_RANGE(675000, 0x0, 0x3, 100000),
307 REGULATOR_LINEAR_RANGE(1025000, 0x4, 0x5, 50000),
308 REGULATOR_LINEAR_RANGE(1175000, 0x6, 0x7, 150000),
309};
310
311static const unsigned int bd71847_buck3_volt_range_sel[] = {
312 0x0, 0x0, 0x0, 0x1, 0x2, 0x2, 0x2
313};
314
315static const struct linear_range bd71847_buck4_volts[] = {
316 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
317 REGULATOR_LINEAR_RANGE(2600000, 0x00, 0x03, 100000),
318};
319
320static const unsigned int bd71847_buck4_volt_range_sel[] = { 0x0, 0x1 };
321
322/*
323 * BUCK6
324 * 3.0V to 3.3V (step 100mV)
325 */
326static const struct linear_range bd71837_buck6_volts[] = {
327 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
328};
329
330/*
331 * BD71837 BUCK7
332 * BD71847 BUCK5
333 * 000 = 1.605V
334 * 001 = 1.695V
335 * 010 = 1.755V
336 * 011 = 1.8V (Initial)
337 * 100 = 1.845V
338 * 101 = 1.905V
339 * 110 = 1.95V
340 * 111 = 1.995V
341 */
342static const unsigned int bd718xx_3rd_nodvs_buck_volts[] = {
343 1605000, 1695000, 1755000, 1800000, 1845000, 1905000, 1950000, 1995000
344};
345
346/*
347 * BUCK8
348 * 0.8V to 1.40V (step 10mV)
349 */
350static const struct linear_range bd718xx_4th_nodvs_buck_volts[] = {
351 REGULATOR_LINEAR_RANGE(800000, 0x00, 0x3C, 10000),
352};
353
354/*
355 * LDO1
356 * 3.0 to 3.3V (100mV step)
357 */
358static const struct linear_range bd718xx_ldo1_volts[] = {
359 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
360 REGULATOR_LINEAR_RANGE(1600000, 0x00, 0x03, 100000),
361};
362
363static const unsigned int bd718xx_ldo1_volt_range_sel[] = { 0x0, 0x1 };
364
365/*
366 * LDO2
367 * 0.8 or 0.9V
368 */
369static const unsigned int ldo_2_volts[] = {
370 900000, 800000
371};
372
373/*
374 * LDO3
375 * 1.8 to 3.3V (100mV step)
376 */
377static const struct linear_range bd718xx_ldo3_volts[] = {
378 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
379};
380
381/*
382 * LDO4
383 * 0.9 to 1.8V (100mV step)
384 */
385static const struct linear_range bd718xx_ldo4_volts[] = {
386 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
387};
388
389/*
390 * LDO5 for BD71837
391 * 1.8 to 3.3V (100mV step)
392 */
393static const struct linear_range bd71837_ldo5_volts[] = {
394 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
395};
396
397/*
398 * LDO5 for BD71837
399 * 1.8 to 3.3V (100mV step)
400 */
401static const struct linear_range bd71847_ldo5_volts[] = {
402 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
403 REGULATOR_LINEAR_RANGE(800000, 0x00, 0x0F, 100000),
404};
405
406static const unsigned int bd71847_ldo5_volt_range_sel[] = { 0x0, 0x1 };
407
408/*
409 * LDO6
410 * 0.9 to 1.8V (100mV step)
411 */
412static const struct linear_range bd718xx_ldo6_volts[] = {
413 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
414};
415
416/*
417 * LDO7
418 * 1.8 to 3.3V (100mV step)
419 */
420static const struct linear_range bd71837_ldo7_volts[] = {
421 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
422};
423
424struct reg_init {
425 unsigned int reg;
426 unsigned int mask;
427 unsigned int val;
428};
429struct bd718xx_regulator_data {
430 struct regulator_desc desc;
431 const struct rohm_dvs_config dvs;
432 const struct reg_init init;
433 const struct reg_init *additional_inits;
434 int additional_init_amnt;
435};
436
437static int bd718x7_xvp_sanity_check(struct regulator_dev *rdev, int lim_uV,
438 int severity)
439{
440 /*
441 * BD71837/47/50 ... (ICs supported by this driver) do not provide
442 * warnings, only protection
443 */
444 if (severity != REGULATOR_SEVERITY_PROT) {
445 dev_err(&rdev->dev,
446 "Unsupported Under Voltage protection level\n");
447 return -EINVAL;
448 }
449
450 /*
451 * And protection limit is not changeable. It can only be enabled
452 * or disabled
453 */
454 if (lim_uV)
455 return -EINVAL;
456
457 return 0;
458}
459
460static int bd718x7_set_ldo_uvp(struct regulator_dev *rdev, int lim_uV,
461 int severity, bool enable)
462{
463 int ldo_offset = rdev->desc->id - BD718XX_LDO1;
464 int prot_bit, ret;
465
466 ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
467 if (ret)
468 return ret;
469
470 prot_bit = BD718XX_LDO1_VRMON80 << ldo_offset;
471
472 if (enable)
473 return regmap_clear_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
474 prot_bit);
475
476 return regmap_set_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
477 prot_bit);
478}
479
480static int bd718x7_get_buck_prot_reg(int id, int *reg)
481{
482
483 if (id > BD718XX_BUCK8) {
484 WARN_ON(id > BD718XX_BUCK8);
485 return -EINVAL;
486 }
487
488 if (id > BD718XX_BUCK4)
489 *reg = BD718XX_REG_MVRFLTMASK0;
490 else
491 *reg = BD718XX_REG_MVRFLTMASK1;
492
493 return 0;
494}
495
496static int bd718x7_get_buck_ovp_info(int id, int *reg, int *bit)
497{
498 int ret;
499
500 ret = bd718x7_get_buck_prot_reg(id, reg);
501 if (ret)
502 return ret;
503
504 *bit = BIT((id % 4) * 2 + 1);
505
506 return 0;
507}
508
509static int bd718x7_get_buck_uvp_info(int id, int *reg, int *bit)
510{
511 int ret;
512
513 ret = bd718x7_get_buck_prot_reg(id, reg);
514 if (ret)
515 return ret;
516
517 *bit = BIT((id % 4) * 2);
518
519 return 0;
520}
521
522static int bd718x7_set_buck_uvp(struct regulator_dev *rdev, int lim_uV,
523 int severity, bool enable)
524{
525 int bit, reg, ret;
526
527 ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
528 if (ret)
529 return ret;
530
531 ret = bd718x7_get_buck_uvp_info(rdev->desc->id, ®, &bit);
532 if (ret)
533 return ret;
534
535 if (enable)
536 return regmap_clear_bits(rdev->regmap, reg, bit);
537
538 return regmap_set_bits(rdev->regmap, reg, bit);
539
540}
541
542static int bd718x7_set_buck_ovp(struct regulator_dev *rdev, int lim_uV,
543 int severity,
544 bool enable)
545{
546 int bit, reg, ret;
547
548 ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
549 if (ret)
550 return ret;
551
552 ret = bd718x7_get_buck_ovp_info(rdev->desc->id, ®, &bit);
553 if (ret)
554 return ret;
555
556 if (enable)
557 return regmap_clear_bits(rdev->regmap, reg, bit);
558
559 return regmap_set_bits(rdev->regmap, reg, bit);
560}
561
562/*
563 * OPS common for BD71847 and BD71850
564 */
565BD718XX_OPS(bd718xx_pickable_range_ldo_ops,
566 regulator_list_voltage_pickable_linear_range, NULL,
567 bd718xx_set_voltage_sel_pickable_restricted,
568 regulator_get_voltage_sel_pickable_regmap, NULL, NULL,
569 bd718x7_set_ldo_uvp, NULL);
570
571/* BD71847 and BD71850 LDO 5 is by default OFF at RUN state */
572static const struct regulator_ops bd718xx_ldo5_ops_hwstate = {
573 .is_enabled = never_enabled_by_hwstate,
574 .list_voltage = regulator_list_voltage_pickable_linear_range,
575 .set_voltage_sel = bd718xx_set_voltage_sel_pickable_restricted,
576 .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
577 .set_under_voltage_protection = bd718x7_set_ldo_uvp,
578};
579
580BD718XX_OPS(bd718xx_pickable_range_buck_ops,
581 regulator_list_voltage_pickable_linear_range, NULL,
582 regulator_set_voltage_sel_pickable_regmap,
583 regulator_get_voltage_sel_pickable_regmap,
584 regulator_set_voltage_time_sel, NULL, bd718x7_set_buck_uvp,
585 bd718x7_set_buck_ovp);
586
587BD718XX_OPS(bd718xx_ldo_regulator_ops, regulator_list_voltage_linear_range,
588 NULL, bd718xx_set_voltage_sel_restricted,
589 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
590 NULL);
591
592BD718XX_OPS(bd718xx_ldo_regulator_nolinear_ops, regulator_list_voltage_table,
593 NULL, bd718xx_set_voltage_sel_restricted,
594 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
595 NULL);
596
597BD718XX_OPS(bd718xx_buck_regulator_ops, regulator_list_voltage_linear_range,
598 NULL, regulator_set_voltage_sel_regmap,
599 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
600 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
601
602BD718XX_OPS(bd718xx_buck_regulator_nolinear_ops, regulator_list_voltage_table,
603 regulator_map_voltage_ascend, regulator_set_voltage_sel_regmap,
604 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
605 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
606
607/*
608 * OPS for BD71837
609 */
610BD718XX_OPS(bd71837_pickable_range_ldo_ops,
611 regulator_list_voltage_pickable_linear_range, NULL,
612 bd71837_set_voltage_sel_pickable_restricted,
613 regulator_get_voltage_sel_pickable_regmap, NULL, NULL,
614 bd718x7_set_ldo_uvp, NULL);
615
616BD718XX_OPS(bd71837_pickable_range_buck_ops,
617 regulator_list_voltage_pickable_linear_range, NULL,
618 bd71837_set_voltage_sel_pickable_restricted,
619 regulator_get_voltage_sel_pickable_regmap,
620 regulator_set_voltage_time_sel, NULL, bd718x7_set_buck_uvp,
621 bd718x7_set_buck_ovp);
622
623BD718XX_OPS(bd71837_ldo_regulator_ops, regulator_list_voltage_linear_range,
624 NULL, rohm_regulator_set_voltage_sel_restricted,
625 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
626 NULL);
627
628BD718XX_OPS(bd71837_ldo_regulator_nolinear_ops, regulator_list_voltage_table,
629 NULL, rohm_regulator_set_voltage_sel_restricted,
630 regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
631 NULL);
632
633BD718XX_OPS(bd71837_buck_regulator_ops, regulator_list_voltage_linear_range,
634 NULL, rohm_regulator_set_voltage_sel_restricted,
635 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
636 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
637
638BD718XX_OPS(bd71837_buck_regulator_nolinear_ops, regulator_list_voltage_table,
639 regulator_map_voltage_ascend, rohm_regulator_set_voltage_sel_restricted,
640 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
641 NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
642/*
643 * BD71837 bucks 3 and 4 support defining their enable/disable state also
644 * when buck enable state is under HW state machine control. In that case the
645 * bit [2] in CTRL register is used to indicate if regulator should be ON.
646 */
647static const struct regulator_ops bd71837_buck34_ops_hwctrl = {
648 .is_enabled = bd71837_get_buck34_enable_hwctrl,
649 .list_voltage = regulator_list_voltage_linear_range,
650 .set_voltage_sel = regulator_set_voltage_sel_regmap,
651 .get_voltage_sel = regulator_get_voltage_sel_regmap,
652 .set_voltage_time_sel = regulator_set_voltage_time_sel,
653 .set_ramp_delay = regulator_set_ramp_delay_regmap,
654 .set_under_voltage_protection = bd718x7_set_buck_uvp,
655 .set_over_voltage_protection = bd718x7_set_buck_ovp,
656};
657
658/*
659 * OPS for all of the ICs - BD718(37/47/50)
660 */
661BD718XX_OPS(bd718xx_dvs_buck_regulator_ops, regulator_list_voltage_linear_range,
662 NULL, regulator_set_voltage_sel_regmap,
663 regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
664 regulator_set_ramp_delay_regmap, bd718x7_set_buck_uvp,
665 bd718x7_set_buck_ovp);
666
667
668
669/*
670 * There is a HW quirk in BD71837. The shutdown sequence timings for
671 * bucks/LDOs which are controlled via register interface are changed.
672 * At PMIC poweroff the voltage for BUCK6/7 is cut immediately at the
673 * beginning of shut-down sequence. As bucks 6 and 7 are parent
674 * supplies for LDO5 and LDO6 - this causes LDO5/6 voltage
675 * monitoring to errorneously detect under voltage and force PMIC to
676 * emergency state instead of poweroff. In order to avoid this we
677 * disable voltage monitoring for LDO5 and LDO6
678 */
679static const struct reg_init bd71837_ldo5_inits[] = {
680 {
681 .reg = BD718XX_REG_MVRFLTMASK2,
682 .mask = BD718XX_LDO5_VRMON80,
683 .val = BD718XX_LDO5_VRMON80,
684 },
685};
686
687static const struct reg_init bd71837_ldo6_inits[] = {
688 {
689 .reg = BD718XX_REG_MVRFLTMASK2,
690 .mask = BD718XX_LDO6_VRMON80,
691 .val = BD718XX_LDO6_VRMON80,
692 },
693};
694
695static int buck_set_hw_dvs_levels(struct device_node *np,
696 const struct regulator_desc *desc,
697 struct regulator_config *cfg)
698{
699 struct bd718xx_regulator_data *data;
700
701 data = container_of(desc, struct bd718xx_regulator_data, desc);
702
703 return rohm_regulator_set_dvs_levels(&data->dvs, np, desc, cfg->regmap);
704}
705
706static const struct regulator_ops *bd71847_swcontrol_ops[] = {
707 &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
708 &bd718xx_pickable_range_buck_ops, &bd718xx_pickable_range_buck_ops,
709 &bd718xx_buck_regulator_nolinear_ops, &bd718xx_buck_regulator_ops,
710 &bd718xx_pickable_range_ldo_ops, &bd718xx_ldo_regulator_nolinear_ops,
711 &bd718xx_ldo_regulator_ops, &bd718xx_ldo_regulator_ops,
712 &bd718xx_pickable_range_ldo_ops, &bd718xx_ldo_regulator_ops,
713};
714
715static const struct regulator_ops *bd71847_hwcontrol_ops[] = {
716 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
717 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
718 &BD718XX_HWOPNAME(bd718xx_pickable_range_buck_ops),
719 &BD718XX_HWOPNAME(bd718xx_pickable_range_buck_ops),
720 &BD718XX_HWOPNAME(bd718xx_buck_regulator_nolinear_ops),
721 &BD718XX_HWOPNAME(bd718xx_buck_regulator_ops),
722 &BD718XX_HWOPNAME(bd718xx_pickable_range_ldo_ops),
723 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_nolinear_ops),
724 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
725 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
726 &bd718xx_ldo5_ops_hwstate,
727 &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
728};
729
730static struct bd718xx_regulator_data bd71847_regulators[] = {
731 {
732 .desc = {
733 .name = "buck1",
734 .of_match = of_match_ptr("BUCK1"),
735 .regulators_node = of_match_ptr("regulators"),
736 .id = BD718XX_BUCK1,
737 .type = REGULATOR_VOLTAGE,
738 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
739 .linear_ranges = bd718xx_dvs_buck_volts,
740 .n_linear_ranges =
741 ARRAY_SIZE(bd718xx_dvs_buck_volts),
742 .vsel_reg = BD718XX_REG_BUCK1_VOLT_RUN,
743 .vsel_mask = DVS_BUCK_RUN_MASK,
744 .enable_reg = BD718XX_REG_BUCK1_CTRL,
745 .enable_mask = BD718XX_BUCK_EN,
746 .enable_time = BD71847_BUCK1_STARTUP_TIME,
747 .owner = THIS_MODULE,
748 .ramp_delay_table = bd718xx_ramp_delay,
749 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
750 .ramp_reg = BD718XX_REG_BUCK1_CTRL,
751 .ramp_mask = BUCK_RAMPRATE_MASK,
752 .of_parse_cb = buck_set_hw_dvs_levels,
753 },
754 .dvs = {
755 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE |
756 ROHM_DVS_LEVEL_SUSPEND,
757 .run_reg = BD718XX_REG_BUCK1_VOLT_RUN,
758 .run_mask = DVS_BUCK_RUN_MASK,
759 .idle_reg = BD718XX_REG_BUCK1_VOLT_IDLE,
760 .idle_mask = DVS_BUCK_RUN_MASK,
761 .suspend_reg = BD718XX_REG_BUCK1_VOLT_SUSP,
762 .suspend_mask = DVS_BUCK_RUN_MASK,
763 },
764 .init = {
765 .reg = BD718XX_REG_BUCK1_CTRL,
766 .mask = BD718XX_BUCK_SEL,
767 .val = BD718XX_BUCK_SEL,
768 },
769 },
770 {
771 .desc = {
772 .name = "buck2",
773 .of_match = of_match_ptr("BUCK2"),
774 .regulators_node = of_match_ptr("regulators"),
775 .id = BD718XX_BUCK2,
776 .type = REGULATOR_VOLTAGE,
777 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
778 .linear_ranges = bd718xx_dvs_buck_volts,
779 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
780 .vsel_reg = BD718XX_REG_BUCK2_VOLT_RUN,
781 .vsel_mask = DVS_BUCK_RUN_MASK,
782 .enable_reg = BD718XX_REG_BUCK2_CTRL,
783 .enable_mask = BD718XX_BUCK_EN,
784 .enable_time = BD71847_BUCK2_STARTUP_TIME,
785 .ramp_delay_table = bd718xx_ramp_delay,
786 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
787 .ramp_reg = BD718XX_REG_BUCK2_CTRL,
788 .ramp_mask = BUCK_RAMPRATE_MASK,
789 .owner = THIS_MODULE,
790 .of_parse_cb = buck_set_hw_dvs_levels,
791 },
792 .dvs = {
793 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE,
794 .run_reg = BD718XX_REG_BUCK2_VOLT_RUN,
795 .run_mask = DVS_BUCK_RUN_MASK,
796 .idle_reg = BD718XX_REG_BUCK2_VOLT_IDLE,
797 .idle_mask = DVS_BUCK_RUN_MASK,
798 },
799 .init = {
800 .reg = BD718XX_REG_BUCK2_CTRL,
801 .mask = BD718XX_BUCK_SEL,
802 .val = BD718XX_BUCK_SEL,
803 },
804 },
805 {
806 .desc = {
807 .name = "buck3",
808 .of_match = of_match_ptr("BUCK3"),
809 .regulators_node = of_match_ptr("regulators"),
810 .id = BD718XX_BUCK3,
811 .type = REGULATOR_VOLTAGE,
812 .n_voltages = BD71847_BUCK3_VOLTAGE_NUM,
813 .linear_ranges = bd71847_buck3_volts,
814 .n_linear_ranges =
815 ARRAY_SIZE(bd71847_buck3_volts),
816 .vsel_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
817 .vsel_mask = BD718XX_1ST_NODVS_BUCK_MASK,
818 .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
819 .vsel_range_mask = BD71847_BUCK3_RANGE_MASK,
820 .linear_range_selectors_bitfield = bd71847_buck3_volt_range_sel,
821 .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
822 .enable_mask = BD718XX_BUCK_EN,
823 .enable_time = BD71847_BUCK3_STARTUP_TIME,
824 .owner = THIS_MODULE,
825 },
826 .init = {
827 .reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
828 .mask = BD718XX_BUCK_SEL,
829 .val = BD718XX_BUCK_SEL,
830 },
831 },
832 {
833 .desc = {
834 .name = "buck4",
835 .of_match = of_match_ptr("BUCK4"),
836 .regulators_node = of_match_ptr("regulators"),
837 .id = BD718XX_BUCK4,
838 .type = REGULATOR_VOLTAGE,
839 .n_voltages = BD71847_BUCK4_VOLTAGE_NUM,
840 .linear_ranges = bd71847_buck4_volts,
841 .n_linear_ranges =
842 ARRAY_SIZE(bd71847_buck4_volts),
843 .enable_reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
844 .vsel_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
845 .vsel_mask = BD71847_BUCK4_MASK,
846 .vsel_range_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
847 .vsel_range_mask = BD71847_BUCK4_RANGE_MASK,
848 .linear_range_selectors_bitfield = bd71847_buck4_volt_range_sel,
849 .enable_mask = BD718XX_BUCK_EN,
850 .enable_time = BD71847_BUCK4_STARTUP_TIME,
851 .owner = THIS_MODULE,
852 },
853 .init = {
854 .reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
855 .mask = BD718XX_BUCK_SEL,
856 .val = BD718XX_BUCK_SEL,
857 },
858 },
859 {
860 .desc = {
861 .name = "buck5",
862 .of_match = of_match_ptr("BUCK5"),
863 .regulators_node = of_match_ptr("regulators"),
864 .id = BD718XX_BUCK5,
865 .type = REGULATOR_VOLTAGE,
866 .volt_table = &bd718xx_3rd_nodvs_buck_volts[0],
867 .n_voltages = ARRAY_SIZE(bd718xx_3rd_nodvs_buck_volts),
868 .vsel_reg = BD718XX_REG_3RD_NODVS_BUCK_VOLT,
869 .vsel_mask = BD718XX_3RD_NODVS_BUCK_MASK,
870 .enable_reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
871 .enable_mask = BD718XX_BUCK_EN,
872 .enable_time = BD71847_BUCK5_STARTUP_TIME,
873 .owner = THIS_MODULE,
874 },
875 .init = {
876 .reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
877 .mask = BD718XX_BUCK_SEL,
878 .val = BD718XX_BUCK_SEL,
879 },
880 },
881 {
882 .desc = {
883 .name = "buck6",
884 .of_match = of_match_ptr("BUCK6"),
885 .regulators_node = of_match_ptr("regulators"),
886 .id = BD718XX_BUCK6,
887 .type = REGULATOR_VOLTAGE,
888 .n_voltages = BD718XX_4TH_NODVS_BUCK_VOLTAGE_NUM,
889 .linear_ranges = bd718xx_4th_nodvs_buck_volts,
890 .n_linear_ranges =
891 ARRAY_SIZE(bd718xx_4th_nodvs_buck_volts),
892 .vsel_reg = BD718XX_REG_4TH_NODVS_BUCK_VOLT,
893 .vsel_mask = BD718XX_4TH_NODVS_BUCK_MASK,
894 .enable_reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
895 .enable_mask = BD718XX_BUCK_EN,
896 .enable_time = BD71847_BUCK6_STARTUP_TIME,
897 .owner = THIS_MODULE,
898 },
899 .init = {
900 .reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
901 .mask = BD718XX_BUCK_SEL,
902 .val = BD718XX_BUCK_SEL,
903 },
904 },
905 {
906 .desc = {
907 .name = "ldo1",
908 .of_match = of_match_ptr("LDO1"),
909 .regulators_node = of_match_ptr("regulators"),
910 .id = BD718XX_LDO1,
911 .type = REGULATOR_VOLTAGE,
912 .n_voltages = BD718XX_LDO1_VOLTAGE_NUM,
913 .linear_ranges = bd718xx_ldo1_volts,
914 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo1_volts),
915 .vsel_reg = BD718XX_REG_LDO1_VOLT,
916 .vsel_mask = BD718XX_LDO1_MASK,
917 .vsel_range_reg = BD718XX_REG_LDO1_VOLT,
918 .vsel_range_mask = BD718XX_LDO1_RANGE_MASK,
919 .linear_range_selectors_bitfield = bd718xx_ldo1_volt_range_sel,
920 .enable_reg = BD718XX_REG_LDO1_VOLT,
921 .enable_mask = BD718XX_LDO_EN,
922 .enable_time = BD71847_LDO1_STARTUP_TIME,
923 .owner = THIS_MODULE,
924 },
925 .init = {
926 .reg = BD718XX_REG_LDO1_VOLT,
927 .mask = BD718XX_LDO_SEL,
928 .val = BD718XX_LDO_SEL,
929 },
930 },
931 {
932 .desc = {
933 .name = "ldo2",
934 .of_match = of_match_ptr("LDO2"),
935 .regulators_node = of_match_ptr("regulators"),
936 .id = BD718XX_LDO2,
937 .type = REGULATOR_VOLTAGE,
938 .volt_table = &ldo_2_volts[0],
939 .vsel_reg = BD718XX_REG_LDO2_VOLT,
940 .vsel_mask = BD718XX_LDO2_MASK,
941 .n_voltages = ARRAY_SIZE(ldo_2_volts),
942 .enable_reg = BD718XX_REG_LDO2_VOLT,
943 .enable_mask = BD718XX_LDO_EN,
944 .enable_time = BD71847_LDO2_STARTUP_TIME,
945 .owner = THIS_MODULE,
946 },
947 .init = {
948 .reg = BD718XX_REG_LDO2_VOLT,
949 .mask = BD718XX_LDO_SEL,
950 .val = BD718XX_LDO_SEL,
951 },
952 },
953 {
954 .desc = {
955 .name = "ldo3",
956 .of_match = of_match_ptr("LDO3"),
957 .regulators_node = of_match_ptr("regulators"),
958 .id = BD718XX_LDO3,
959 .type = REGULATOR_VOLTAGE,
960 .n_voltages = BD718XX_LDO3_VOLTAGE_NUM,
961 .linear_ranges = bd718xx_ldo3_volts,
962 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo3_volts),
963 .vsel_reg = BD718XX_REG_LDO3_VOLT,
964 .vsel_mask = BD718XX_LDO3_MASK,
965 .enable_reg = BD718XX_REG_LDO3_VOLT,
966 .enable_mask = BD718XX_LDO_EN,
967 .enable_time = BD71847_LDO3_STARTUP_TIME,
968 .owner = THIS_MODULE,
969 },
970 .init = {
971 .reg = BD718XX_REG_LDO3_VOLT,
972 .mask = BD718XX_LDO_SEL,
973 .val = BD718XX_LDO_SEL,
974 },
975 },
976 {
977 .desc = {
978 .name = "ldo4",
979 .of_match = of_match_ptr("LDO4"),
980 .regulators_node = of_match_ptr("regulators"),
981 .id = BD718XX_LDO4,
982 .type = REGULATOR_VOLTAGE,
983 .n_voltages = BD718XX_LDO4_VOLTAGE_NUM,
984 .linear_ranges = bd718xx_ldo4_volts,
985 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo4_volts),
986 .vsel_reg = BD718XX_REG_LDO4_VOLT,
987 .vsel_mask = BD718XX_LDO4_MASK,
988 .enable_reg = BD718XX_REG_LDO4_VOLT,
989 .enable_mask = BD718XX_LDO_EN,
990 .enable_time = BD71847_LDO4_STARTUP_TIME,
991 .owner = THIS_MODULE,
992 },
993 .init = {
994 .reg = BD718XX_REG_LDO4_VOLT,
995 .mask = BD718XX_LDO_SEL,
996 .val = BD718XX_LDO_SEL,
997 },
998 },
999 {
1000 .desc = {
1001 .name = "ldo5",
1002 .of_match = of_match_ptr("LDO5"),
1003 .regulators_node = of_match_ptr("regulators"),
1004 .id = BD718XX_LDO5,
1005 .type = REGULATOR_VOLTAGE,
1006 .n_voltages = BD71847_LDO5_VOLTAGE_NUM,
1007 .linear_ranges = bd71847_ldo5_volts,
1008 .n_linear_ranges = ARRAY_SIZE(bd71847_ldo5_volts),
1009 .vsel_reg = BD718XX_REG_LDO5_VOLT,
1010 .vsel_mask = BD71847_LDO5_MASK,
1011 .vsel_range_reg = BD718XX_REG_LDO5_VOLT,
1012 .vsel_range_mask = BD71847_LDO5_RANGE_MASK,
1013 .linear_range_selectors_bitfield = bd71847_ldo5_volt_range_sel,
1014 .enable_reg = BD718XX_REG_LDO5_VOLT,
1015 .enable_mask = BD718XX_LDO_EN,
1016 .enable_time = BD71847_LDO5_STARTUP_TIME,
1017 .owner = THIS_MODULE,
1018 },
1019 .init = {
1020 .reg = BD718XX_REG_LDO5_VOLT,
1021 .mask = BD718XX_LDO_SEL,
1022 .val = BD718XX_LDO_SEL,
1023 },
1024 },
1025 {
1026 .desc = {
1027 .name = "ldo6",
1028 .of_match = of_match_ptr("LDO6"),
1029 .regulators_node = of_match_ptr("regulators"),
1030 .id = BD718XX_LDO6,
1031 .type = REGULATOR_VOLTAGE,
1032 .n_voltages = BD718XX_LDO6_VOLTAGE_NUM,
1033 .linear_ranges = bd718xx_ldo6_volts,
1034 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo6_volts),
1035 /* LDO6 is supplied by buck5 */
1036 .supply_name = "buck5",
1037 .vsel_reg = BD718XX_REG_LDO6_VOLT,
1038 .vsel_mask = BD718XX_LDO6_MASK,
1039 .enable_reg = BD718XX_REG_LDO6_VOLT,
1040 .enable_mask = BD718XX_LDO_EN,
1041 .enable_time = BD71847_LDO6_STARTUP_TIME,
1042 .owner = THIS_MODULE,
1043 },
1044 .init = {
1045 .reg = BD718XX_REG_LDO6_VOLT,
1046 .mask = BD718XX_LDO_SEL,
1047 .val = BD718XX_LDO_SEL,
1048 },
1049 },
1050};
1051
1052static const struct regulator_ops *bd71837_swcontrol_ops[] = {
1053 &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
1054 &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
1055 &bd71837_pickable_range_buck_ops, &bd71837_buck_regulator_ops,
1056 &bd71837_buck_regulator_nolinear_ops, &bd71837_buck_regulator_ops,
1057 &bd71837_pickable_range_ldo_ops, &bd71837_ldo_regulator_nolinear_ops,
1058 &bd71837_ldo_regulator_ops, &bd71837_ldo_regulator_ops,
1059 &bd71837_ldo_regulator_ops, &bd71837_ldo_regulator_ops,
1060 &bd71837_ldo_regulator_ops,
1061};
1062
1063static const struct regulator_ops *bd71837_hwcontrol_ops[] = {
1064 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
1065 &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
1066 &bd71837_buck34_ops_hwctrl, &bd71837_buck34_ops_hwctrl,
1067 &BD718XX_HWOPNAME(bd71837_pickable_range_buck_ops),
1068 &BD718XX_HWOPNAME(bd71837_buck_regulator_ops),
1069 &BD718XX_HWOPNAME(bd71837_buck_regulator_nolinear_ops),
1070 &BD718XX_HWOPNAME(bd71837_buck_regulator_ops),
1071 &BD718XX_HWOPNAME(bd71837_pickable_range_ldo_ops),
1072 &BD718XX_HWOPNAME(bd71837_ldo_regulator_nolinear_ops),
1073 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1074 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1075 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1076 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1077 &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1078};
1079
1080static struct bd718xx_regulator_data bd71837_regulators[] = {
1081 {
1082 .desc = {
1083 .name = "buck1",
1084 .of_match = of_match_ptr("BUCK1"),
1085 .regulators_node = of_match_ptr("regulators"),
1086 .id = BD718XX_BUCK1,
1087 .type = REGULATOR_VOLTAGE,
1088 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1089 .linear_ranges = bd718xx_dvs_buck_volts,
1090 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1091 .vsel_reg = BD718XX_REG_BUCK1_VOLT_RUN,
1092 .vsel_mask = DVS_BUCK_RUN_MASK,
1093 .enable_reg = BD718XX_REG_BUCK1_CTRL,
1094 .enable_mask = BD718XX_BUCK_EN,
1095 .enable_time = BD71837_BUCK1_STARTUP_TIME,
1096 .ramp_delay_table = bd718xx_ramp_delay,
1097 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1098 .ramp_reg = BD718XX_REG_BUCK1_CTRL,
1099 .ramp_mask = BUCK_RAMPRATE_MASK,
1100 .owner = THIS_MODULE,
1101 .of_parse_cb = buck_set_hw_dvs_levels,
1102 },
1103 .dvs = {
1104 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE |
1105 ROHM_DVS_LEVEL_SUSPEND,
1106 .run_reg = BD718XX_REG_BUCK1_VOLT_RUN,
1107 .run_mask = DVS_BUCK_RUN_MASK,
1108 .idle_reg = BD718XX_REG_BUCK1_VOLT_IDLE,
1109 .idle_mask = DVS_BUCK_RUN_MASK,
1110 .suspend_reg = BD718XX_REG_BUCK1_VOLT_SUSP,
1111 .suspend_mask = DVS_BUCK_RUN_MASK,
1112 },
1113 .init = {
1114 .reg = BD718XX_REG_BUCK1_CTRL,
1115 .mask = BD718XX_BUCK_SEL,
1116 .val = BD718XX_BUCK_SEL,
1117 },
1118 },
1119 {
1120 .desc = {
1121 .name = "buck2",
1122 .of_match = of_match_ptr("BUCK2"),
1123 .regulators_node = of_match_ptr("regulators"),
1124 .id = BD718XX_BUCK2,
1125 .type = REGULATOR_VOLTAGE,
1126 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1127 .linear_ranges = bd718xx_dvs_buck_volts,
1128 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1129 .vsel_reg = BD718XX_REG_BUCK2_VOLT_RUN,
1130 .vsel_mask = DVS_BUCK_RUN_MASK,
1131 .enable_reg = BD718XX_REG_BUCK2_CTRL,
1132 .enable_mask = BD718XX_BUCK_EN,
1133 .enable_time = BD71837_BUCK2_STARTUP_TIME,
1134 .ramp_delay_table = bd718xx_ramp_delay,
1135 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1136 .ramp_reg = BD718XX_REG_BUCK2_CTRL,
1137 .ramp_mask = BUCK_RAMPRATE_MASK,
1138 .owner = THIS_MODULE,
1139 .of_parse_cb = buck_set_hw_dvs_levels,
1140 },
1141 .dvs = {
1142 .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE,
1143 .run_reg = BD718XX_REG_BUCK2_VOLT_RUN,
1144 .run_mask = DVS_BUCK_RUN_MASK,
1145 .idle_reg = BD718XX_REG_BUCK2_VOLT_IDLE,
1146 .idle_mask = DVS_BUCK_RUN_MASK,
1147 },
1148 .init = {
1149 .reg = BD718XX_REG_BUCK2_CTRL,
1150 .mask = BD718XX_BUCK_SEL,
1151 .val = BD718XX_BUCK_SEL,
1152 },
1153 },
1154 {
1155 .desc = {
1156 .name = "buck3",
1157 .of_match = of_match_ptr("BUCK3"),
1158 .regulators_node = of_match_ptr("regulators"),
1159 .id = BD718XX_BUCK3,
1160 .type = REGULATOR_VOLTAGE,
1161 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1162 .linear_ranges = bd718xx_dvs_buck_volts,
1163 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1164 .vsel_reg = BD71837_REG_BUCK3_VOLT_RUN,
1165 .vsel_mask = DVS_BUCK_RUN_MASK,
1166 .enable_reg = BD71837_REG_BUCK3_CTRL,
1167 .enable_mask = BD718XX_BUCK_EN,
1168 .enable_time = BD71837_BUCK3_STARTUP_TIME,
1169 .ramp_delay_table = bd718xx_ramp_delay,
1170 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1171 .ramp_reg = BD71837_REG_BUCK3_CTRL,
1172 .ramp_mask = BUCK_RAMPRATE_MASK,
1173 .owner = THIS_MODULE,
1174 .of_parse_cb = buck_set_hw_dvs_levels,
1175 },
1176 .dvs = {
1177 .level_map = ROHM_DVS_LEVEL_RUN,
1178 .run_reg = BD71837_REG_BUCK3_VOLT_RUN,
1179 .run_mask = DVS_BUCK_RUN_MASK,
1180 },
1181 .init = {
1182 .reg = BD71837_REG_BUCK3_CTRL,
1183 .mask = BD718XX_BUCK_SEL,
1184 .val = BD718XX_BUCK_SEL,
1185 },
1186 },
1187 {
1188 .desc = {
1189 .name = "buck4",
1190 .of_match = of_match_ptr("BUCK4"),
1191 .regulators_node = of_match_ptr("regulators"),
1192 .id = BD718XX_BUCK4,
1193 .type = REGULATOR_VOLTAGE,
1194 .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1195 .linear_ranges = bd718xx_dvs_buck_volts,
1196 .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1197 .vsel_reg = BD71837_REG_BUCK4_VOLT_RUN,
1198 .vsel_mask = DVS_BUCK_RUN_MASK,
1199 .enable_reg = BD71837_REG_BUCK4_CTRL,
1200 .enable_mask = BD718XX_BUCK_EN,
1201 .enable_time = BD71837_BUCK4_STARTUP_TIME,
1202 .ramp_delay_table = bd718xx_ramp_delay,
1203 .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1204 .ramp_reg = BD71837_REG_BUCK4_CTRL,
1205 .ramp_mask = BUCK_RAMPRATE_MASK,
1206 .owner = THIS_MODULE,
1207 .of_parse_cb = buck_set_hw_dvs_levels,
1208 },
1209 .dvs = {
1210 .level_map = ROHM_DVS_LEVEL_RUN,
1211 .run_reg = BD71837_REG_BUCK4_VOLT_RUN,
1212 .run_mask = DVS_BUCK_RUN_MASK,
1213 },
1214 .init = {
1215 .reg = BD71837_REG_BUCK4_CTRL,
1216 .mask = BD718XX_BUCK_SEL,
1217 .val = BD718XX_BUCK_SEL,
1218 },
1219 },
1220 {
1221 .desc = {
1222 .name = "buck5",
1223 .of_match = of_match_ptr("BUCK5"),
1224 .regulators_node = of_match_ptr("regulators"),
1225 .id = BD718XX_BUCK5,
1226 .type = REGULATOR_VOLTAGE,
1227 .n_voltages = BD71837_BUCK5_VOLTAGE_NUM,
1228 .linear_ranges = bd71837_buck5_volts,
1229 .n_linear_ranges =
1230 ARRAY_SIZE(bd71837_buck5_volts),
1231 .vsel_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
1232 .vsel_mask = BD71837_BUCK5_MASK,
1233 .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
1234 .vsel_range_mask = BD71837_BUCK5_RANGE_MASK,
1235 .linear_range_selectors_bitfield = bd71837_buck5_volt_range_sel,
1236 .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
1237 .enable_mask = BD718XX_BUCK_EN,
1238 .enable_time = BD71837_BUCK5_STARTUP_TIME,
1239 .owner = THIS_MODULE,
1240 },
1241 .init = {
1242 .reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
1243 .mask = BD718XX_BUCK_SEL,
1244 .val = BD718XX_BUCK_SEL,
1245 },
1246 },
1247 {
1248 .desc = {
1249 .name = "buck6",
1250 .of_match = of_match_ptr("BUCK6"),
1251 .regulators_node = of_match_ptr("regulators"),
1252 .id = BD718XX_BUCK6,
1253 .type = REGULATOR_VOLTAGE,
1254 .n_voltages = BD71837_BUCK6_VOLTAGE_NUM,
1255 .linear_ranges = bd71837_buck6_volts,
1256 .n_linear_ranges =
1257 ARRAY_SIZE(bd71837_buck6_volts),
1258 .vsel_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
1259 .vsel_mask = BD71837_BUCK6_MASK,
1260 .enable_reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
1261 .enable_mask = BD718XX_BUCK_EN,
1262 .enable_time = BD71837_BUCK6_STARTUP_TIME,
1263 .owner = THIS_MODULE,
1264 },
1265 .init = {
1266 .reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
1267 .mask = BD718XX_BUCK_SEL,
1268 .val = BD718XX_BUCK_SEL,
1269 },
1270 },
1271 {
1272 .desc = {
1273 .name = "buck7",
1274 .of_match = of_match_ptr("BUCK7"),
1275 .regulators_node = of_match_ptr("regulators"),
1276 .id = BD718XX_BUCK7,
1277 .type = REGULATOR_VOLTAGE,
1278 .volt_table = &bd718xx_3rd_nodvs_buck_volts[0],
1279 .n_voltages = ARRAY_SIZE(bd718xx_3rd_nodvs_buck_volts),
1280 .vsel_reg = BD718XX_REG_3RD_NODVS_BUCK_VOLT,
1281 .vsel_mask = BD718XX_3RD_NODVS_BUCK_MASK,
1282 .enable_reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
1283 .enable_mask = BD718XX_BUCK_EN,
1284 .enable_time = BD71837_BUCK7_STARTUP_TIME,
1285 .owner = THIS_MODULE,
1286 },
1287 .init = {
1288 .reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
1289 .mask = BD718XX_BUCK_SEL,
1290 .val = BD718XX_BUCK_SEL,
1291 },
1292 },
1293 {
1294 .desc = {
1295 .name = "buck8",
1296 .of_match = of_match_ptr("BUCK8"),
1297 .regulators_node = of_match_ptr("regulators"),
1298 .id = BD718XX_BUCK8,
1299 .type = REGULATOR_VOLTAGE,
1300 .n_voltages = BD718XX_4TH_NODVS_BUCK_VOLTAGE_NUM,
1301 .linear_ranges = bd718xx_4th_nodvs_buck_volts,
1302 .n_linear_ranges =
1303 ARRAY_SIZE(bd718xx_4th_nodvs_buck_volts),
1304 .vsel_reg = BD718XX_REG_4TH_NODVS_BUCK_VOLT,
1305 .vsel_mask = BD718XX_4TH_NODVS_BUCK_MASK,
1306 .enable_reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
1307 .enable_mask = BD718XX_BUCK_EN,
1308 .enable_time = BD71837_BUCK8_STARTUP_TIME,
1309 .owner = THIS_MODULE,
1310 },
1311 .init = {
1312 .reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
1313 .mask = BD718XX_BUCK_SEL,
1314 .val = BD718XX_BUCK_SEL,
1315 },
1316 },
1317 {
1318 .desc = {
1319 .name = "ldo1",
1320 .of_match = of_match_ptr("LDO1"),
1321 .regulators_node = of_match_ptr("regulators"),
1322 .id = BD718XX_LDO1,
1323 .type = REGULATOR_VOLTAGE,
1324 .n_voltages = BD718XX_LDO1_VOLTAGE_NUM,
1325 .linear_ranges = bd718xx_ldo1_volts,
1326 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo1_volts),
1327 .vsel_reg = BD718XX_REG_LDO1_VOLT,
1328 .vsel_mask = BD718XX_LDO1_MASK,
1329 .vsel_range_reg = BD718XX_REG_LDO1_VOLT,
1330 .vsel_range_mask = BD718XX_LDO1_RANGE_MASK,
1331 .linear_range_selectors_bitfield = bd718xx_ldo1_volt_range_sel,
1332 .enable_reg = BD718XX_REG_LDO1_VOLT,
1333 .enable_mask = BD718XX_LDO_EN,
1334 .enable_time = BD71837_LDO1_STARTUP_TIME,
1335 .owner = THIS_MODULE,
1336 },
1337 .init = {
1338 .reg = BD718XX_REG_LDO1_VOLT,
1339 .mask = BD718XX_LDO_SEL,
1340 .val = BD718XX_LDO_SEL,
1341 },
1342 },
1343 {
1344 .desc = {
1345 .name = "ldo2",
1346 .of_match = of_match_ptr("LDO2"),
1347 .regulators_node = of_match_ptr("regulators"),
1348 .id = BD718XX_LDO2,
1349 .type = REGULATOR_VOLTAGE,
1350 .volt_table = &ldo_2_volts[0],
1351 .vsel_reg = BD718XX_REG_LDO2_VOLT,
1352 .vsel_mask = BD718XX_LDO2_MASK,
1353 .n_voltages = ARRAY_SIZE(ldo_2_volts),
1354 .enable_reg = BD718XX_REG_LDO2_VOLT,
1355 .enable_mask = BD718XX_LDO_EN,
1356 .enable_time = BD71837_LDO2_STARTUP_TIME,
1357 .owner = THIS_MODULE,
1358 },
1359 .init = {
1360 .reg = BD718XX_REG_LDO2_VOLT,
1361 .mask = BD718XX_LDO_SEL,
1362 .val = BD718XX_LDO_SEL,
1363 },
1364 },
1365 {
1366 .desc = {
1367 .name = "ldo3",
1368 .of_match = of_match_ptr("LDO3"),
1369 .regulators_node = of_match_ptr("regulators"),
1370 .id = BD718XX_LDO3,
1371 .type = REGULATOR_VOLTAGE,
1372 .n_voltages = BD718XX_LDO3_VOLTAGE_NUM,
1373 .linear_ranges = bd718xx_ldo3_volts,
1374 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo3_volts),
1375 .vsel_reg = BD718XX_REG_LDO3_VOLT,
1376 .vsel_mask = BD718XX_LDO3_MASK,
1377 .enable_reg = BD718XX_REG_LDO3_VOLT,
1378 .enable_mask = BD718XX_LDO_EN,
1379 .enable_time = BD71837_LDO3_STARTUP_TIME,
1380 .owner = THIS_MODULE,
1381 },
1382 .init = {
1383 .reg = BD718XX_REG_LDO3_VOLT,
1384 .mask = BD718XX_LDO_SEL,
1385 .val = BD718XX_LDO_SEL,
1386 },
1387 },
1388 {
1389 .desc = {
1390 .name = "ldo4",
1391 .of_match = of_match_ptr("LDO4"),
1392 .regulators_node = of_match_ptr("regulators"),
1393 .id = BD718XX_LDO4,
1394 .type = REGULATOR_VOLTAGE,
1395 .n_voltages = BD718XX_LDO4_VOLTAGE_NUM,
1396 .linear_ranges = bd718xx_ldo4_volts,
1397 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo4_volts),
1398 .vsel_reg = BD718XX_REG_LDO4_VOLT,
1399 .vsel_mask = BD718XX_LDO4_MASK,
1400 .enable_reg = BD718XX_REG_LDO4_VOLT,
1401 .enable_mask = BD718XX_LDO_EN,
1402 .enable_time = BD71837_LDO4_STARTUP_TIME,
1403 .owner = THIS_MODULE,
1404 },
1405 .init = {
1406 .reg = BD718XX_REG_LDO4_VOLT,
1407 .mask = BD718XX_LDO_SEL,
1408 .val = BD718XX_LDO_SEL,
1409 },
1410 },
1411 {
1412 .desc = {
1413 .name = "ldo5",
1414 .of_match = of_match_ptr("LDO5"),
1415 .regulators_node = of_match_ptr("regulators"),
1416 .id = BD718XX_LDO5,
1417 .type = REGULATOR_VOLTAGE,
1418 .n_voltages = BD71837_LDO5_VOLTAGE_NUM,
1419 .linear_ranges = bd71837_ldo5_volts,
1420 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo5_volts),
1421 /* LDO5 is supplied by buck6 */
1422 .supply_name = "buck6",
1423 .vsel_reg = BD718XX_REG_LDO5_VOLT,
1424 .vsel_mask = BD71837_LDO5_MASK,
1425 .enable_reg = BD718XX_REG_LDO5_VOLT,
1426 .enable_mask = BD718XX_LDO_EN,
1427 .enable_time = BD71837_LDO5_STARTUP_TIME,
1428 .owner = THIS_MODULE,
1429 },
1430 .init = {
1431 .reg = BD718XX_REG_LDO5_VOLT,
1432 .mask = BD718XX_LDO_SEL,
1433 .val = BD718XX_LDO_SEL,
1434 },
1435 .additional_inits = bd71837_ldo5_inits,
1436 .additional_init_amnt = ARRAY_SIZE(bd71837_ldo5_inits),
1437 },
1438 {
1439 .desc = {
1440 .name = "ldo6",
1441 .of_match = of_match_ptr("LDO6"),
1442 .regulators_node = of_match_ptr("regulators"),
1443 .id = BD718XX_LDO6,
1444 .type = REGULATOR_VOLTAGE,
1445 .n_voltages = BD718XX_LDO6_VOLTAGE_NUM,
1446 .linear_ranges = bd718xx_ldo6_volts,
1447 .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo6_volts),
1448 /* LDO6 is supplied by buck7 */
1449 .supply_name = "buck7",
1450 .vsel_reg = BD718XX_REG_LDO6_VOLT,
1451 .vsel_mask = BD718XX_LDO6_MASK,
1452 .enable_reg = BD718XX_REG_LDO6_VOLT,
1453 .enable_mask = BD718XX_LDO_EN,
1454 .enable_time = BD71837_LDO6_STARTUP_TIME,
1455 .owner = THIS_MODULE,
1456 },
1457 .init = {
1458 .reg = BD718XX_REG_LDO6_VOLT,
1459 .mask = BD718XX_LDO_SEL,
1460 .val = BD718XX_LDO_SEL,
1461 },
1462 .additional_inits = bd71837_ldo6_inits,
1463 .additional_init_amnt = ARRAY_SIZE(bd71837_ldo6_inits),
1464 },
1465 {
1466 .desc = {
1467 .name = "ldo7",
1468 .of_match = of_match_ptr("LDO7"),
1469 .regulators_node = of_match_ptr("regulators"),
1470 .id = BD718XX_LDO7,
1471 .type = REGULATOR_VOLTAGE,
1472 .n_voltages = BD71837_LDO7_VOLTAGE_NUM,
1473 .linear_ranges = bd71837_ldo7_volts,
1474 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo7_volts),
1475 .vsel_reg = BD71837_REG_LDO7_VOLT,
1476 .vsel_mask = BD71837_LDO7_MASK,
1477 .enable_reg = BD71837_REG_LDO7_VOLT,
1478 .enable_mask = BD718XX_LDO_EN,
1479 .enable_time = BD71837_LDO7_STARTUP_TIME,
1480 .owner = THIS_MODULE,
1481 },
1482 .init = {
1483 .reg = BD71837_REG_LDO7_VOLT,
1484 .mask = BD718XX_LDO_SEL,
1485 .val = BD718XX_LDO_SEL,
1486 },
1487 },
1488};
1489
1490static void mark_hw_controlled(struct device *dev, struct device_node *np,
1491 struct bd718xx_regulator_data *reg_data,
1492 unsigned int num_reg_data, int *info)
1493{
1494 int i;
1495
1496 for (i = 1; i <= num_reg_data; i++) {
1497 if (!of_node_name_eq(np, reg_data[i-1].desc.of_match))
1498 continue;
1499
1500 *info |= 1 << (i - 1);
1501 dev_dbg(dev, "regulator %d runlevel controlled\n", i);
1502 return;
1503 }
1504 dev_warn(dev, "Bad regulator node\n");
1505}
1506
1507/*
1508 * Setups where regulator (especially the buck8) output voltage is scaled
1509 * by adding external connection where some other regulator output is connected
1510 * to feedback-pin (over suitable resistors) is getting popular amongst users
1511 * of BD71837. (This allows for example scaling down the buck8 voltages to suit
1512 * lover GPU voltages for projects where buck8 is (ab)used to supply power
1513 * for GPU. Additionally some setups do allow DVS for buck8 but as this do
1514 * produce voltage spikes the HW must be evaluated to be able to survive this
1515 * - hence I keep the DVS disabled for non DVS bucks by default. I don't want
1516 * to help you burn your proto board)
1517 *
1518 * So we allow describing this external connection from DT and scale the
1519 * voltages accordingly. This is what the connection should look like:
1520 *
1521 * |------------|
1522 * | buck 8 |-------+----->Vout
1523 * | | |
1524 * |------------| |
1525 * | FB pin |
1526 * | |
1527 * +-------+--R2---+
1528 * |
1529 * R1
1530 * |
1531 * V FB-pull-up
1532 *
1533 * Here the buck output is sifted according to formula:
1534 *
1535 * Vout_o = Vo - (Vpu - Vo)*R2/R1
1536 * Linear_step = step_orig*(R1+R2)/R1
1537 *
1538 * where:
1539 * Vout_o is adjusted voltage output at vsel reg value 0
1540 * Vo is original voltage output at vsel reg value 0
1541 * Vpu is the pull-up voltage V FB-pull-up in the picture
1542 * R1 and R2 are resistor values.
1543 *
1544 * As a real world example for buck8 and a specific GPU:
1545 * VLDO = 1.6V (used as FB-pull-up)
1546 * R1 = 1000ohms
1547 * R2 = 150ohms
1548 * VSEL 0x0 => 0.8V – (VLDO – 0.8) * R2 / R1 = 0.68V
1549 * Linear Step = 10mV * (R1 + R2) / R1 = 11.5mV
1550 */
1551static int setup_feedback_loop(struct device *dev, struct device_node *np,
1552 struct bd718xx_regulator_data *reg_data,
1553 unsigned int num_reg_data, int fb_uv)
1554{
1555 int i, r1, r2, ret;
1556
1557 /*
1558 * We do adjust the values in the global desc based on DT settings.
1559 * This may not be best approach as it can cause problems if more than
1560 * one PMIC is controlled from same processor. I don't see such use-case
1561 * for BD718x7 now - so we spare some bits.
1562 *
1563 * If this will point out to be a problem - then we can allocate new
1564 * bd718xx_regulator_data array at probe and just use the global
1565 * array as a template where we copy initial values. Then we can
1566 * use allocated descs for regultor registration and do IC specific
1567 * modifications to this copy while leaving other PMICs untouched. But
1568 * that means allocating new array for each PMIC - and currently I see
1569 * no need for that.
1570 */
1571
1572 for (i = 0; i < num_reg_data; i++) {
1573 struct regulator_desc *desc = ®_data[i].desc;
1574 int j;
1575
1576 if (!of_node_name_eq(np, desc->of_match))
1577 continue;
1578
1579 /* The feedback loop connection does not make sense for LDOs */
1580 if (desc->id >= BD718XX_LDO1)
1581 return -EINVAL;
1582
1583 ret = of_property_read_u32(np, "rohm,feedback-pull-up-r1-ohms",
1584 &r1);
1585 if (ret)
1586 return ret;
1587
1588 if (!r1)
1589 return -EINVAL;
1590
1591 ret = of_property_read_u32(np, "rohm,feedback-pull-up-r2-ohms",
1592 &r2);
1593 if (ret)
1594 return ret;
1595
1596 if (desc->n_linear_ranges && desc->linear_ranges) {
1597 struct linear_range *new;
1598
1599 new = devm_kzalloc(dev, desc->n_linear_ranges *
1600 sizeof(struct linear_range),
1601 GFP_KERNEL);
1602 if (!new)
1603 return -ENOMEM;
1604
1605 for (j = 0; j < desc->n_linear_ranges; j++) {
1606 int min = desc->linear_ranges[j].min;
1607 int step = desc->linear_ranges[j].step;
1608
1609 min -= (fb_uv - min)*r2/r1;
1610 step = step * (r1 + r2);
1611 step /= r1;
1612
1613 new[j].min = min;
1614 new[j].step = step;
1615
1616 dev_dbg(dev, "%s: old range min %d, step %d\n",
1617 desc->name, desc->linear_ranges[j].min,
1618 desc->linear_ranges[j].step);
1619 dev_dbg(dev, "new range min %d, step %d\n", min,
1620 step);
1621 }
1622 desc->linear_ranges = new;
1623 }
1624 dev_dbg(dev, "regulator '%s' has FB pull-up configured\n",
1625 desc->name);
1626
1627 return 0;
1628 }
1629
1630 return -ENODEV;
1631}
1632
1633static int get_special_regulators(struct device *dev,
1634 struct bd718xx_regulator_data *reg_data,
1635 unsigned int num_reg_data, int *info)
1636{
1637 int ret;
1638 struct device_node *np;
1639 struct device_node *nproot = dev->of_node;
1640 int uv;
1641
1642 *info = 0;
1643
1644 nproot = of_get_child_by_name(nproot, "regulators");
1645 if (!nproot) {
1646 dev_err(dev, "failed to find regulators node\n");
1647 return -ENODEV;
1648 }
1649 for_each_child_of_node(nproot, np) {
1650 if (of_property_read_bool(np, "rohm,no-regulator-enable-control"))
1651 mark_hw_controlled(dev, np, reg_data, num_reg_data,
1652 info);
1653 ret = of_property_read_u32(np, "rohm,fb-pull-up-microvolt",
1654 &uv);
1655 if (ret) {
1656 if (ret == -EINVAL)
1657 continue;
1658 else
1659 goto err_out;
1660 }
1661
1662 ret = setup_feedback_loop(dev, np, reg_data, num_reg_data, uv);
1663 if (ret)
1664 goto err_out;
1665 }
1666
1667 of_node_put(nproot);
1668 return 0;
1669
1670err_out:
1671 of_node_put(np);
1672 of_node_put(nproot);
1673
1674 return ret;
1675}
1676
1677static int bd718xx_probe(struct platform_device *pdev)
1678{
1679 struct regmap *regmap;
1680 struct regulator_config config = { 0 };
1681 int i, j, err, omit_enable;
1682 bool use_snvs;
1683 struct bd718xx_regulator_data *reg_data;
1684 unsigned int num_reg_data;
1685 enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
1686 const struct regulator_ops **swops, **hwops;
1687
1688 regmap = dev_get_regmap(pdev->dev.parent, NULL);
1689 if (!regmap) {
1690 dev_err(&pdev->dev, "No MFD driver data\n");
1691 return -EINVAL;
1692 }
1693
1694 switch (chip) {
1695 case ROHM_CHIP_TYPE_BD71837:
1696 reg_data = bd71837_regulators;
1697 num_reg_data = ARRAY_SIZE(bd71837_regulators);
1698 swops = &bd71837_swcontrol_ops[0];
1699 hwops = &bd71837_hwcontrol_ops[0];
1700 break;
1701 case ROHM_CHIP_TYPE_BD71847:
1702 reg_data = bd71847_regulators;
1703 num_reg_data = ARRAY_SIZE(bd71847_regulators);
1704 swops = &bd71847_swcontrol_ops[0];
1705 hwops = &bd71847_hwcontrol_ops[0];
1706 break;
1707 default:
1708 dev_err(&pdev->dev, "Unsupported chip type\n");
1709 return -EINVAL;
1710 }
1711
1712 /* Register LOCK release */
1713 err = regmap_update_bits(regmap, BD718XX_REG_REGLOCK,
1714 (REGLOCK_PWRSEQ | REGLOCK_VREG), 0);
1715 if (err)
1716 return dev_err_probe(&pdev->dev, err, "Failed to unlock PMIC\n");
1717
1718 dev_dbg(&pdev->dev, "Unlocked lock register 0x%x\n",
1719 BD718XX_REG_REGLOCK);
1720
1721 use_snvs = of_property_read_bool(pdev->dev.parent->of_node,
1722 "rohm,reset-snvs-powered");
1723
1724 /*
1725 * Change the next stage from poweroff to be READY instead of SNVS
1726 * for all reset types because OTP loading at READY will clear SEL
1727 * bit allowing HW defaults for power rails to be used
1728 */
1729 if (!use_snvs) {
1730 err = regmap_update_bits(regmap, BD718XX_REG_TRANS_COND1,
1731 BD718XX_ON_REQ_POWEROFF_MASK |
1732 BD718XX_SWRESET_POWEROFF_MASK |
1733 BD718XX_WDOG_POWEROFF_MASK |
1734 BD718XX_KEY_L_POWEROFF_MASK,
1735 BD718XX_POWOFF_TO_RDY);
1736 if (err)
1737 return dev_err_probe(&pdev->dev, err,
1738 "Failed to change reset target\n");
1739
1740 dev_dbg(&pdev->dev, "Changed all resets from SVNS to READY\n");
1741 }
1742
1743 config.dev = pdev->dev.parent;
1744 config.regmap = regmap;
1745 /*
1746 * There are cases when we want to leave the enable-control for
1747 * the HW state machine and use this driver only for voltage control.
1748 * One special case is when we use PMIC_STBY_REQ line from SoC to PMIC
1749 * in order to set the system to SUSPEND state.
1750 *
1751 * If regulator is taken under SW control the regulator state will not
1752 * be affected by PMIC state machine - Eg. regulator is likely to stay
1753 * on even in SUSPEND
1754 */
1755 err = get_special_regulators(pdev->dev.parent, reg_data, num_reg_data,
1756 &omit_enable);
1757 if (err)
1758 return err;
1759
1760 for (i = 0; i < num_reg_data; i++) {
1761
1762 struct regulator_desc *desc;
1763 struct regulator_dev *rdev;
1764 struct bd718xx_regulator_data *r;
1765 int no_enable_control = omit_enable & (1 << i);
1766
1767 r = ®_data[i];
1768 desc = &r->desc;
1769
1770 if (no_enable_control)
1771 desc->ops = hwops[i];
1772 else
1773 desc->ops = swops[i];
1774
1775 rdev = devm_regulator_register(&pdev->dev, desc, &config);
1776 if (IS_ERR(rdev))
1777 return dev_err_probe(&pdev->dev, PTR_ERR(rdev),
1778 "failed to register %s regulator\n",
1779 desc->name);
1780
1781 /*
1782 * Regulator register gets the regulator constraints and
1783 * applies them (set_machine_constraints). This should have
1784 * turned the control register(s) to correct values and we
1785 * can now switch the control from PMIC state machine to the
1786 * register interface
1787 *
1788 * At poweroff transition PMIC HW disables EN bit for
1789 * regulators but leaves SEL bit untouched. So if state
1790 * transition from POWEROFF is done to SNVS - then all power
1791 * rails controlled by SW (having SEL bit set) stay disabled
1792 * as EN is cleared. This will result boot failure if any
1793 * crucial systems are powered by these rails. We don't
1794 * enable SW control for crucial regulators if snvs state is
1795 * used
1796 */
1797 if (!no_enable_control && (!use_snvs ||
1798 !rdev->constraints->always_on ||
1799 !rdev->constraints->boot_on)) {
1800 err = regmap_update_bits(regmap, r->init.reg,
1801 r->init.mask, r->init.val);
1802 if (err)
1803 return dev_err_probe(&pdev->dev, err,
1804 "Failed to take control for (%s)\n",
1805 desc->name);
1806 }
1807 for (j = 0; j < r->additional_init_amnt; j++) {
1808 err = regmap_update_bits(regmap,
1809 r->additional_inits[j].reg,
1810 r->additional_inits[j].mask,
1811 r->additional_inits[j].val);
1812 if (err)
1813 return dev_err_probe(&pdev->dev, err,
1814 "Buck (%s) initialization failed\n",
1815 desc->name);
1816 }
1817 }
1818
1819 return err;
1820}
1821
1822static const struct platform_device_id bd718x7_pmic_id[] = {
1823 { "bd71837-pmic", ROHM_CHIP_TYPE_BD71837 },
1824 { "bd71847-pmic", ROHM_CHIP_TYPE_BD71847 },
1825 { },
1826};
1827MODULE_DEVICE_TABLE(platform, bd718x7_pmic_id);
1828
1829static struct platform_driver bd718xx_regulator = {
1830 .driver = {
1831 .name = "bd718xx-pmic",
1832 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1833 },
1834 .probe = bd718xx_probe,
1835 .id_table = bd718x7_pmic_id,
1836};
1837
1838module_platform_driver(bd718xx_regulator);
1839
1840MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1841MODULE_DESCRIPTION("BD71837/BD71847 voltage regulator driver");
1842MODULE_LICENSE("GPL");
1843MODULE_ALIAS("platform:bd718xx-pmic");