xref: /openbmc/linux/drivers/regulator/s2mps11.c (revision 151f4e2b)
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 			continue;
828 		}
829 		if (gpio[reg])
830 			dev_dbg(&pdev->dev, "Using GPIO for ext-control over %d/%s\n",
831 				reg, rdata[reg].name);
832 	}
833 }
834 
835 static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
836 		struct of_regulator_match *rdata, struct s2mps11_info *s2mps11,
837 		unsigned int rdev_num)
838 {
839 	struct device_node *reg_np;
840 
841 	reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
842 	if (!reg_np) {
843 		dev_err(&pdev->dev, "could not find regulators sub-node\n");
844 		return -EINVAL;
845 	}
846 
847 	of_regulator_match(&pdev->dev, reg_np, rdata, rdev_num);
848 	if (s2mps11->dev_type == S2MPS14X)
849 		s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
850 
851 	of_node_put(reg_np);
852 
853 	return 0;
854 }
855 
856 static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
857 {
858 	unsigned int ramp_val, ramp_shift, ramp_reg;
859 
860 	switch (rdev_get_id(rdev)) {
861 	case S2MPU02_BUCK1:
862 		ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
863 		break;
864 	case S2MPU02_BUCK2:
865 		ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
866 		break;
867 	case S2MPU02_BUCK3:
868 		ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
869 		break;
870 	case S2MPU02_BUCK4:
871 		ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
872 		break;
873 	default:
874 		return 0;
875 	}
876 	ramp_reg = S2MPU02_REG_RAMP1;
877 	ramp_val = get_ramp_delay(ramp_delay);
878 
879 	return regmap_update_bits(rdev->regmap, ramp_reg,
880 				  S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
881 				  ramp_val << ramp_shift);
882 }
883 
884 static const struct regulator_ops s2mpu02_ldo_ops = {
885 	.list_voltage		= regulator_list_voltage_linear,
886 	.map_voltage		= regulator_map_voltage_linear,
887 	.is_enabled		= regulator_is_enabled_regmap,
888 	.enable			= s2mps14_regulator_enable,
889 	.disable		= regulator_disable_regmap,
890 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
891 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
892 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
893 	.set_suspend_disable	= s2mps14_regulator_set_suspend_disable,
894 };
895 
896 static const struct regulator_ops s2mpu02_buck_ops = {
897 	.list_voltage		= regulator_list_voltage_linear,
898 	.map_voltage		= regulator_map_voltage_linear,
899 	.is_enabled		= regulator_is_enabled_regmap,
900 	.enable			= s2mps14_regulator_enable,
901 	.disable		= regulator_disable_regmap,
902 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
903 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
904 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
905 	.set_suspend_disable	= s2mps14_regulator_set_suspend_disable,
906 	.set_ramp_delay		= s2mpu02_set_ramp_delay,
907 };
908 
909 #define regulator_desc_s2mpu02_ldo1(num) {		\
910 	.name		= "LDO"#num,			\
911 	.id		= S2MPU02_LDO##num,		\
912 	.ops		= &s2mpu02_ldo_ops,		\
913 	.type		= REGULATOR_VOLTAGE,		\
914 	.owner		= THIS_MODULE,			\
915 	.min_uV		= S2MPU02_LDO_MIN_900MV,	\
916 	.uV_step	= S2MPU02_LDO_STEP_12_5MV,	\
917 	.linear_min_sel	= S2MPU02_LDO_GROUP1_START_SEL,	\
918 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
919 	.vsel_reg	= S2MPU02_REG_L1CTRL,		\
920 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
921 	.enable_reg	= S2MPU02_REG_L1CTRL,		\
922 	.enable_mask	= S2MPU02_ENABLE_MASK		\
923 }
924 #define regulator_desc_s2mpu02_ldo2(num) {		\
925 	.name		= "LDO"#num,			\
926 	.id		= S2MPU02_LDO##num,		\
927 	.ops		= &s2mpu02_ldo_ops,		\
928 	.type		= REGULATOR_VOLTAGE,		\
929 	.owner		= THIS_MODULE,			\
930 	.min_uV		= S2MPU02_LDO_MIN_1050MV,	\
931 	.uV_step	= S2MPU02_LDO_STEP_25MV,	\
932 	.linear_min_sel	= S2MPU02_LDO_GROUP2_START_SEL,	\
933 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
934 	.vsel_reg	= S2MPU02_REG_L2CTRL1,		\
935 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
936 	.enable_reg	= S2MPU02_REG_L2CTRL1,		\
937 	.enable_mask	= S2MPU02_ENABLE_MASK		\
938 }
939 #define regulator_desc_s2mpu02_ldo3(num) {		\
940 	.name		= "LDO"#num,			\
941 	.id		= S2MPU02_LDO##num,		\
942 	.ops		= &s2mpu02_ldo_ops,		\
943 	.type		= REGULATOR_VOLTAGE,		\
944 	.owner		= THIS_MODULE,			\
945 	.min_uV		= S2MPU02_LDO_MIN_900MV,	\
946 	.uV_step	= S2MPU02_LDO_STEP_12_5MV,	\
947 	.linear_min_sel	= S2MPU02_LDO_GROUP1_START_SEL,	\
948 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
949 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
950 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
951 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
952 	.enable_mask	= S2MPU02_ENABLE_MASK		\
953 }
954 #define regulator_desc_s2mpu02_ldo4(num) {		\
955 	.name		= "LDO"#num,			\
956 	.id		= S2MPU02_LDO##num,		\
957 	.ops		= &s2mpu02_ldo_ops,		\
958 	.type		= REGULATOR_VOLTAGE,		\
959 	.owner		= THIS_MODULE,			\
960 	.min_uV		= S2MPU02_LDO_MIN_1050MV,	\
961 	.uV_step	= S2MPU02_LDO_STEP_25MV,	\
962 	.linear_min_sel	= S2MPU02_LDO_GROUP2_START_SEL,	\
963 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
964 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
965 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
966 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
967 	.enable_mask	= S2MPU02_ENABLE_MASK		\
968 }
969 #define regulator_desc_s2mpu02_ldo5(num) {		\
970 	.name		= "LDO"#num,			\
971 	.id		= S2MPU02_LDO##num,		\
972 	.ops		= &s2mpu02_ldo_ops,		\
973 	.type		= REGULATOR_VOLTAGE,		\
974 	.owner		= THIS_MODULE,			\
975 	.min_uV		= S2MPU02_LDO_MIN_1600MV,	\
976 	.uV_step	= S2MPU02_LDO_STEP_50MV,	\
977 	.linear_min_sel	= S2MPU02_LDO_GROUP3_START_SEL,	\
978 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
979 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
980 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
981 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
982 	.enable_mask	= S2MPU02_ENABLE_MASK		\
983 }
984 
985 #define regulator_desc_s2mpu02_buck1234(num) {			\
986 	.name		= "BUCK"#num,				\
987 	.id		= S2MPU02_BUCK##num,			\
988 	.ops		= &s2mpu02_buck_ops,			\
989 	.type		= REGULATOR_VOLTAGE,			\
990 	.owner		= THIS_MODULE,				\
991 	.min_uV		= S2MPU02_BUCK1234_MIN_600MV,		\
992 	.uV_step	= S2MPU02_BUCK1234_STEP_6_25MV,		\
993 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
994 	.linear_min_sel = S2MPU02_BUCK1234_START_SEL,		\
995 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
996 	.vsel_reg	= S2MPU02_REG_B1CTRL2 + (num - 1) * 2,	\
997 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
998 	.enable_reg	= S2MPU02_REG_B1CTRL1 + (num - 1) * 2,	\
999 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1000 }
1001 #define regulator_desc_s2mpu02_buck5(num) {			\
1002 	.name		= "BUCK"#num,				\
1003 	.id		= S2MPU02_BUCK##num,			\
1004 	.ops		= &s2mpu02_ldo_ops,			\
1005 	.type		= REGULATOR_VOLTAGE,			\
1006 	.owner		= THIS_MODULE,				\
1007 	.min_uV		= S2MPU02_BUCK5_MIN_1081_25MV,		\
1008 	.uV_step	= S2MPU02_BUCK5_STEP_6_25MV,		\
1009 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
1010 	.linear_min_sel = S2MPU02_BUCK5_START_SEL,		\
1011 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
1012 	.vsel_reg	= S2MPU02_REG_B5CTRL2,			\
1013 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
1014 	.enable_reg	= S2MPU02_REG_B5CTRL1,			\
1015 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1016 }
1017 #define regulator_desc_s2mpu02_buck6(num) {			\
1018 	.name		= "BUCK"#num,				\
1019 	.id		= S2MPU02_BUCK##num,			\
1020 	.ops		= &s2mpu02_ldo_ops,			\
1021 	.type		= REGULATOR_VOLTAGE,			\
1022 	.owner		= THIS_MODULE,				\
1023 	.min_uV		= S2MPU02_BUCK6_MIN_1700MV,		\
1024 	.uV_step	= S2MPU02_BUCK6_STEP_2_50MV,		\
1025 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
1026 	.linear_min_sel = S2MPU02_BUCK6_START_SEL,		\
1027 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
1028 	.vsel_reg	= S2MPU02_REG_B6CTRL2,			\
1029 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
1030 	.enable_reg	= S2MPU02_REG_B6CTRL1,			\
1031 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1032 }
1033 #define regulator_desc_s2mpu02_buck7(num) {			\
1034 	.name		= "BUCK"#num,				\
1035 	.id		= S2MPU02_BUCK##num,			\
1036 	.ops		= &s2mpu02_ldo_ops,			\
1037 	.type		= REGULATOR_VOLTAGE,			\
1038 	.owner		= THIS_MODULE,				\
1039 	.min_uV		= S2MPU02_BUCK7_MIN_900MV,		\
1040 	.uV_step	= S2MPU02_BUCK7_STEP_6_25MV,		\
1041 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
1042 	.linear_min_sel = S2MPU02_BUCK7_START_SEL,		\
1043 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
1044 	.vsel_reg	= S2MPU02_REG_B7CTRL2,			\
1045 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
1046 	.enable_reg	= S2MPU02_REG_B7CTRL1,			\
1047 	.enable_mask	= S2MPU02_ENABLE_MASK			\
1048 }
1049 
1050 static const struct regulator_desc s2mpu02_regulators[] = {
1051 	regulator_desc_s2mpu02_ldo1(1),
1052 	regulator_desc_s2mpu02_ldo2(2),
1053 	regulator_desc_s2mpu02_ldo4(3),
1054 	regulator_desc_s2mpu02_ldo5(4),
1055 	regulator_desc_s2mpu02_ldo4(5),
1056 	regulator_desc_s2mpu02_ldo3(6),
1057 	regulator_desc_s2mpu02_ldo3(7),
1058 	regulator_desc_s2mpu02_ldo4(8),
1059 	regulator_desc_s2mpu02_ldo5(9),
1060 	regulator_desc_s2mpu02_ldo3(10),
1061 	regulator_desc_s2mpu02_ldo4(11),
1062 	regulator_desc_s2mpu02_ldo5(12),
1063 	regulator_desc_s2mpu02_ldo5(13),
1064 	regulator_desc_s2mpu02_ldo5(14),
1065 	regulator_desc_s2mpu02_ldo5(15),
1066 	regulator_desc_s2mpu02_ldo5(16),
1067 	regulator_desc_s2mpu02_ldo4(17),
1068 	regulator_desc_s2mpu02_ldo5(18),
1069 	regulator_desc_s2mpu02_ldo3(19),
1070 	regulator_desc_s2mpu02_ldo4(20),
1071 	regulator_desc_s2mpu02_ldo5(21),
1072 	regulator_desc_s2mpu02_ldo5(22),
1073 	regulator_desc_s2mpu02_ldo5(23),
1074 	regulator_desc_s2mpu02_ldo4(24),
1075 	regulator_desc_s2mpu02_ldo5(25),
1076 	regulator_desc_s2mpu02_ldo4(26),
1077 	regulator_desc_s2mpu02_ldo5(27),
1078 	regulator_desc_s2mpu02_ldo5(28),
1079 	regulator_desc_s2mpu02_buck1234(1),
1080 	regulator_desc_s2mpu02_buck1234(2),
1081 	regulator_desc_s2mpu02_buck1234(3),
1082 	regulator_desc_s2mpu02_buck1234(4),
1083 	regulator_desc_s2mpu02_buck5(5),
1084 	regulator_desc_s2mpu02_buck6(6),
1085 	regulator_desc_s2mpu02_buck7(7),
1086 };
1087 
1088 static int s2mps11_pmic_probe(struct platform_device *pdev)
1089 {
1090 	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
1091 	struct sec_platform_data *pdata = NULL;
1092 	struct of_regulator_match *rdata = NULL;
1093 	struct regulator_config config = { };
1094 	struct s2mps11_info *s2mps11;
1095 	unsigned int rdev_num = 0;
1096 	int i, ret = 0;
1097 	const struct regulator_desc *regulators;
1098 
1099 	s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
1100 				GFP_KERNEL);
1101 	if (!s2mps11)
1102 		return -ENOMEM;
1103 
1104 	s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
1105 	switch (s2mps11->dev_type) {
1106 	case S2MPS11X:
1107 		rdev_num = ARRAY_SIZE(s2mps11_regulators);
1108 		regulators = s2mps11_regulators;
1109 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps11_regulators));
1110 		break;
1111 	case S2MPS13X:
1112 		rdev_num = ARRAY_SIZE(s2mps13_regulators);
1113 		regulators = s2mps13_regulators;
1114 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps13_regulators));
1115 		break;
1116 	case S2MPS14X:
1117 		rdev_num = ARRAY_SIZE(s2mps14_regulators);
1118 		regulators = s2mps14_regulators;
1119 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps14_regulators));
1120 		break;
1121 	case S2MPS15X:
1122 		rdev_num = ARRAY_SIZE(s2mps15_regulators);
1123 		regulators = s2mps15_regulators;
1124 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps15_regulators));
1125 		break;
1126 	case S2MPU02:
1127 		rdev_num = ARRAY_SIZE(s2mpu02_regulators);
1128 		regulators = s2mpu02_regulators;
1129 		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mpu02_regulators));
1130 		break;
1131 	default:
1132 		dev_err(&pdev->dev, "Invalid device type: %u\n",
1133 				    s2mps11->dev_type);
1134 		return -EINVAL;
1135 	}
1136 
1137 	s2mps11->ext_control_gpiod = devm_kcalloc(&pdev->dev, rdev_num,
1138 			       sizeof(*s2mps11->ext_control_gpiod), GFP_KERNEL);
1139 	if (!s2mps11->ext_control_gpiod)
1140 		return -ENOMEM;
1141 
1142 	if (!iodev->dev->of_node) {
1143 		if (iodev->pdata) {
1144 			pdata = iodev->pdata;
1145 			goto common_reg;
1146 		} else {
1147 			dev_err(pdev->dev.parent,
1148 				"Platform data or DT node not supplied\n");
1149 			return -ENODEV;
1150 		}
1151 	}
1152 
1153 	rdata = kcalloc(rdev_num, sizeof(*rdata), GFP_KERNEL);
1154 	if (!rdata)
1155 		return -ENOMEM;
1156 
1157 	for (i = 0; i < rdev_num; i++)
1158 		rdata[i].name = regulators[i].name;
1159 
1160 	ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11, rdev_num);
1161 	if (ret)
1162 		goto out;
1163 
1164 common_reg:
1165 	platform_set_drvdata(pdev, s2mps11);
1166 
1167 	config.dev = &pdev->dev;
1168 	config.regmap = iodev->regmap_pmic;
1169 	config.driver_data = s2mps11;
1170 	for (i = 0; i < rdev_num; i++) {
1171 		struct regulator_dev *regulator;
1172 
1173 		if (pdata) {
1174 			config.init_data = pdata->regulators[i].initdata;
1175 			config.of_node = pdata->regulators[i].reg_node;
1176 		} else {
1177 			config.init_data = rdata[i].init_data;
1178 			config.of_node = rdata[i].of_node;
1179 		}
1180 		config.ena_gpiod = s2mps11->ext_control_gpiod[i];
1181 		/*
1182 		 * Hand the GPIO descriptor management over to the regulator
1183 		 * core, remove it from devres management.
1184 		 */
1185 		if (config.ena_gpiod)
1186 			devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
1187 		regulator = devm_regulator_register(&pdev->dev,
1188 						&regulators[i], &config);
1189 		if (IS_ERR(regulator)) {
1190 			ret = PTR_ERR(regulator);
1191 			dev_err(&pdev->dev, "regulator init failed for %d\n",
1192 				i);
1193 			goto out;
1194 		}
1195 
1196 		if (s2mps11->ext_control_gpiod[i]) {
1197 			ret = s2mps14_pmic_enable_ext_control(s2mps11,
1198 					regulator);
1199 			if (ret < 0) {
1200 				dev_err(&pdev->dev,
1201 						"failed to enable GPIO control over %s: %d\n",
1202 						regulator->desc->name, ret);
1203 				goto out;
1204 			}
1205 		}
1206 	}
1207 
1208 out:
1209 	kfree(rdata);
1210 
1211 	return ret;
1212 }
1213 
1214 static const struct platform_device_id s2mps11_pmic_id[] = {
1215 	{ "s2mps11-regulator", S2MPS11X},
1216 	{ "s2mps13-regulator", S2MPS13X},
1217 	{ "s2mps14-regulator", S2MPS14X},
1218 	{ "s2mps15-regulator", S2MPS15X},
1219 	{ "s2mpu02-regulator", S2MPU02},
1220 	{ },
1221 };
1222 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1223 
1224 static struct platform_driver s2mps11_pmic_driver = {
1225 	.driver = {
1226 		.name = "s2mps11-pmic",
1227 	},
1228 	.probe = s2mps11_pmic_probe,
1229 	.id_table = s2mps11_pmic_id,
1230 };
1231 
1232 module_platform_driver(s2mps11_pmic_driver);
1233 
1234 /* Module information */
1235 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
1236 MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPS15/S2MPU02 Regulator Driver");
1237 MODULE_LICENSE("GPL");
1238