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