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