xref: /openbmc/linux/drivers/regulator/s2mps11.c (revision 65d80db2)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
4 //              http://www.samsung.com
5 
6 #include <linux/bug.h>
7 #include <linux/err.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/regmap.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/mfd/samsung/core.h>
18 #include <linux/mfd/samsung/s2mps11.h>
19 #include <linux/mfd/samsung/s2mps13.h>
20 #include <linux/mfd/samsung/s2mps14.h>
21 #include <linux/mfd/samsung/s2mps15.h>
22 #include <linux/mfd/samsung/s2mpu02.h>
23 
24 /* The highest number of possible regulators for supported devices. */
25 #define S2MPS_REGULATOR_MAX		S2MPS13_REGULATOR_MAX
26 struct s2mps11_info {
27 	int ramp_delay2;
28 	int ramp_delay34;
29 	int ramp_delay5;
30 	int ramp_delay16;
31 	int ramp_delay7810;
32 	int ramp_delay9;
33 
34 	enum sec_device_type dev_type;
35 
36 	/*
37 	 * One bit for each S2MPS11/S2MPS13/S2MPS14/S2MPU02 regulator whether
38 	 * the suspend mode was enabled.
39 	 */
40 	DECLARE_BITMAP(suspend_state, S2MPS_REGULATOR_MAX);
41 
42 	/*
43 	 * Array (size: number of regulators) with GPIO-s for external
44 	 * sleep control.
45 	 */
46 	struct gpio_desc **ext_control_gpiod;
47 };
48 
49 static int get_ramp_delay(int ramp_delay)
50 {
51 	unsigned char cnt = 0;
52 
53 	ramp_delay /= 6250;
54 
55 	while (true) {
56 		ramp_delay = ramp_delay >> 1;
57 		if (ramp_delay == 0)
58 			break;
59 		cnt++;
60 	}
61 
62 	if (cnt > 3)
63 		cnt = 3;
64 
65 	return cnt;
66 }
67 
68 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
69 				   unsigned int old_selector,
70 				   unsigned int new_selector)
71 {
72 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
73 	int rdev_id = rdev_get_id(rdev);
74 	unsigned int ramp_delay = 0;
75 	int old_volt, new_volt;
76 
77 	switch (rdev_id) {
78 	case S2MPS11_BUCK2:
79 		ramp_delay = s2mps11->ramp_delay2;
80 		break;
81 	case S2MPS11_BUCK3:
82 	case S2MPS11_BUCK4:
83 		ramp_delay = s2mps11->ramp_delay34;
84 		break;
85 	case S2MPS11_BUCK5:
86 		ramp_delay = s2mps11->ramp_delay5;
87 		break;
88 	case S2MPS11_BUCK6:
89 	case S2MPS11_BUCK1:
90 		ramp_delay = s2mps11->ramp_delay16;
91 		break;
92 	case S2MPS11_BUCK7:
93 	case S2MPS11_BUCK8:
94 	case S2MPS11_BUCK10:
95 		ramp_delay = s2mps11->ramp_delay7810;
96 		break;
97 	case S2MPS11_BUCK9:
98 		ramp_delay = s2mps11->ramp_delay9;
99 	}
100 
101 	if (ramp_delay == 0)
102 		ramp_delay = rdev->desc->ramp_delay;
103 
104 	old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
105 	new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
106 
107 	return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
108 }
109 
110 static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
111 {
112 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
113 	unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
114 	unsigned int ramp_enable = 1, enable_shift = 0;
115 	int rdev_id = rdev_get_id(rdev);
116 	int ret;
117 
118 	switch (rdev_id) {
119 	case S2MPS11_BUCK1:
120 		if (ramp_delay > s2mps11->ramp_delay16)
121 			s2mps11->ramp_delay16 = ramp_delay;
122 		else
123 			ramp_delay = s2mps11->ramp_delay16;
124 
125 		ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
126 		break;
127 	case S2MPS11_BUCK2:
128 		enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
129 		if (!ramp_delay) {
130 			ramp_enable = 0;
131 			break;
132 		}
133 
134 		s2mps11->ramp_delay2 = ramp_delay;
135 		ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
136 		ramp_reg = S2MPS11_REG_RAMP;
137 		break;
138 	case S2MPS11_BUCK3:
139 		enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
140 		if (!ramp_delay) {
141 			ramp_enable = 0;
142 			break;
143 		}
144 
145 		if (ramp_delay > s2mps11->ramp_delay34)
146 			s2mps11->ramp_delay34 = ramp_delay;
147 		else
148 			ramp_delay = s2mps11->ramp_delay34;
149 
150 		ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
151 		ramp_reg = S2MPS11_REG_RAMP;
152 		break;
153 	case S2MPS11_BUCK4:
154 		enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
155 		if (!ramp_delay) {
156 			ramp_enable = 0;
157 			break;
158 		}
159 
160 		if (ramp_delay > s2mps11->ramp_delay34)
161 			s2mps11->ramp_delay34 = ramp_delay;
162 		else
163 			ramp_delay = s2mps11->ramp_delay34;
164 
165 		ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
166 		ramp_reg = S2MPS11_REG_RAMP;
167 		break;
168 	case S2MPS11_BUCK5:
169 		s2mps11->ramp_delay5 = ramp_delay;
170 		ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
171 		break;
172 	case S2MPS11_BUCK6:
173 		enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
174 		if (!ramp_delay) {
175 			ramp_enable = 0;
176 			break;
177 		}
178 
179 		if (ramp_delay > s2mps11->ramp_delay16)
180 			s2mps11->ramp_delay16 = ramp_delay;
181 		else
182 			ramp_delay = s2mps11->ramp_delay16;
183 
184 		ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
185 		break;
186 	case S2MPS11_BUCK7:
187 	case S2MPS11_BUCK8:
188 	case S2MPS11_BUCK10:
189 		if (ramp_delay > s2mps11->ramp_delay7810)
190 			s2mps11->ramp_delay7810 = ramp_delay;
191 		else
192 			ramp_delay = s2mps11->ramp_delay7810;
193 
194 		ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
195 		break;
196 	case S2MPS11_BUCK9:
197 		s2mps11->ramp_delay9 = ramp_delay;
198 		ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
199 		break;
200 	default:
201 		return 0;
202 	}
203 
204 	if (!ramp_enable)
205 		goto ramp_disable;
206 
207 	/* Ramp delay can be enabled/disabled only for buck[2346] */
208 	if ((rdev_id >= S2MPS11_BUCK2 && rdev_id <= S2MPS11_BUCK4) ||
209 	    rdev_id == S2MPS11_BUCK6)  {
210 		ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
211 					 1 << enable_shift, 1 << enable_shift);
212 		if (ret) {
213 			dev_err(&rdev->dev, "failed to enable ramp rate\n");
214 			return ret;
215 		}
216 	}
217 
218 	ramp_val = get_ramp_delay(ramp_delay);
219 
220 	return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
221 				  ramp_val << ramp_shift);
222 
223 ramp_disable:
224 	return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
225 				  1 << enable_shift, 0);
226 }
227 
228 static int s2mps11_regulator_enable(struct regulator_dev *rdev)
229 {
230 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
231 	int rdev_id = rdev_get_id(rdev);
232 	unsigned int val;
233 
234 	switch (s2mps11->dev_type) {
235 	case S2MPS11X:
236 		if (test_bit(rdev_id, s2mps11->suspend_state))
237 			val = S2MPS14_ENABLE_SUSPEND;
238 		else
239 			val = rdev->desc->enable_mask;
240 		break;
241 	case S2MPS13X:
242 	case S2MPS14X:
243 		if (test_bit(rdev_id, s2mps11->suspend_state))
244 			val = S2MPS14_ENABLE_SUSPEND;
245 		else if (s2mps11->ext_control_gpiod[rdev_id])
246 			val = S2MPS14_ENABLE_EXT_CONTROL;
247 		else
248 			val = rdev->desc->enable_mask;
249 		break;
250 	case S2MPU02:
251 		if (test_bit(rdev_id, s2mps11->suspend_state))
252 			val = S2MPU02_ENABLE_SUSPEND;
253 		else
254 			val = rdev->desc->enable_mask;
255 		break;
256 	default:
257 		return -EINVAL;
258 	}
259 
260 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
261 			rdev->desc->enable_mask, val);
262 }
263 
264 static int s2mps11_regulator_set_suspend_disable(struct regulator_dev *rdev)
265 {
266 	int ret;
267 	unsigned int val, state;
268 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
269 	int rdev_id = rdev_get_id(rdev);
270 
271 	/* Below LDO should be always on or does not support suspend mode. */
272 	switch (s2mps11->dev_type) {
273 	case S2MPS11X:
274 		switch (rdev_id) {
275 		case S2MPS11_LDO2:
276 		case S2MPS11_LDO36:
277 		case S2MPS11_LDO37:
278 		case S2MPS11_LDO38:
279 			return 0;
280 		default:
281 			state = S2MPS14_ENABLE_SUSPEND;
282 			break;
283 		}
284 		break;
285 	case S2MPS13X:
286 	case S2MPS14X:
287 		switch (rdev_id) {
288 		case S2MPS14_LDO3:
289 			return 0;
290 		default:
291 			state = S2MPS14_ENABLE_SUSPEND;
292 			break;
293 		}
294 		break;
295 	case S2MPU02:
296 		switch (rdev_id) {
297 		case S2MPU02_LDO13:
298 		case S2MPU02_LDO14:
299 		case S2MPU02_LDO15:
300 		case S2MPU02_LDO17:
301 		case S2MPU02_BUCK7:
302 			state = S2MPU02_DISABLE_SUSPEND;
303 			break;
304 		default:
305 			state = S2MPU02_ENABLE_SUSPEND;
306 			break;
307 		}
308 		break;
309 	default:
310 		return -EINVAL;
311 	}
312 
313 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
314 	if (ret < 0)
315 		return ret;
316 
317 	set_bit(rdev_id, s2mps11->suspend_state);
318 	/*
319 	 * Don't enable suspend mode if regulator is already disabled because
320 	 * this would effectively for a short time turn on the regulator after
321 	 * resuming.
322 	 * However we still want to toggle the suspend_state bit for regulator
323 	 * in case if it got enabled before suspending the system.
324 	 */
325 	if (!(val & rdev->desc->enable_mask))
326 		return 0;
327 
328 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
329 				  rdev->desc->enable_mask, state);
330 }
331 
332 static const struct regulator_ops s2mps11_ldo_ops = {
333 	.list_voltage		= regulator_list_voltage_linear,
334 	.map_voltage		= regulator_map_voltage_linear,
335 	.is_enabled		= regulator_is_enabled_regmap,
336 	.enable			= s2mps11_regulator_enable,
337 	.disable		= regulator_disable_regmap,
338 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
339 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
340 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
341 	.set_suspend_disable	= s2mps11_regulator_set_suspend_disable,
342 };
343 
344 static const struct regulator_ops s2mps11_buck_ops = {
345 	.list_voltage		= regulator_list_voltage_linear,
346 	.map_voltage		= regulator_map_voltage_linear,
347 	.is_enabled		= regulator_is_enabled_regmap,
348 	.enable			= s2mps11_regulator_enable,
349 	.disable		= regulator_disable_regmap,
350 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
351 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
352 	.set_voltage_time_sel	= s2mps11_regulator_set_voltage_time_sel,
353 	.set_ramp_delay		= s2mps11_set_ramp_delay,
354 	.set_suspend_disable	= s2mps11_regulator_set_suspend_disable,
355 };
356 
357 #define regulator_desc_s2mps11_ldo(num, step) {		\
358 	.name		= "LDO"#num,			\
359 	.id		= S2MPS11_LDO##num,		\
360 	.ops		= &s2mps11_ldo_ops,		\
361 	.type		= REGULATOR_VOLTAGE,		\
362 	.owner		= THIS_MODULE,			\
363 	.ramp_delay	= RAMP_DELAY_12_MVUS,		\
364 	.min_uV		= MIN_800_MV,			\
365 	.uV_step	= step,				\
366 	.n_voltages	= S2MPS11_LDO_N_VOLTAGES,	\
367 	.vsel_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
368 	.vsel_mask	= S2MPS11_LDO_VSEL_MASK,	\
369 	.enable_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
370 	.enable_mask	= S2MPS11_ENABLE_MASK		\
371 }
372 
373 #define regulator_desc_s2mps11_buck1_4(num) {			\
374 	.name		= "BUCK"#num,				\
375 	.id		= S2MPS11_BUCK##num,			\
376 	.ops		= &s2mps11_buck_ops,			\
377 	.type		= REGULATOR_VOLTAGE,			\
378 	.owner		= THIS_MODULE,				\
379 	.min_uV		= MIN_600_MV,				\
380 	.uV_step	= STEP_6_25_MV,				\
381 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
382 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
383 	.vsel_reg	= S2MPS11_REG_B1CTRL2 + (num - 1) * 2,	\
384 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
385 	.enable_reg	= S2MPS11_REG_B1CTRL1 + (num - 1) * 2,	\
386 	.enable_mask	= S2MPS11_ENABLE_MASK			\
387 }
388 
389 #define regulator_desc_s2mps11_buck5 {				\
390 	.name		= "BUCK5",				\
391 	.id		= S2MPS11_BUCK5,			\
392 	.ops		= &s2mps11_buck_ops,			\
393 	.type		= REGULATOR_VOLTAGE,			\
394 	.owner		= THIS_MODULE,				\
395 	.min_uV		= MIN_600_MV,				\
396 	.uV_step	= STEP_6_25_MV,				\
397 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
398 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
399 	.vsel_reg	= S2MPS11_REG_B5CTRL2,			\
400 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
401 	.enable_reg	= S2MPS11_REG_B5CTRL1,			\
402 	.enable_mask	= S2MPS11_ENABLE_MASK			\
403 }
404 
405 #define regulator_desc_s2mps11_buck67810(num, min, step) {	\
406 	.name		= "BUCK"#num,				\
407 	.id		= S2MPS11_BUCK##num,			\
408 	.ops		= &s2mps11_buck_ops,			\
409 	.type		= REGULATOR_VOLTAGE,			\
410 	.owner		= THIS_MODULE,				\
411 	.min_uV		= min,					\
412 	.uV_step	= step,					\
413 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
414 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
415 	.vsel_reg	= S2MPS11_REG_B6CTRL2 + (num - 6) * 2,	\
416 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
417 	.enable_reg	= S2MPS11_REG_B6CTRL1 + (num - 6) * 2,	\
418 	.enable_mask	= S2MPS11_ENABLE_MASK			\
419 }
420 
421 #define regulator_desc_s2mps11_buck9 {				\
422 	.name		= "BUCK9",				\
423 	.id		= S2MPS11_BUCK9,			\
424 	.ops		= &s2mps11_buck_ops,			\
425 	.type		= REGULATOR_VOLTAGE,			\
426 	.owner		= THIS_MODULE,				\
427 	.min_uV		= MIN_3000_MV,				\
428 	.uV_step	= STEP_25_MV,				\
429 	.n_voltages	= S2MPS11_BUCK9_N_VOLTAGES,		\
430 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
431 	.vsel_reg	= S2MPS11_REG_B9CTRL2,			\
432 	.vsel_mask	= S2MPS11_BUCK9_VSEL_MASK,		\
433 	.enable_reg	= S2MPS11_REG_B9CTRL1,			\
434 	.enable_mask	= S2MPS11_ENABLE_MASK			\
435 }
436 
437 static const struct regulator_desc s2mps11_regulators[] = {
438 	regulator_desc_s2mps11_ldo(1, STEP_25_MV),
439 	regulator_desc_s2mps11_ldo(2, STEP_50_MV),
440 	regulator_desc_s2mps11_ldo(3, STEP_50_MV),
441 	regulator_desc_s2mps11_ldo(4, STEP_50_MV),
442 	regulator_desc_s2mps11_ldo(5, STEP_50_MV),
443 	regulator_desc_s2mps11_ldo(6, STEP_25_MV),
444 	regulator_desc_s2mps11_ldo(7, STEP_50_MV),
445 	regulator_desc_s2mps11_ldo(8, STEP_50_MV),
446 	regulator_desc_s2mps11_ldo(9, STEP_50_MV),
447 	regulator_desc_s2mps11_ldo(10, STEP_50_MV),
448 	regulator_desc_s2mps11_ldo(11, STEP_25_MV),
449 	regulator_desc_s2mps11_ldo(12, STEP_50_MV),
450 	regulator_desc_s2mps11_ldo(13, STEP_50_MV),
451 	regulator_desc_s2mps11_ldo(14, STEP_50_MV),
452 	regulator_desc_s2mps11_ldo(15, STEP_50_MV),
453 	regulator_desc_s2mps11_ldo(16, STEP_50_MV),
454 	regulator_desc_s2mps11_ldo(17, STEP_50_MV),
455 	regulator_desc_s2mps11_ldo(18, STEP_50_MV),
456 	regulator_desc_s2mps11_ldo(19, STEP_50_MV),
457 	regulator_desc_s2mps11_ldo(20, STEP_50_MV),
458 	regulator_desc_s2mps11_ldo(21, STEP_50_MV),
459 	regulator_desc_s2mps11_ldo(22, STEP_25_MV),
460 	regulator_desc_s2mps11_ldo(23, STEP_25_MV),
461 	regulator_desc_s2mps11_ldo(24, STEP_50_MV),
462 	regulator_desc_s2mps11_ldo(25, STEP_50_MV),
463 	regulator_desc_s2mps11_ldo(26, STEP_50_MV),
464 	regulator_desc_s2mps11_ldo(27, STEP_25_MV),
465 	regulator_desc_s2mps11_ldo(28, STEP_50_MV),
466 	regulator_desc_s2mps11_ldo(29, STEP_50_MV),
467 	regulator_desc_s2mps11_ldo(30, STEP_50_MV),
468 	regulator_desc_s2mps11_ldo(31, STEP_50_MV),
469 	regulator_desc_s2mps11_ldo(32, STEP_50_MV),
470 	regulator_desc_s2mps11_ldo(33, STEP_50_MV),
471 	regulator_desc_s2mps11_ldo(34, STEP_50_MV),
472 	regulator_desc_s2mps11_ldo(35, STEP_25_MV),
473 	regulator_desc_s2mps11_ldo(36, STEP_50_MV),
474 	regulator_desc_s2mps11_ldo(37, STEP_50_MV),
475 	regulator_desc_s2mps11_ldo(38, STEP_50_MV),
476 	regulator_desc_s2mps11_buck1_4(1),
477 	regulator_desc_s2mps11_buck1_4(2),
478 	regulator_desc_s2mps11_buck1_4(3),
479 	regulator_desc_s2mps11_buck1_4(4),
480 	regulator_desc_s2mps11_buck5,
481 	regulator_desc_s2mps11_buck67810(6, MIN_600_MV, STEP_6_25_MV),
482 	regulator_desc_s2mps11_buck67810(7, MIN_600_MV, STEP_12_5_MV),
483 	regulator_desc_s2mps11_buck67810(8, MIN_600_MV, STEP_12_5_MV),
484 	regulator_desc_s2mps11_buck9,
485 	regulator_desc_s2mps11_buck67810(10, MIN_750_MV, STEP_12_5_MV),
486 };
487 
488 static const struct regulator_ops s2mps14_reg_ops;
489 
490 #define regulator_desc_s2mps13_ldo(num, min, step, min_sel) {	\
491 	.name		= "LDO"#num,				\
492 	.id		= S2MPS13_LDO##num,			\
493 	.ops		= &s2mps14_reg_ops,			\
494 	.type		= REGULATOR_VOLTAGE,			\
495 	.owner		= THIS_MODULE,				\
496 	.min_uV		= min,					\
497 	.uV_step	= step,					\
498 	.linear_min_sel	= min_sel,				\
499 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,		\
500 	.vsel_reg	= S2MPS13_REG_L1CTRL + num - 1,		\
501 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,		\
502 	.enable_reg	= S2MPS13_REG_L1CTRL + num - 1,		\
503 	.enable_mask	= S2MPS14_ENABLE_MASK			\
504 }
505 
506 #define regulator_desc_s2mps13_buck(num, min, step, min_sel) {	\
507 	.name		= "BUCK"#num,				\
508 	.id		= S2MPS13_BUCK##num,			\
509 	.ops		= &s2mps14_reg_ops,			\
510 	.type		= REGULATOR_VOLTAGE,			\
511 	.owner		= THIS_MODULE,				\
512 	.min_uV		= min,					\
513 	.uV_step	= step,					\
514 	.linear_min_sel	= min_sel,				\
515 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
516 	.ramp_delay	= S2MPS13_BUCK_RAMP_DELAY,		\
517 	.vsel_reg	= S2MPS13_REG_B1OUT + (num - 1) * 2,	\
518 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
519 	.enable_reg	= S2MPS13_REG_B1CTRL + (num - 1) * 2,	\
520 	.enable_mask	= S2MPS14_ENABLE_MASK			\
521 }
522 
523 #define regulator_desc_s2mps13_buck7(num, min, step, min_sel) {	\
524 	.name		= "BUCK"#num,				\
525 	.id		= S2MPS13_BUCK##num,			\
526 	.ops		= &s2mps14_reg_ops,			\
527 	.type		= REGULATOR_VOLTAGE,			\
528 	.owner		= THIS_MODULE,				\
529 	.min_uV		= min,					\
530 	.uV_step	= step,					\
531 	.linear_min_sel	= min_sel,				\
532 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
533 	.ramp_delay	= S2MPS13_BUCK_RAMP_DELAY,		\
534 	.vsel_reg	= S2MPS13_REG_B1OUT + (num) * 2 - 1,	\
535 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
536 	.enable_reg	= S2MPS13_REG_B1CTRL + (num - 1) * 2,	\
537 	.enable_mask	= S2MPS14_ENABLE_MASK			\
538 }
539 
540 #define regulator_desc_s2mps13_buck8_10(num, min, step, min_sel) {	\
541 	.name		= "BUCK"#num,				\
542 	.id		= S2MPS13_BUCK##num,			\
543 	.ops		= &s2mps14_reg_ops,			\
544 	.type		= REGULATOR_VOLTAGE,			\
545 	.owner		= THIS_MODULE,				\
546 	.min_uV		= min,					\
547 	.uV_step	= step,					\
548 	.linear_min_sel	= min_sel,				\
549 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
550 	.ramp_delay	= S2MPS13_BUCK_RAMP_DELAY,		\
551 	.vsel_reg	= S2MPS13_REG_B1OUT + (num) * 2 - 1,	\
552 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
553 	.enable_reg	= S2MPS13_REG_B1CTRL + (num) * 2 - 1,	\
554 	.enable_mask	= S2MPS14_ENABLE_MASK			\
555 }
556 
557 static const struct regulator_desc s2mps13_regulators[] = {
558 	regulator_desc_s2mps13_ldo(1,  MIN_800_MV,  STEP_12_5_MV, 0x00),
559 	regulator_desc_s2mps13_ldo(2,  MIN_1400_MV, STEP_50_MV,   0x0C),
560 	regulator_desc_s2mps13_ldo(3,  MIN_1000_MV, STEP_25_MV,   0x08),
561 	regulator_desc_s2mps13_ldo(4,  MIN_800_MV,  STEP_12_5_MV, 0x00),
562 	regulator_desc_s2mps13_ldo(5,  MIN_800_MV,  STEP_12_5_MV, 0x00),
563 	regulator_desc_s2mps13_ldo(6,  MIN_800_MV,  STEP_12_5_MV, 0x00),
564 	regulator_desc_s2mps13_ldo(7,  MIN_1000_MV, STEP_25_MV,   0x08),
565 	regulator_desc_s2mps13_ldo(8,  MIN_1000_MV, STEP_25_MV,   0x08),
566 	regulator_desc_s2mps13_ldo(9,  MIN_1000_MV, STEP_25_MV,   0x08),
567 	regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV,   0x0C),
568 	regulator_desc_s2mps13_ldo(11, MIN_800_MV,  STEP_25_MV,   0x10),
569 	regulator_desc_s2mps13_ldo(12, MIN_800_MV,  STEP_25_MV,   0x10),
570 	regulator_desc_s2mps13_ldo(13, MIN_800_MV,  STEP_25_MV,   0x10),
571 	regulator_desc_s2mps13_ldo(14, MIN_800_MV,  STEP_12_5_MV, 0x00),
572 	regulator_desc_s2mps13_ldo(15, MIN_800_MV,  STEP_12_5_MV, 0x00),
573 	regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV,   0x0C),
574 	regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV,   0x0C),
575 	regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV,   0x08),
576 	regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV,   0x08),
577 	regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV,   0x0C),
578 	regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV,   0x08),
579 	regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV,   0x08),
580 	regulator_desc_s2mps13_ldo(23, MIN_800_MV,  STEP_12_5_MV, 0x00),
581 	regulator_desc_s2mps13_ldo(24, MIN_800_MV,  STEP_12_5_MV, 0x00),
582 	regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV,   0x0C),
583 	regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV,   0x0C),
584 	regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV,   0x0C),
585 	regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV,   0x08),
586 	regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV,   0x0C),
587 	regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV,   0x0C),
588 	regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV,   0x08),
589 	regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV,   0x08),
590 	regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV,   0x0C),
591 	regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV,   0x08),
592 	regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV,   0x0C),
593 	regulator_desc_s2mps13_ldo(36, MIN_800_MV,  STEP_12_5_MV, 0x00),
594 	regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV,   0x08),
595 	regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV,   0x0C),
596 	regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV,   0x08),
597 	regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV,   0x0C),
598 	regulator_desc_s2mps13_buck(1,  MIN_500_MV,  STEP_6_25_MV, 0x10),
599 	regulator_desc_s2mps13_buck(2,  MIN_500_MV,  STEP_6_25_MV, 0x10),
600 	regulator_desc_s2mps13_buck(3,  MIN_500_MV,  STEP_6_25_MV, 0x10),
601 	regulator_desc_s2mps13_buck(4,  MIN_500_MV,  STEP_6_25_MV, 0x10),
602 	regulator_desc_s2mps13_buck(5,  MIN_500_MV,  STEP_6_25_MV, 0x10),
603 	regulator_desc_s2mps13_buck(6,  MIN_500_MV,  STEP_6_25_MV, 0x10),
604 	regulator_desc_s2mps13_buck7(7,  MIN_500_MV,  STEP_6_25_MV, 0x10),
605 	regulator_desc_s2mps13_buck8_10(8,  MIN_1000_MV, STEP_12_5_MV, 0x20),
606 	regulator_desc_s2mps13_buck8_10(9,  MIN_1000_MV, STEP_12_5_MV, 0x20),
607 	regulator_desc_s2mps13_buck8_10(10, MIN_500_MV,  STEP_6_25_MV, 0x10),
608 };
609 
610 static const struct regulator_ops s2mps14_reg_ops = {
611 	.list_voltage		= regulator_list_voltage_linear,
612 	.map_voltage		= regulator_map_voltage_linear,
613 	.is_enabled		= regulator_is_enabled_regmap,
614 	.enable			= s2mps11_regulator_enable,
615 	.disable		= regulator_disable_regmap,
616 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
617 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
618 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
619 	.set_suspend_disable	= s2mps11_regulator_set_suspend_disable,
620 };
621 
622 #define regulator_desc_s2mps14_ldo(num, min, step) {	\
623 	.name		= "LDO"#num,			\
624 	.id		= S2MPS14_LDO##num,		\
625 	.ops		= &s2mps14_reg_ops,		\
626 	.type		= REGULATOR_VOLTAGE,		\
627 	.owner		= THIS_MODULE,			\
628 	.min_uV		= min,				\
629 	.uV_step	= step,				\
630 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,	\
631 	.vsel_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
632 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,	\
633 	.enable_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
634 	.enable_mask	= S2MPS14_ENABLE_MASK		\
635 }
636 
637 #define regulator_desc_s2mps14_buck(num, min, step, min_sel) {	\
638 	.name		= "BUCK"#num,				\
639 	.id		= S2MPS14_BUCK##num,			\
640 	.ops		= &s2mps14_reg_ops,			\
641 	.type		= REGULATOR_VOLTAGE,			\
642 	.owner		= THIS_MODULE,				\
643 	.min_uV		= min,					\
644 	.uV_step	= step,					\
645 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
646 	.linear_min_sel = min_sel,				\
647 	.ramp_delay	= S2MPS14_BUCK_RAMP_DELAY,		\
648 	.vsel_reg	= S2MPS14_REG_B1CTRL2 + (num - 1) * 2,	\
649 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
650 	.enable_reg	= S2MPS14_REG_B1CTRL1 + (num - 1) * 2,	\
651 	.enable_mask	= S2MPS14_ENABLE_MASK			\
652 }
653 
654 static const struct regulator_desc s2mps14_regulators[] = {
655 	regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
656 	regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
657 	regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
658 	regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
659 	regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
660 	regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
661 	regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
662 	regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
663 	regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
664 	regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
665 	regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
666 	regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
667 	regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
668 	regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
669 	regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
670 	regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
671 	regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
672 	regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
673 	regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
674 	regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
675 	regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
676 	regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
677 	regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
678 	regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
679 	regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
680 	regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV,
681 				    S2MPS14_BUCK1235_START_SEL),
682 	regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV,
683 				    S2MPS14_BUCK1235_START_SEL),
684 	regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV,
685 				    S2MPS14_BUCK1235_START_SEL),
686 	regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV,
687 				    S2MPS14_BUCK4_START_SEL),
688 	regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV,
689 				    S2MPS14_BUCK1235_START_SEL),
690 };
691 
692 static const struct regulator_ops s2mps15_reg_ldo_ops = {
693 	.list_voltage		= regulator_list_voltage_linear_range,
694 	.map_voltage		= regulator_map_voltage_linear_range,
695 	.is_enabled		= regulator_is_enabled_regmap,
696 	.enable			= regulator_enable_regmap,
697 	.disable		= regulator_disable_regmap,
698 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
699 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
700 };
701 
702 static const struct regulator_ops s2mps15_reg_buck_ops = {
703 	.list_voltage		= regulator_list_voltage_linear_range,
704 	.map_voltage		= regulator_map_voltage_linear_range,
705 	.is_enabled		= regulator_is_enabled_regmap,
706 	.enable			= regulator_enable_regmap,
707 	.disable		= regulator_disable_regmap,
708 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
709 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
710 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
711 };
712 
713 #define regulator_desc_s2mps15_ldo(num, range) {	\
714 	.name		= "LDO"#num,			\
715 	.id		= S2MPS15_LDO##num,		\
716 	.ops		= &s2mps15_reg_ldo_ops,		\
717 	.type		= REGULATOR_VOLTAGE,		\
718 	.owner		= THIS_MODULE,			\
719 	.linear_ranges	= range,			\
720 	.n_linear_ranges = ARRAY_SIZE(range),		\
721 	.n_voltages	= S2MPS15_LDO_N_VOLTAGES,	\
722 	.vsel_reg	= S2MPS15_REG_L1CTRL + num - 1,	\
723 	.vsel_mask	= S2MPS15_LDO_VSEL_MASK,	\
724 	.enable_reg	= S2MPS15_REG_L1CTRL + num - 1,	\
725 	.enable_mask	= S2MPS15_ENABLE_MASK		\
726 }
727 
728 #define regulator_desc_s2mps15_buck(num, range) {			\
729 	.name		= "BUCK"#num,					\
730 	.id		= S2MPS15_BUCK##num,				\
731 	.ops		= &s2mps15_reg_buck_ops,			\
732 	.type		= REGULATOR_VOLTAGE,				\
733 	.owner		= THIS_MODULE,					\
734 	.linear_ranges	= range,					\
735 	.n_linear_ranges = ARRAY_SIZE(range),				\
736 	.ramp_delay	= 12500,					\
737 	.n_voltages	= S2MPS15_BUCK_N_VOLTAGES,			\
738 	.vsel_reg	= S2MPS15_REG_B1CTRL2 + ((num - 1) * 2),	\
739 	.vsel_mask	= S2MPS15_BUCK_VSEL_MASK,			\
740 	.enable_reg	= S2MPS15_REG_B1CTRL1 + ((num - 1) * 2),	\
741 	.enable_mask	= S2MPS15_ENABLE_MASK				\
742 }
743 
744 /* voltage range for s2mps15 LDO 3, 5, 15, 16, 18, 20, 23 and 27 */
745 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges1[] = {
746 	REGULATOR_LINEAR_RANGE(1000000, 0xc, 0x38, 25000),
747 };
748 
749 /* voltage range for s2mps15 LDO 2, 6, 14, 17, 19, 21, 24 and 25 */
750 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges2[] = {
751 	REGULATOR_LINEAR_RANGE(1800000, 0x0, 0x3f, 25000),
752 };
753 
754 /* voltage range for s2mps15 LDO 4, 11, 12, 13, 22 and 26 */
755 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges3[] = {
756 	REGULATOR_LINEAR_RANGE(700000, 0x0, 0x34, 12500),
757 };
758 
759 /* voltage range for s2mps15 LDO 7, 8, 9 and 10 */
760 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges4[] = {
761 	REGULATOR_LINEAR_RANGE(700000, 0x10, 0x20, 25000),
762 };
763 
764 /* voltage range for s2mps15 LDO 1 */
765 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges5[] = {
766 	REGULATOR_LINEAR_RANGE(500000, 0x0, 0x20, 12500),
767 };
768 
769 /* voltage range for s2mps15 BUCK 1, 2, 3, 4, 5, 6 and 7 */
770 static const struct regulator_linear_range s2mps15_buck_voltage_ranges1[] = {
771 	REGULATOR_LINEAR_RANGE(500000, 0x20, 0xc0, 6250),
772 };
773 
774 /* voltage range for s2mps15 BUCK 8, 9 and 10 */
775 static const struct regulator_linear_range s2mps15_buck_voltage_ranges2[] = {
776 	REGULATOR_LINEAR_RANGE(1000000, 0x20, 0x78, 12500),
777 };
778 
779 static const struct regulator_desc s2mps15_regulators[] = {
780 	regulator_desc_s2mps15_ldo(1, s2mps15_ldo_voltage_ranges5),
781 	regulator_desc_s2mps15_ldo(2, s2mps15_ldo_voltage_ranges2),
782 	regulator_desc_s2mps15_ldo(3, s2mps15_ldo_voltage_ranges1),
783 	regulator_desc_s2mps15_ldo(4, s2mps15_ldo_voltage_ranges3),
784 	regulator_desc_s2mps15_ldo(5, s2mps15_ldo_voltage_ranges1),
785 	regulator_desc_s2mps15_ldo(6, s2mps15_ldo_voltage_ranges2),
786 	regulator_desc_s2mps15_ldo(7, s2mps15_ldo_voltage_ranges4),
787 	regulator_desc_s2mps15_ldo(8, s2mps15_ldo_voltage_ranges4),
788 	regulator_desc_s2mps15_ldo(9, s2mps15_ldo_voltage_ranges4),
789 	regulator_desc_s2mps15_ldo(10, s2mps15_ldo_voltage_ranges4),
790 	regulator_desc_s2mps15_ldo(11, s2mps15_ldo_voltage_ranges3),
791 	regulator_desc_s2mps15_ldo(12, s2mps15_ldo_voltage_ranges3),
792 	regulator_desc_s2mps15_ldo(13, s2mps15_ldo_voltage_ranges3),
793 	regulator_desc_s2mps15_ldo(14, s2mps15_ldo_voltage_ranges2),
794 	regulator_desc_s2mps15_ldo(15, s2mps15_ldo_voltage_ranges1),
795 	regulator_desc_s2mps15_ldo(16, s2mps15_ldo_voltage_ranges1),
796 	regulator_desc_s2mps15_ldo(17, s2mps15_ldo_voltage_ranges2),
797 	regulator_desc_s2mps15_ldo(18, s2mps15_ldo_voltage_ranges1),
798 	regulator_desc_s2mps15_ldo(19, s2mps15_ldo_voltage_ranges2),
799 	regulator_desc_s2mps15_ldo(20, s2mps15_ldo_voltage_ranges1),
800 	regulator_desc_s2mps15_ldo(21, s2mps15_ldo_voltage_ranges2),
801 	regulator_desc_s2mps15_ldo(22, s2mps15_ldo_voltage_ranges3),
802 	regulator_desc_s2mps15_ldo(23, s2mps15_ldo_voltage_ranges1),
803 	regulator_desc_s2mps15_ldo(24, s2mps15_ldo_voltage_ranges2),
804 	regulator_desc_s2mps15_ldo(25, s2mps15_ldo_voltage_ranges2),
805 	regulator_desc_s2mps15_ldo(26, s2mps15_ldo_voltage_ranges3),
806 	regulator_desc_s2mps15_ldo(27, s2mps15_ldo_voltage_ranges1),
807 	regulator_desc_s2mps15_buck(1, s2mps15_buck_voltage_ranges1),
808 	regulator_desc_s2mps15_buck(2, s2mps15_buck_voltage_ranges1),
809 	regulator_desc_s2mps15_buck(3, s2mps15_buck_voltage_ranges1),
810 	regulator_desc_s2mps15_buck(4, s2mps15_buck_voltage_ranges1),
811 	regulator_desc_s2mps15_buck(5, s2mps15_buck_voltage_ranges1),
812 	regulator_desc_s2mps15_buck(6, s2mps15_buck_voltage_ranges1),
813 	regulator_desc_s2mps15_buck(7, s2mps15_buck_voltage_ranges1),
814 	regulator_desc_s2mps15_buck(8, s2mps15_buck_voltage_ranges2),
815 	regulator_desc_s2mps15_buck(9, s2mps15_buck_voltage_ranges2),
816 	regulator_desc_s2mps15_buck(10, s2mps15_buck_voltage_ranges2),
817 };
818 
819 static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
820 		struct regulator_dev *rdev)
821 {
822 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
823 			rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
824 }
825 
826 static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
827 		struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
828 {
829 	struct gpio_desc **gpio = s2mps11->ext_control_gpiod;
830 	unsigned int i;
831 	unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
832 		S2MPS14_LDO12 };
833 
834 	for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
835 		unsigned int reg = valid_regulators[i];
836 
837 		if (!rdata[reg].init_data || !rdata[reg].of_node)
838 			continue;
839 
840 		gpio[reg] = devm_gpiod_get_from_of_node(&pdev->dev,
841 				rdata[reg].of_node,
842 				"samsung,ext-control-gpios",
843 				0,
844 				GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
845 				"s2mps11-regulator");
846 		if (IS_ERR(gpio[reg])) {
847 			dev_err(&pdev->dev, "Failed to get control GPIO for %d/%s\n",
848 				reg, rdata[reg].name);
849 			continue;
850 		}
851 		if (gpio[reg])
852 			dev_dbg(&pdev->dev, "Using GPIO for ext-control over %d/%s\n",
853 				reg, rdata[reg].name);
854 	}
855 }
856 
857 static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
858 		struct of_regulator_match *rdata, struct s2mps11_info *s2mps11,
859 		unsigned int rdev_num)
860 {
861 	struct device_node *reg_np;
862 
863 	reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
864 	if (!reg_np) {
865 		dev_err(&pdev->dev, "could not find regulators sub-node\n");
866 		return -EINVAL;
867 	}
868 
869 	of_regulator_match(&pdev->dev, reg_np, rdata, rdev_num);
870 	if (s2mps11->dev_type == S2MPS14X)
871 		s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
872 
873 	of_node_put(reg_np);
874 
875 	return 0;
876 }
877 
878 static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
879 {
880 	unsigned int ramp_val, ramp_shift, ramp_reg;
881 	int rdev_id = rdev_get_id(rdev);
882 
883 	switch (rdev_id) {
884 	case S2MPU02_BUCK1:
885 		ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
886 		break;
887 	case S2MPU02_BUCK2:
888 		ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
889 		break;
890 	case S2MPU02_BUCK3:
891 		ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
892 		break;
893 	case S2MPU02_BUCK4:
894 		ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
895 		break;
896 	default:
897 		return 0;
898 	}
899 	ramp_reg = S2MPU02_REG_RAMP1;
900 	ramp_val = get_ramp_delay(ramp_delay);
901 
902 	return regmap_update_bits(rdev->regmap, ramp_reg,
903 				  S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
904 				  ramp_val << ramp_shift);
905 }
906 
907 static const struct regulator_ops s2mpu02_ldo_ops = {
908 	.list_voltage		= regulator_list_voltage_linear,
909 	.map_voltage		= regulator_map_voltage_linear,
910 	.is_enabled		= regulator_is_enabled_regmap,
911 	.enable			= s2mps11_regulator_enable,
912 	.disable		= regulator_disable_regmap,
913 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
914 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
915 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
916 	.set_suspend_disable	= s2mps11_regulator_set_suspend_disable,
917 };
918 
919 static const struct regulator_ops s2mpu02_buck_ops = {
920 	.list_voltage		= regulator_list_voltage_linear,
921 	.map_voltage		= regulator_map_voltage_linear,
922 	.is_enabled		= regulator_is_enabled_regmap,
923 	.enable			= s2mps11_regulator_enable,
924 	.disable		= regulator_disable_regmap,
925 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
926 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
927 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
928 	.set_suspend_disable	= s2mps11_regulator_set_suspend_disable,
929 	.set_ramp_delay		= s2mpu02_set_ramp_delay,
930 };
931 
932 #define regulator_desc_s2mpu02_ldo1(num) {		\
933 	.name		= "LDO"#num,			\
934 	.id		= S2MPU02_LDO##num,		\
935 	.ops		= &s2mpu02_ldo_ops,		\
936 	.type		= REGULATOR_VOLTAGE,		\
937 	.owner		= THIS_MODULE,			\
938 	.min_uV		= S2MPU02_LDO_MIN_900MV,	\
939 	.uV_step	= S2MPU02_LDO_STEP_12_5MV,	\
940 	.linear_min_sel	= S2MPU02_LDO_GROUP1_START_SEL,	\
941 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
942 	.vsel_reg	= S2MPU02_REG_L1CTRL,		\
943 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
944 	.enable_reg	= S2MPU02_REG_L1CTRL,		\
945 	.enable_mask	= S2MPU02_ENABLE_MASK		\
946 }
947 #define regulator_desc_s2mpu02_ldo2(num) {		\
948 	.name		= "LDO"#num,			\
949 	.id		= S2MPU02_LDO##num,		\
950 	.ops		= &s2mpu02_ldo_ops,		\
951 	.type		= REGULATOR_VOLTAGE,		\
952 	.owner		= THIS_MODULE,			\
953 	.min_uV		= S2MPU02_LDO_MIN_1050MV,	\
954 	.uV_step	= S2MPU02_LDO_STEP_25MV,	\
955 	.linear_min_sel	= S2MPU02_LDO_GROUP2_START_SEL,	\
956 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
957 	.vsel_reg	= S2MPU02_REG_L2CTRL1,		\
958 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
959 	.enable_reg	= S2MPU02_REG_L2CTRL1,		\
960 	.enable_mask	= S2MPU02_ENABLE_MASK		\
961 }
962 #define regulator_desc_s2mpu02_ldo3(num) {		\
963 	.name		= "LDO"#num,			\
964 	.id		= S2MPU02_LDO##num,		\
965 	.ops		= &s2mpu02_ldo_ops,		\
966 	.type		= REGULATOR_VOLTAGE,		\
967 	.owner		= THIS_MODULE,			\
968 	.min_uV		= S2MPU02_LDO_MIN_900MV,	\
969 	.uV_step	= S2MPU02_LDO_STEP_12_5MV,	\
970 	.linear_min_sel	= S2MPU02_LDO_GROUP1_START_SEL,	\
971 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
972 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
973 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
974 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
975 	.enable_mask	= S2MPU02_ENABLE_MASK		\
976 }
977 #define regulator_desc_s2mpu02_ldo4(num) {		\
978 	.name		= "LDO"#num,			\
979 	.id		= S2MPU02_LDO##num,		\
980 	.ops		= &s2mpu02_ldo_ops,		\
981 	.type		= REGULATOR_VOLTAGE,		\
982 	.owner		= THIS_MODULE,			\
983 	.min_uV		= S2MPU02_LDO_MIN_1050MV,	\
984 	.uV_step	= S2MPU02_LDO_STEP_25MV,	\
985 	.linear_min_sel	= S2MPU02_LDO_GROUP2_START_SEL,	\
986 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
987 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
988 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
989 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
990 	.enable_mask	= S2MPU02_ENABLE_MASK		\
991 }
992 #define regulator_desc_s2mpu02_ldo5(num) {		\
993 	.name		= "LDO"#num,			\
994 	.id		= S2MPU02_LDO##num,		\
995 	.ops		= &s2mpu02_ldo_ops,		\
996 	.type		= REGULATOR_VOLTAGE,		\
997 	.owner		= THIS_MODULE,			\
998 	.min_uV		= S2MPU02_LDO_MIN_1600MV,	\
999 	.uV_step	= S2MPU02_LDO_STEP_50MV,	\
1000 	.linear_min_sel	= S2MPU02_LDO_GROUP3_START_SEL,	\
1001 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
1002 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
1003 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
1004 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
1005 	.enable_mask	= S2MPU02_ENABLE_MASK		\
1006 }
1007 
1008 #define regulator_desc_s2mpu02_buck1234(num) {			\
1009 	.name		= "BUCK"#num,				\
1010 	.id		= S2MPU02_BUCK##num,			\
1011 	.ops		= &s2mpu02_buck_ops,			\
1012 	.type		= REGULATOR_VOLTAGE,			\
1013 	.owner		= THIS_MODULE,				\
1014 	.min_uV		= S2MPU02_BUCK1234_MIN_600MV,		\
1015 	.uV_step	= S2MPU02_BUCK1234_STEP_6_25MV,		\
1016 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
1017 	.linear_min_sel = S2MPU02_BUCK1234_START_SEL,		\
1018 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
1019 	.vsel_reg	= S2MPU02_REG_B1CTRL2 + (num - 1) * 2,	\
1020 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
1021 	.enable_reg	= S2MPU02_REG_B1CTRL1 + (num - 1) * 2,	\
1022 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1023 }
1024 #define regulator_desc_s2mpu02_buck5(num) {			\
1025 	.name		= "BUCK"#num,				\
1026 	.id		= S2MPU02_BUCK##num,			\
1027 	.ops		= &s2mpu02_ldo_ops,			\
1028 	.type		= REGULATOR_VOLTAGE,			\
1029 	.owner		= THIS_MODULE,				\
1030 	.min_uV		= S2MPU02_BUCK5_MIN_1081_25MV,		\
1031 	.uV_step	= S2MPU02_BUCK5_STEP_6_25MV,		\
1032 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
1033 	.linear_min_sel = S2MPU02_BUCK5_START_SEL,		\
1034 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
1035 	.vsel_reg	= S2MPU02_REG_B5CTRL2,			\
1036 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
1037 	.enable_reg	= S2MPU02_REG_B5CTRL1,			\
1038 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1039 }
1040 #define regulator_desc_s2mpu02_buck6(num) {			\
1041 	.name		= "BUCK"#num,				\
1042 	.id		= S2MPU02_BUCK##num,			\
1043 	.ops		= &s2mpu02_ldo_ops,			\
1044 	.type		= REGULATOR_VOLTAGE,			\
1045 	.owner		= THIS_MODULE,				\
1046 	.min_uV		= S2MPU02_BUCK6_MIN_1700MV,		\
1047 	.uV_step	= S2MPU02_BUCK6_STEP_2_50MV,		\
1048 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
1049 	.linear_min_sel = S2MPU02_BUCK6_START_SEL,		\
1050 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
1051 	.vsel_reg	= S2MPU02_REG_B6CTRL2,			\
1052 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
1053 	.enable_reg	= S2MPU02_REG_B6CTRL1,			\
1054 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1055 }
1056 #define regulator_desc_s2mpu02_buck7(num) {			\
1057 	.name		= "BUCK"#num,				\
1058 	.id		= S2MPU02_BUCK##num,			\
1059 	.ops		= &s2mpu02_ldo_ops,			\
1060 	.type		= REGULATOR_VOLTAGE,			\
1061 	.owner		= THIS_MODULE,				\
1062 	.min_uV		= S2MPU02_BUCK7_MIN_900MV,		\
1063 	.uV_step	= S2MPU02_BUCK7_STEP_6_25MV,		\
1064 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
1065 	.linear_min_sel = S2MPU02_BUCK7_START_SEL,		\
1066 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
1067 	.vsel_reg	= S2MPU02_REG_B7CTRL2,			\
1068 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
1069 	.enable_reg	= S2MPU02_REG_B7CTRL1,			\
1070 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1071 }
1072 
1073 static const struct regulator_desc s2mpu02_regulators[] = {
1074 	regulator_desc_s2mpu02_ldo1(1),
1075 	regulator_desc_s2mpu02_ldo2(2),
1076 	regulator_desc_s2mpu02_ldo4(3),
1077 	regulator_desc_s2mpu02_ldo5(4),
1078 	regulator_desc_s2mpu02_ldo4(5),
1079 	regulator_desc_s2mpu02_ldo3(6),
1080 	regulator_desc_s2mpu02_ldo3(7),
1081 	regulator_desc_s2mpu02_ldo4(8),
1082 	regulator_desc_s2mpu02_ldo5(9),
1083 	regulator_desc_s2mpu02_ldo3(10),
1084 	regulator_desc_s2mpu02_ldo4(11),
1085 	regulator_desc_s2mpu02_ldo5(12),
1086 	regulator_desc_s2mpu02_ldo5(13),
1087 	regulator_desc_s2mpu02_ldo5(14),
1088 	regulator_desc_s2mpu02_ldo5(15),
1089 	regulator_desc_s2mpu02_ldo5(16),
1090 	regulator_desc_s2mpu02_ldo4(17),
1091 	regulator_desc_s2mpu02_ldo5(18),
1092 	regulator_desc_s2mpu02_ldo3(19),
1093 	regulator_desc_s2mpu02_ldo4(20),
1094 	regulator_desc_s2mpu02_ldo5(21),
1095 	regulator_desc_s2mpu02_ldo5(22),
1096 	regulator_desc_s2mpu02_ldo5(23),
1097 	regulator_desc_s2mpu02_ldo4(24),
1098 	regulator_desc_s2mpu02_ldo5(25),
1099 	regulator_desc_s2mpu02_ldo4(26),
1100 	regulator_desc_s2mpu02_ldo5(27),
1101 	regulator_desc_s2mpu02_ldo5(28),
1102 	regulator_desc_s2mpu02_buck1234(1),
1103 	regulator_desc_s2mpu02_buck1234(2),
1104 	regulator_desc_s2mpu02_buck1234(3),
1105 	regulator_desc_s2mpu02_buck1234(4),
1106 	regulator_desc_s2mpu02_buck5(5),
1107 	regulator_desc_s2mpu02_buck6(6),
1108 	regulator_desc_s2mpu02_buck7(7),
1109 };
1110 
1111 static int s2mps11_pmic_probe(struct platform_device *pdev)
1112 {
1113 	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
1114 	struct sec_platform_data *pdata = NULL;
1115 	struct of_regulator_match *rdata = NULL;
1116 	struct regulator_config config = { };
1117 	struct s2mps11_info *s2mps11;
1118 	unsigned int rdev_num = 0;
1119 	int i, ret = 0;
1120 	const struct regulator_desc *regulators;
1121 
1122 	s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
1123 				GFP_KERNEL);
1124 	if (!s2mps11)
1125 		return -ENOMEM;
1126 
1127 	s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
1128 	switch (s2mps11->dev_type) {
1129 	case S2MPS11X:
1130 		rdev_num = ARRAY_SIZE(s2mps11_regulators);
1131 		regulators = s2mps11_regulators;
1132 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps11_regulators));
1133 		break;
1134 	case S2MPS13X:
1135 		rdev_num = ARRAY_SIZE(s2mps13_regulators);
1136 		regulators = s2mps13_regulators;
1137 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps13_regulators));
1138 		break;
1139 	case S2MPS14X:
1140 		rdev_num = ARRAY_SIZE(s2mps14_regulators);
1141 		regulators = s2mps14_regulators;
1142 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps14_regulators));
1143 		break;
1144 	case S2MPS15X:
1145 		rdev_num = ARRAY_SIZE(s2mps15_regulators);
1146 		regulators = s2mps15_regulators;
1147 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps15_regulators));
1148 		break;
1149 	case S2MPU02:
1150 		rdev_num = ARRAY_SIZE(s2mpu02_regulators);
1151 		regulators = s2mpu02_regulators;
1152 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mpu02_regulators));
1153 		break;
1154 	default:
1155 		dev_err(&pdev->dev, "Invalid device type: %u\n",
1156 				    s2mps11->dev_type);
1157 		return -EINVAL;
1158 	}
1159 
1160 	s2mps11->ext_control_gpiod = devm_kcalloc(&pdev->dev, rdev_num,
1161 			       sizeof(*s2mps11->ext_control_gpiod), GFP_KERNEL);
1162 	if (!s2mps11->ext_control_gpiod)
1163 		return -ENOMEM;
1164 
1165 	if (!iodev->dev->of_node) {
1166 		if (iodev->pdata) {
1167 			pdata = iodev->pdata;
1168 			goto common_reg;
1169 		} else {
1170 			dev_err(pdev->dev.parent,
1171 				"Platform data or DT node not supplied\n");
1172 			return -ENODEV;
1173 		}
1174 	}
1175 
1176 	rdata = kcalloc(rdev_num, sizeof(*rdata), GFP_KERNEL);
1177 	if (!rdata)
1178 		return -ENOMEM;
1179 
1180 	for (i = 0; i < rdev_num; i++)
1181 		rdata[i].name = regulators[i].name;
1182 
1183 	ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11, rdev_num);
1184 	if (ret)
1185 		goto out;
1186 
1187 common_reg:
1188 	platform_set_drvdata(pdev, s2mps11);
1189 
1190 	config.dev = &pdev->dev;
1191 	config.regmap = iodev->regmap_pmic;
1192 	config.driver_data = s2mps11;
1193 	for (i = 0; i < rdev_num; i++) {
1194 		struct regulator_dev *regulator;
1195 
1196 		if (pdata) {
1197 			config.init_data = pdata->regulators[i].initdata;
1198 			config.of_node = pdata->regulators[i].reg_node;
1199 		} else {
1200 			config.init_data = rdata[i].init_data;
1201 			config.of_node = rdata[i].of_node;
1202 		}
1203 		config.ena_gpiod = s2mps11->ext_control_gpiod[i];
1204 		/*
1205 		 * Hand the GPIO descriptor management over to the regulator
1206 		 * core, remove it from devres management.
1207 		 */
1208 		if (config.ena_gpiod)
1209 			devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
1210 		regulator = devm_regulator_register(&pdev->dev,
1211 						&regulators[i], &config);
1212 		if (IS_ERR(regulator)) {
1213 			ret = PTR_ERR(regulator);
1214 			dev_err(&pdev->dev, "regulator init failed for %d\n",
1215 				i);
1216 			goto out;
1217 		}
1218 
1219 		if (s2mps11->ext_control_gpiod[i]) {
1220 			ret = s2mps14_pmic_enable_ext_control(s2mps11,
1221 					regulator);
1222 			if (ret < 0) {
1223 				dev_err(&pdev->dev,
1224 						"failed to enable GPIO control over %s: %d\n",
1225 						regulator->desc->name, ret);
1226 				goto out;
1227 			}
1228 		}
1229 	}
1230 
1231 out:
1232 	kfree(rdata);
1233 
1234 	return ret;
1235 }
1236 
1237 static const struct platform_device_id s2mps11_pmic_id[] = {
1238 	{ "s2mps11-regulator", S2MPS11X},
1239 	{ "s2mps13-regulator", S2MPS13X},
1240 	{ "s2mps14-regulator", S2MPS14X},
1241 	{ "s2mps15-regulator", S2MPS15X},
1242 	{ "s2mpu02-regulator", S2MPU02},
1243 	{ },
1244 };
1245 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1246 
1247 static struct platform_driver s2mps11_pmic_driver = {
1248 	.driver = {
1249 		.name = "s2mps11-pmic",
1250 	},
1251 	.probe = s2mps11_pmic_probe,
1252 	.id_table = s2mps11_pmic_id,
1253 };
1254 
1255 module_platform_driver(s2mps11_pmic_driver);
1256 
1257 /* Module information */
1258 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
1259 MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPS15/S2MPU02 Regulator Driver");
1260 MODULE_LICENSE("GPL");
1261