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