1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 ROHM Semiconductors
3 // bd71837-regulator.c ROHM BD71837MWV regulator driver
4 
5 #include <linux/delay.h>
6 #include <linux/err.h>
7 #include <linux/gpio.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/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 struct bd71837_pmic {
19 	struct regulator_desc descs[BD71837_REGULATOR_CNT];
20 	struct bd71837 *mfd;
21 	struct platform_device *pdev;
22 	struct regulator_dev *rdev[BD71837_REGULATOR_CNT];
23 };
24 
25 /*
26  * BUCK1/2/3/4
27  * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
28  * 00: 10.00mV/usec 10mV 1uS
29  * 01: 5.00mV/usec	10mV 2uS
30  * 10: 2.50mV/usec	10mV 4uS
31  * 11: 1.25mV/usec	10mV 8uS
32  */
bd71837_buck1234_set_ramp_delay(struct regulator_dev * rdev,int ramp_delay)33 static int bd71837_buck1234_set_ramp_delay(struct regulator_dev *rdev,
34 					   int ramp_delay)
35 {
36 	struct bd71837_pmic *pmic = rdev_get_drvdata(rdev);
37 	struct bd71837 *mfd = pmic->mfd;
38 	int id = rdev->desc->id;
39 	unsigned int ramp_value = BUCK_RAMPRATE_10P00MV;
40 
41 	dev_dbg(&pmic->pdev->dev, "Buck[%d] Set Ramp = %d\n", id + 1,
42 		ramp_delay);
43 	switch (ramp_delay) {
44 	case 1 ... 1250:
45 		ramp_value = BUCK_RAMPRATE_1P25MV;
46 		break;
47 	case 1251 ... 2500:
48 		ramp_value = BUCK_RAMPRATE_2P50MV;
49 		break;
50 	case 2501 ... 5000:
51 		ramp_value = BUCK_RAMPRATE_5P00MV;
52 		break;
53 	case 5001 ... 10000:
54 		ramp_value = BUCK_RAMPRATE_10P00MV;
55 		break;
56 	default:
57 		ramp_value = BUCK_RAMPRATE_10P00MV;
58 		dev_err(&pmic->pdev->dev,
59 			"%s: ramp_delay: %d not supported, setting 10000mV//us\n",
60 			rdev->desc->name, ramp_delay);
61 	}
62 
63 	return regmap_update_bits(mfd->regmap, BD71837_REG_BUCK1_CTRL + id,
64 				  BUCK_RAMPRATE_MASK, ramp_value << 6);
65 }
66 
67 /* Bucks 1 to 4 support DVS. PWM mode is used when voltage is changed.
68  * Bucks 5 to 8 and LDOs can use PFM and must be disabled when voltage
69  * is changed. Hence we return -EBUSY for these if voltage is changed
70  * when BUCK/LDO is enabled.
71  */
bd71837_set_voltage_sel_restricted(struct regulator_dev * rdev,unsigned int sel)72 static int bd71837_set_voltage_sel_restricted(struct regulator_dev *rdev,
73 						    unsigned int sel)
74 {
75 	if (regulator_is_enabled_regmap(rdev))
76 		return -EBUSY;
77 
78 	return regulator_set_voltage_sel_regmap(rdev, sel);
79 }
80 
81 static struct regulator_ops bd71837_ldo_regulator_ops = {
82 	.enable = regulator_enable_regmap,
83 	.disable = regulator_disable_regmap,
84 	.is_enabled = regulator_is_enabled_regmap,
85 	.list_voltage = regulator_list_voltage_linear_range,
86 	.set_voltage_sel = bd71837_set_voltage_sel_restricted,
87 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
88 };
89 
90 static struct regulator_ops bd71837_ldo_regulator_nolinear_ops = {
91 	.enable = regulator_enable_regmap,
92 	.disable = regulator_disable_regmap,
93 	.is_enabled = regulator_is_enabled_regmap,
94 	.list_voltage = regulator_list_voltage_table,
95 	.set_voltage_sel = bd71837_set_voltage_sel_restricted,
96 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
97 };
98 
99 static struct regulator_ops bd71837_buck_regulator_ops = {
100 	.enable = regulator_enable_regmap,
101 	.disable = regulator_disable_regmap,
102 	.is_enabled = regulator_is_enabled_regmap,
103 	.list_voltage = regulator_list_voltage_linear_range,
104 	.set_voltage_sel = bd71837_set_voltage_sel_restricted,
105 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
106 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
107 };
108 
109 static struct regulator_ops bd71837_buck_regulator_nolinear_ops = {
110 	.enable = regulator_enable_regmap,
111 	.disable = regulator_disable_regmap,
112 	.is_enabled = regulator_is_enabled_regmap,
113 	.list_voltage = regulator_list_voltage_table,
114 	.set_voltage_sel = bd71837_set_voltage_sel_restricted,
115 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
116 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
117 };
118 
119 static struct regulator_ops bd71837_buck1234_regulator_ops = {
120 	.enable = regulator_enable_regmap,
121 	.disable = regulator_disable_regmap,
122 	.is_enabled = regulator_is_enabled_regmap,
123 	.list_voltage = regulator_list_voltage_linear_range,
124 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
125 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
126 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
127 	.set_ramp_delay = bd71837_buck1234_set_ramp_delay,
128 };
129 
130 /*
131  * BUCK1/2/3/4
132  * 0.70 to 1.30V (10mV step)
133  */
134 static const struct regulator_linear_range bd71837_buck1234_voltage_ranges[] = {
135 	REGULATOR_LINEAR_RANGE(700000, 0x00, 0x3C, 10000),
136 	REGULATOR_LINEAR_RANGE(1300000, 0x3D, 0x3F, 0),
137 };
138 
139 /*
140  * BUCK5
141  * 0.9V to 1.35V ()
142  */
143 static const struct regulator_linear_range bd71837_buck5_voltage_ranges[] = {
144 	REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
145 	REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
146 	REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
147 };
148 
149 /*
150  * BUCK6
151  * 3.0V to 3.3V (step 100mV)
152  */
153 static const struct regulator_linear_range bd71837_buck6_voltage_ranges[] = {
154 	REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
155 };
156 
157 /*
158  * BUCK7
159  * 000 = 1.605V
160  * 001 = 1.695V
161  * 010 = 1.755V
162  * 011 = 1.8V (Initial)
163  * 100 = 1.845V
164  * 101 = 1.905V
165  * 110 = 1.95V
166  * 111 = 1.995V
167  */
168 static const unsigned int buck_7_volts[] = {
169 	1605000, 1695000, 1755000, 1800000, 1845000, 1905000, 1950000, 1995000
170 };
171 
172 /*
173  * BUCK8
174  * 0.8V to 1.40V (step 10mV)
175  */
176 static const struct regulator_linear_range bd71837_buck8_voltage_ranges[] = {
177 	REGULATOR_LINEAR_RANGE(800000, 0x00, 0x3C, 10000),
178 	REGULATOR_LINEAR_RANGE(1400000, 0x3D, 0x3F, 0),
179 };
180 
181 /*
182  * LDO1
183  * 3.0 to 3.3V (100mV step)
184  */
185 static const struct regulator_linear_range bd71837_ldo1_voltage_ranges[] = {
186 	REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
187 };
188 
189 /*
190  * LDO2
191  * 0.8 or 0.9V
192  */
193 static const unsigned int ldo_2_volts[] = {
194 	900000, 800000
195 };
196 
197 /*
198  * LDO3
199  * 1.8 to 3.3V (100mV step)
200  */
201 static const struct regulator_linear_range bd71837_ldo3_voltage_ranges[] = {
202 	REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
203 };
204 
205 /*
206  * LDO4
207  * 0.9 to 1.8V (100mV step)
208  */
209 static const struct regulator_linear_range bd71837_ldo4_voltage_ranges[] = {
210 	REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
211 	REGULATOR_LINEAR_RANGE(1800000, 0x0A, 0x0F, 0),
212 };
213 
214 /*
215  * LDO5
216  * 1.8 to 3.3V (100mV step)
217  */
218 static const struct regulator_linear_range bd71837_ldo5_voltage_ranges[] = {
219 	REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
220 };
221 
222 /*
223  * LDO6
224  * 0.9 to 1.8V (100mV step)
225  */
226 static const struct regulator_linear_range bd71837_ldo6_voltage_ranges[] = {
227 	REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
228 	REGULATOR_LINEAR_RANGE(1800000, 0x0A, 0x0F, 0),
229 };
230 
231 /*
232  * LDO7
233  * 1.8 to 3.3V (100mV step)
234  */
235 static const struct regulator_linear_range bd71837_ldo7_voltage_ranges[] = {
236 	REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
237 };
238 
239 static const struct regulator_desc bd71837_regulators[] = {
240 	{
241 		.name = "buck1",
242 		.of_match = of_match_ptr("BUCK1"),
243 		.regulators_node = of_match_ptr("regulators"),
244 		.id = BD71837_BUCK1,
245 		.ops = &bd71837_buck1234_regulator_ops,
246 		.type = REGULATOR_VOLTAGE,
247 		.n_voltages = BD71837_BUCK1_VOLTAGE_NUM,
248 		.linear_ranges = bd71837_buck1234_voltage_ranges,
249 		.n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
250 		.vsel_reg = BD71837_REG_BUCK1_VOLT_RUN,
251 		.vsel_mask = BUCK1_RUN_MASK,
252 		.enable_reg = BD71837_REG_BUCK1_CTRL,
253 		.enable_mask = BD71837_BUCK_EN,
254 		.owner = THIS_MODULE,
255 	},
256 	{
257 		.name = "buck2",
258 		.of_match = of_match_ptr("BUCK2"),
259 		.regulators_node = of_match_ptr("regulators"),
260 		.id = BD71837_BUCK2,
261 		.ops = &bd71837_buck1234_regulator_ops,
262 		.type = REGULATOR_VOLTAGE,
263 		.n_voltages = BD71837_BUCK2_VOLTAGE_NUM,
264 		.linear_ranges = bd71837_buck1234_voltage_ranges,
265 		.n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
266 		.vsel_reg = BD71837_REG_BUCK2_VOLT_RUN,
267 		.vsel_mask = BUCK2_RUN_MASK,
268 		.enable_reg = BD71837_REG_BUCK2_CTRL,
269 		.enable_mask = BD71837_BUCK_EN,
270 		.owner = THIS_MODULE,
271 	},
272 	{
273 		.name = "buck3",
274 		.of_match = of_match_ptr("BUCK3"),
275 		.regulators_node = of_match_ptr("regulators"),
276 		.id = BD71837_BUCK3,
277 		.ops = &bd71837_buck1234_regulator_ops,
278 		.type = REGULATOR_VOLTAGE,
279 		.n_voltages = BD71837_BUCK3_VOLTAGE_NUM,
280 		.linear_ranges = bd71837_buck1234_voltage_ranges,
281 		.n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
282 		.vsel_reg = BD71837_REG_BUCK3_VOLT_RUN,
283 		.vsel_mask = BUCK3_RUN_MASK,
284 		.enable_reg = BD71837_REG_BUCK3_CTRL,
285 		.enable_mask = BD71837_BUCK_EN,
286 		.owner = THIS_MODULE,
287 	},
288 	{
289 		.name = "buck4",
290 		.of_match = of_match_ptr("BUCK4"),
291 		.regulators_node = of_match_ptr("regulators"),
292 			.id = BD71837_BUCK4,
293 		.ops = &bd71837_buck1234_regulator_ops,
294 		.type = REGULATOR_VOLTAGE,
295 		.n_voltages = BD71837_BUCK4_VOLTAGE_NUM,
296 		.linear_ranges = bd71837_buck1234_voltage_ranges,
297 		.n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
298 		.vsel_reg = BD71837_REG_BUCK4_VOLT_RUN,
299 		.vsel_mask = BUCK4_RUN_MASK,
300 		.enable_reg = BD71837_REG_BUCK4_CTRL,
301 		.enable_mask = BD71837_BUCK_EN,
302 		.owner = THIS_MODULE,
303 	},
304 	{
305 		.name = "buck5",
306 		.of_match = of_match_ptr("BUCK5"),
307 		.regulators_node = of_match_ptr("regulators"),
308 		.id = BD71837_BUCK5,
309 		.ops = &bd71837_buck_regulator_ops,
310 		.type = REGULATOR_VOLTAGE,
311 		.n_voltages = BD71837_BUCK5_VOLTAGE_NUM,
312 		.linear_ranges = bd71837_buck5_voltage_ranges,
313 		.n_linear_ranges = ARRAY_SIZE(bd71837_buck5_voltage_ranges),
314 		.vsel_reg = BD71837_REG_BUCK5_VOLT,
315 		.vsel_mask = BUCK5_MASK,
316 		.enable_reg = BD71837_REG_BUCK5_CTRL,
317 		.enable_mask = BD71837_BUCK_EN,
318 		.owner = THIS_MODULE,
319 	},
320 	{
321 		.name = "buck6",
322 		.of_match = of_match_ptr("BUCK6"),
323 		.regulators_node = of_match_ptr("regulators"),
324 		.id = BD71837_BUCK6,
325 		.ops = &bd71837_buck_regulator_ops,
326 		.type = REGULATOR_VOLTAGE,
327 		.n_voltages = BD71837_BUCK6_VOLTAGE_NUM,
328 		.linear_ranges = bd71837_buck6_voltage_ranges,
329 		.n_linear_ranges = ARRAY_SIZE(bd71837_buck6_voltage_ranges),
330 		.vsel_reg = BD71837_REG_BUCK6_VOLT,
331 		.vsel_mask = BUCK6_MASK,
332 		.enable_reg = BD71837_REG_BUCK6_CTRL,
333 		.enable_mask = BD71837_BUCK_EN,
334 		.owner = THIS_MODULE,
335 	},
336 	{
337 		.name = "buck7",
338 		.of_match = of_match_ptr("BUCK7"),
339 		.regulators_node = of_match_ptr("regulators"),
340 		.id = BD71837_BUCK7,
341 		.ops = &bd71837_buck_regulator_nolinear_ops,
342 		.type = REGULATOR_VOLTAGE,
343 		.volt_table = &buck_7_volts[0],
344 		.n_voltages = ARRAY_SIZE(buck_7_volts),
345 		.vsel_reg = BD71837_REG_BUCK7_VOLT,
346 		.vsel_mask = BUCK7_MASK,
347 		.enable_reg = BD71837_REG_BUCK7_CTRL,
348 		.enable_mask = BD71837_BUCK_EN,
349 		.owner = THIS_MODULE,
350 	},
351 	{
352 		.name = "buck8",
353 		.of_match = of_match_ptr("BUCK8"),
354 		.regulators_node = of_match_ptr("regulators"),
355 		.id = BD71837_BUCK8,
356 		.ops = &bd71837_buck_regulator_ops,
357 		.type = REGULATOR_VOLTAGE,
358 		.n_voltages = BD71837_BUCK8_VOLTAGE_NUM,
359 		.linear_ranges = bd71837_buck8_voltage_ranges,
360 		.n_linear_ranges = ARRAY_SIZE(bd71837_buck8_voltage_ranges),
361 		.vsel_reg = BD71837_REG_BUCK8_VOLT,
362 		.vsel_mask = BUCK8_MASK,
363 		.enable_reg = BD71837_REG_BUCK8_CTRL,
364 		.enable_mask = BD71837_BUCK_EN,
365 		.owner = THIS_MODULE,
366 	},
367 	{
368 		.name = "ldo1",
369 		.of_match = of_match_ptr("LDO1"),
370 		.regulators_node = of_match_ptr("regulators"),
371 		.id = BD71837_LDO1,
372 		.ops = &bd71837_ldo_regulator_ops,
373 		.type = REGULATOR_VOLTAGE,
374 		.n_voltages = BD71837_LDO1_VOLTAGE_NUM,
375 		.linear_ranges = bd71837_ldo1_voltage_ranges,
376 		.n_linear_ranges = ARRAY_SIZE(bd71837_ldo1_voltage_ranges),
377 		.vsel_reg = BD71837_REG_LDO1_VOLT,
378 		.vsel_mask = LDO1_MASK,
379 		.enable_reg = BD71837_REG_LDO1_VOLT,
380 		.enable_mask = BD71837_LDO_EN,
381 		.owner = THIS_MODULE,
382 	},
383 	{
384 		.name = "ldo2",
385 		.of_match = of_match_ptr("LDO2"),
386 		.regulators_node = of_match_ptr("regulators"),
387 		.id = BD71837_LDO2,
388 		.ops = &bd71837_ldo_regulator_nolinear_ops,
389 		.type = REGULATOR_VOLTAGE,
390 		.volt_table = &ldo_2_volts[0],
391 		.vsel_reg = BD71837_REG_LDO2_VOLT,
392 		.vsel_mask = LDO2_MASK,
393 		.n_voltages = ARRAY_SIZE(ldo_2_volts),
394 		.n_voltages = BD71837_LDO2_VOLTAGE_NUM,
395 		.enable_reg = BD71837_REG_LDO2_VOLT,
396 		.enable_mask = BD71837_LDO_EN,
397 		.owner = THIS_MODULE,
398 	},
399 	{
400 		.name = "ldo3",
401 		.of_match = of_match_ptr("LDO3"),
402 		.regulators_node = of_match_ptr("regulators"),
403 		.id = BD71837_LDO3,
404 		.ops = &bd71837_ldo_regulator_ops,
405 		.type = REGULATOR_VOLTAGE,
406 		.n_voltages = BD71837_LDO3_VOLTAGE_NUM,
407 		.linear_ranges = bd71837_ldo3_voltage_ranges,
408 		.n_linear_ranges = ARRAY_SIZE(bd71837_ldo3_voltage_ranges),
409 		.vsel_reg = BD71837_REG_LDO3_VOLT,
410 		.vsel_mask = LDO3_MASK,
411 		.enable_reg = BD71837_REG_LDO3_VOLT,
412 		.enable_mask = BD71837_LDO_EN,
413 		.owner = THIS_MODULE,
414 	},
415 	{
416 		.name = "ldo4",
417 		.of_match = of_match_ptr("LDO4"),
418 		.regulators_node = of_match_ptr("regulators"),
419 		.id = BD71837_LDO4,
420 		.ops = &bd71837_ldo_regulator_ops,
421 		.type = REGULATOR_VOLTAGE,
422 		.n_voltages = BD71837_LDO4_VOLTAGE_NUM,
423 		.linear_ranges = bd71837_ldo4_voltage_ranges,
424 		.n_linear_ranges = ARRAY_SIZE(bd71837_ldo4_voltage_ranges),
425 		.vsel_reg = BD71837_REG_LDO4_VOLT,
426 		.vsel_mask = LDO4_MASK,
427 		.enable_reg = BD71837_REG_LDO4_VOLT,
428 		.enable_mask = BD71837_LDO_EN,
429 		.owner = THIS_MODULE,
430 	},
431 	{
432 		.name = "ldo5",
433 		.of_match = of_match_ptr("LDO5"),
434 		.regulators_node = of_match_ptr("regulators"),
435 		.id = BD71837_LDO5,
436 		.ops = &bd71837_ldo_regulator_ops,
437 		.type = REGULATOR_VOLTAGE,
438 		.n_voltages = BD71837_LDO5_VOLTAGE_NUM,
439 		.linear_ranges = bd71837_ldo5_voltage_ranges,
440 		.n_linear_ranges = ARRAY_SIZE(bd71837_ldo5_voltage_ranges),
441 		/* LDO5 is supplied by buck6 */
442 		.supply_name = "buck6",
443 		.vsel_reg = BD71837_REG_LDO5_VOLT,
444 		.vsel_mask = LDO5_MASK,
445 		.enable_reg = BD71837_REG_LDO5_VOLT,
446 		.enable_mask = BD71837_LDO_EN,
447 		.owner = THIS_MODULE,
448 	},
449 	{
450 		.name = "ldo6",
451 		.of_match = of_match_ptr("LDO6"),
452 		.regulators_node = of_match_ptr("regulators"),
453 		.id = BD71837_LDO6,
454 		.ops = &bd71837_ldo_regulator_ops,
455 		.type = REGULATOR_VOLTAGE,
456 		.n_voltages = BD71837_LDO6_VOLTAGE_NUM,
457 		.linear_ranges = bd71837_ldo6_voltage_ranges,
458 		.n_linear_ranges = ARRAY_SIZE(bd71837_ldo6_voltage_ranges),
459 		/* LDO6 is supplied by buck7 */
460 		.supply_name = "buck7",
461 		.vsel_reg = BD71837_REG_LDO6_VOLT,
462 		.vsel_mask = LDO6_MASK,
463 		.enable_reg = BD71837_REG_LDO6_VOLT,
464 		.enable_mask = BD71837_LDO_EN,
465 		.owner = THIS_MODULE,
466 	},
467 	{
468 		.name = "ldo7",
469 		.of_match = of_match_ptr("LDO7"),
470 		.regulators_node = of_match_ptr("regulators"),
471 		.id = BD71837_LDO7,
472 		.ops = &bd71837_ldo_regulator_ops,
473 		.type = REGULATOR_VOLTAGE,
474 		.n_voltages = BD71837_LDO7_VOLTAGE_NUM,
475 		.linear_ranges = bd71837_ldo7_voltage_ranges,
476 		.n_linear_ranges = ARRAY_SIZE(bd71837_ldo7_voltage_ranges),
477 		.vsel_reg = BD71837_REG_LDO7_VOLT,
478 		.vsel_mask = LDO7_MASK,
479 		.enable_reg = BD71837_REG_LDO7_VOLT,
480 		.enable_mask = BD71837_LDO_EN,
481 		.owner = THIS_MODULE,
482 	},
483 };
484 
485 struct reg_init {
486 	unsigned int reg;
487 	unsigned int mask;
488 };
489 
bd71837_probe(struct platform_device * pdev)490 static int bd71837_probe(struct platform_device *pdev)
491 {
492 	struct bd71837_pmic *pmic;
493 	struct regulator_config config = { 0 };
494 	struct reg_init pmic_regulator_inits[] = {
495 		{
496 			.reg = BD71837_REG_BUCK1_CTRL,
497 			.mask = BD71837_BUCK_SEL,
498 		}, {
499 			.reg = BD71837_REG_BUCK2_CTRL,
500 			.mask = BD71837_BUCK_SEL,
501 		}, {
502 			.reg = BD71837_REG_BUCK3_CTRL,
503 			.mask = BD71837_BUCK_SEL,
504 		}, {
505 			.reg = BD71837_REG_BUCK4_CTRL,
506 			.mask = BD71837_BUCK_SEL,
507 		}, {
508 			.reg = BD71837_REG_BUCK5_CTRL,
509 			.mask = BD71837_BUCK_SEL,
510 		}, {
511 			.reg = BD71837_REG_BUCK6_CTRL,
512 			.mask = BD71837_BUCK_SEL,
513 		}, {
514 			.reg = BD71837_REG_BUCK7_CTRL,
515 			.mask = BD71837_BUCK_SEL,
516 		}, {
517 			.reg = BD71837_REG_BUCK8_CTRL,
518 			.mask = BD71837_BUCK_SEL,
519 		}, {
520 			.reg = BD71837_REG_LDO1_VOLT,
521 			.mask = BD71837_LDO_SEL,
522 		}, {
523 			.reg = BD71837_REG_LDO2_VOLT,
524 			.mask = BD71837_LDO_SEL,
525 		}, {
526 			.reg = BD71837_REG_LDO3_VOLT,
527 			.mask = BD71837_LDO_SEL,
528 		}, {
529 			.reg = BD71837_REG_LDO4_VOLT,
530 			.mask = BD71837_LDO_SEL,
531 		}, {
532 			.reg = BD71837_REG_LDO5_VOLT,
533 			.mask = BD71837_LDO_SEL,
534 		}, {
535 			.reg = BD71837_REG_LDO6_VOLT,
536 			.mask = BD71837_LDO_SEL,
537 		}, {
538 			.reg = BD71837_REG_LDO7_VOLT,
539 			.mask = BD71837_LDO_SEL,
540 		}
541 	};
542 
543 	int i, err;
544 
545 	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
546 	if (!pmic)
547 		return -ENOMEM;
548 
549 	memcpy(pmic->descs, bd71837_regulators, sizeof(pmic->descs));
550 
551 	pmic->pdev = pdev;
552 	pmic->mfd = dev_get_drvdata(pdev->dev.parent);
553 
554 	if (!pmic->mfd) {
555 		dev_err(&pdev->dev, "No MFD driver data\n");
556 		err = -EINVAL;
557 		goto err;
558 	}
559 	platform_set_drvdata(pdev, pmic);
560 
561 	/* Register LOCK release */
562 	err = regmap_update_bits(pmic->mfd->regmap, BD71837_REG_REGLOCK,
563 				 (REGLOCK_PWRSEQ | REGLOCK_VREG), 0);
564 	if (err) {
565 		dev_err(&pmic->pdev->dev, "Failed to unlock PMIC (%d)\n", err);
566 		goto err;
567 	} else {
568 		dev_dbg(&pmic->pdev->dev, "Unlocked lock register 0x%x\n",
569 			BD71837_REG_REGLOCK);
570 	}
571 
572 	/*
573 	 * There is a HW quirk in BD71837. The shutdown sequence timings for
574 	 * bucks/LDOs which are controlled via register interface are changed.
575 	 * At PMIC poweroff the voltage for BUCK6/7 is cut immediately at the
576 	 * beginning of shut-down sequence. As bucks 6 and 7 are parent
577 	 * supplies for LDO5 and LDO6 - this causes LDO5/6 voltage
578 	 * monitoring to errorneously detect under voltage and force PMIC to
579 	 * emergency state instead of poweroff. In order to avoid this we
580 	 * disable voltage monitoring for LDO5 and LDO6
581 	 */
582 	err = regmap_update_bits(pmic->mfd->regmap, BD718XX_REG_MVRFLTMASK2,
583 				 BD718XX_LDO5_VRMON80 | BD718XX_LDO6_VRMON80,
584 				 BD718XX_LDO5_VRMON80 | BD718XX_LDO6_VRMON80);
585 	if (err) {
586 		dev_err(&pmic->pdev->dev,
587 			"Failed to disable voltage monitoring\n");
588 		goto err;
589 	}
590 
591 	for (i = 0; i < ARRAY_SIZE(pmic_regulator_inits); i++) {
592 
593 		struct regulator_desc *desc;
594 		struct regulator_dev *rdev;
595 
596 		desc = &pmic->descs[i];
597 
598 		config.dev = pdev->dev.parent;
599 		config.driver_data = pmic;
600 		config.regmap = pmic->mfd->regmap;
601 
602 		rdev = devm_regulator_register(&pdev->dev, desc, &config);
603 		if (IS_ERR(rdev)) {
604 			dev_err(pmic->mfd->dev,
605 				"failed to register %s regulator\n",
606 				desc->name);
607 			err = PTR_ERR(rdev);
608 			goto err;
609 		}
610 		/* Regulator register gets the regulator constraints and
611 		 * applies them (set_machine_constraints). This should have
612 		 * turned the control register(s) to correct values and we
613 		 * can now switch the control from PMIC state machine to the
614 		 * register interface
615 		 */
616 		err = regmap_update_bits(pmic->mfd->regmap,
617 					 pmic_regulator_inits[i].reg,
618 					 pmic_regulator_inits[i].mask,
619 					 0xFFFFFFFF);
620 		if (err) {
621 			dev_err(&pmic->pdev->dev,
622 				"Failed to write BUCK/LDO SEL bit for (%s)\n",
623 				desc->name);
624 			goto err;
625 		}
626 
627 		pmic->rdev[i] = rdev;
628 	}
629 
630 err:
631 	return err;
632 }
633 
634 static struct platform_driver bd71837_regulator = {
635 	.driver = {
636 		.name = "bd71837-pmic",
637 	},
638 	.probe = bd71837_probe,
639 };
640 
641 module_platform_driver(bd71837_regulator);
642 
643 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
644 MODULE_DESCRIPTION("BD71837 voltage regulator driver");
645 MODULE_LICENSE("GPL");
646