xref: /openbmc/linux/drivers/regulator/s2mps11.c (revision 33ac9dba)
1 /*
2  * s2mps11.c
3  *
4  * Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
5  *              http://www.samsung.com
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18 
19 #include <linux/bug.h>
20 #include <linux/err.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/regmap.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/machine.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/of_gpio.h>
31 #include <linux/mfd/samsung/core.h>
32 #include <linux/mfd/samsung/s2mps11.h>
33 #include <linux/mfd/samsung/s2mps14.h>
34 #include <linux/mfd/samsung/s2mpu02.h>
35 
36 struct s2mps11_info {
37 	unsigned int rdev_num;
38 	int ramp_delay2;
39 	int ramp_delay34;
40 	int ramp_delay5;
41 	int ramp_delay16;
42 	int ramp_delay7810;
43 	int ramp_delay9;
44 
45 	enum sec_device_type dev_type;
46 
47 	/*
48 	 * One bit for each S2MPS14/S2MPU02 regulator whether the suspend mode
49 	 * was enabled.
50 	 */
51 	unsigned long long s2mps14_suspend_state:35;
52 
53 	/* Array of size rdev_num with GPIO-s for external sleep control */
54 	int *ext_control_gpio;
55 };
56 
57 static int get_ramp_delay(int ramp_delay)
58 {
59 	unsigned char cnt = 0;
60 
61 	ramp_delay /= 6250;
62 
63 	while (true) {
64 		ramp_delay = ramp_delay >> 1;
65 		if (ramp_delay == 0)
66 			break;
67 		cnt++;
68 	}
69 
70 	if (cnt > 3)
71 		cnt = 3;
72 
73 	return cnt;
74 }
75 
76 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
77 				   unsigned int old_selector,
78 				   unsigned int new_selector)
79 {
80 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
81 	unsigned int ramp_delay = 0;
82 	int old_volt, new_volt;
83 
84 	switch (rdev_get_id(rdev)) {
85 	case S2MPS11_BUCK2:
86 		ramp_delay = s2mps11->ramp_delay2;
87 		break;
88 	case S2MPS11_BUCK3:
89 	case S2MPS11_BUCK4:
90 		ramp_delay = s2mps11->ramp_delay34;
91 		break;
92 	case S2MPS11_BUCK5:
93 		ramp_delay = s2mps11->ramp_delay5;
94 		break;
95 	case S2MPS11_BUCK6:
96 	case S2MPS11_BUCK1:
97 		ramp_delay = s2mps11->ramp_delay16;
98 		break;
99 	case S2MPS11_BUCK7:
100 	case S2MPS11_BUCK8:
101 	case S2MPS11_BUCK10:
102 		ramp_delay = s2mps11->ramp_delay7810;
103 		break;
104 	case S2MPS11_BUCK9:
105 		ramp_delay = s2mps11->ramp_delay9;
106 	}
107 
108 	if (ramp_delay == 0)
109 		ramp_delay = rdev->desc->ramp_delay;
110 
111 	old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
112 	new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
113 
114 	return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
115 }
116 
117 static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
118 {
119 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
120 	unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
121 	unsigned int ramp_enable = 1, enable_shift = 0;
122 	int ret;
123 
124 	switch (rdev_get_id(rdev)) {
125 	case S2MPS11_BUCK1:
126 		if (ramp_delay > s2mps11->ramp_delay16)
127 			s2mps11->ramp_delay16 = ramp_delay;
128 		else
129 			ramp_delay = s2mps11->ramp_delay16;
130 
131 		ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
132 		break;
133 	case S2MPS11_BUCK2:
134 		enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
135 		if (!ramp_delay) {
136 			ramp_enable = 0;
137 			break;
138 		}
139 
140 		s2mps11->ramp_delay2 = ramp_delay;
141 		ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
142 		ramp_reg = S2MPS11_REG_RAMP;
143 		break;
144 	case S2MPS11_BUCK3:
145 		enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
146 		if (!ramp_delay) {
147 			ramp_enable = 0;
148 			break;
149 		}
150 
151 		if (ramp_delay > s2mps11->ramp_delay34)
152 			s2mps11->ramp_delay34 = ramp_delay;
153 		else
154 			ramp_delay = s2mps11->ramp_delay34;
155 
156 		ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
157 		ramp_reg = S2MPS11_REG_RAMP;
158 		break;
159 	case S2MPS11_BUCK4:
160 		enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
161 		if (!ramp_delay) {
162 			ramp_enable = 0;
163 			break;
164 		}
165 
166 		if (ramp_delay > s2mps11->ramp_delay34)
167 			s2mps11->ramp_delay34 = ramp_delay;
168 		else
169 			ramp_delay = s2mps11->ramp_delay34;
170 
171 		ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
172 		ramp_reg = S2MPS11_REG_RAMP;
173 		break;
174 	case S2MPS11_BUCK5:
175 		s2mps11->ramp_delay5 = ramp_delay;
176 		ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
177 		break;
178 	case S2MPS11_BUCK6:
179 		enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
180 		if (!ramp_delay) {
181 			ramp_enable = 0;
182 			break;
183 		}
184 
185 		if (ramp_delay > s2mps11->ramp_delay16)
186 			s2mps11->ramp_delay16 = ramp_delay;
187 		else
188 			ramp_delay = s2mps11->ramp_delay16;
189 
190 		ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
191 		break;
192 	case S2MPS11_BUCK7:
193 	case S2MPS11_BUCK8:
194 	case S2MPS11_BUCK10:
195 		if (ramp_delay > s2mps11->ramp_delay7810)
196 			s2mps11->ramp_delay7810 = ramp_delay;
197 		else
198 			ramp_delay = s2mps11->ramp_delay7810;
199 
200 		ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
201 		break;
202 	case S2MPS11_BUCK9:
203 		s2mps11->ramp_delay9 = ramp_delay;
204 		ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
205 		break;
206 	default:
207 		return 0;
208 	}
209 
210 	if (!ramp_enable)
211 		goto ramp_disable;
212 
213 	/* Ramp delay can be enabled/disabled only for buck[2346] */
214 	if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 &&
215 			rdev_get_id(rdev) <= S2MPS11_BUCK4) ||
216 			rdev_get_id(rdev) == S2MPS11_BUCK6)  {
217 		ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
218 					 1 << enable_shift, 1 << enable_shift);
219 		if (ret) {
220 			dev_err(&rdev->dev, "failed to enable ramp rate\n");
221 			return ret;
222 		}
223 	}
224 
225 	ramp_val = get_ramp_delay(ramp_delay);
226 
227 	return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
228 				  ramp_val << ramp_shift);
229 
230 ramp_disable:
231 	return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
232 				  1 << enable_shift, 0);
233 }
234 
235 static struct regulator_ops s2mps11_ldo_ops = {
236 	.list_voltage		= regulator_list_voltage_linear,
237 	.map_voltage		= regulator_map_voltage_linear,
238 	.is_enabled		= regulator_is_enabled_regmap,
239 	.enable			= regulator_enable_regmap,
240 	.disable		= regulator_disable_regmap,
241 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
242 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
243 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
244 };
245 
246 static struct regulator_ops s2mps11_buck_ops = {
247 	.list_voltage		= regulator_list_voltage_linear,
248 	.map_voltage		= regulator_map_voltage_linear,
249 	.is_enabled		= regulator_is_enabled_regmap,
250 	.enable			= regulator_enable_regmap,
251 	.disable		= regulator_disable_regmap,
252 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
253 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
254 	.set_voltage_time_sel	= s2mps11_regulator_set_voltage_time_sel,
255 	.set_ramp_delay		= s2mps11_set_ramp_delay,
256 };
257 
258 #define regulator_desc_s2mps11_ldo1(num)	{		\
259 	.name		= "LDO"#num,			\
260 	.id		= S2MPS11_LDO##num,		\
261 	.ops		= &s2mps11_ldo_ops,		\
262 	.type		= REGULATOR_VOLTAGE,		\
263 	.owner		= THIS_MODULE,			\
264 	.min_uV		= S2MPS11_LDO_MIN,		\
265 	.uV_step	= S2MPS11_LDO_STEP1,		\
266 	.n_voltages	= S2MPS11_LDO_N_VOLTAGES,	\
267 	.vsel_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
268 	.vsel_mask	= S2MPS11_LDO_VSEL_MASK,	\
269 	.enable_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
270 	.enable_mask	= S2MPS11_ENABLE_MASK		\
271 }
272 #define regulator_desc_s2mps11_ldo2(num) {		\
273 	.name		= "LDO"#num,			\
274 	.id		= S2MPS11_LDO##num,		\
275 	.ops		= &s2mps11_ldo_ops,		\
276 	.type		= REGULATOR_VOLTAGE,		\
277 	.owner		= THIS_MODULE,			\
278 	.min_uV		= S2MPS11_LDO_MIN,		\
279 	.uV_step	= S2MPS11_LDO_STEP2,		\
280 	.n_voltages	= S2MPS11_LDO_N_VOLTAGES,	\
281 	.vsel_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
282 	.vsel_mask	= S2MPS11_LDO_VSEL_MASK,	\
283 	.enable_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
284 	.enable_mask	= S2MPS11_ENABLE_MASK		\
285 }
286 
287 #define regulator_desc_s2mps11_buck1_4(num) {			\
288 	.name		= "BUCK"#num,				\
289 	.id		= S2MPS11_BUCK##num,			\
290 	.ops		= &s2mps11_buck_ops,			\
291 	.type		= REGULATOR_VOLTAGE,			\
292 	.owner		= THIS_MODULE,				\
293 	.min_uV		= S2MPS11_BUCK_MIN1,			\
294 	.uV_step	= S2MPS11_BUCK_STEP1,			\
295 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
296 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
297 	.vsel_reg	= S2MPS11_REG_B1CTRL2 + (num - 1) * 2,	\
298 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
299 	.enable_reg	= S2MPS11_REG_B1CTRL1 + (num - 1) * 2,	\
300 	.enable_mask	= S2MPS11_ENABLE_MASK			\
301 }
302 
303 #define regulator_desc_s2mps11_buck5 {				\
304 	.name		= "BUCK5",				\
305 	.id		= S2MPS11_BUCK5,			\
306 	.ops		= &s2mps11_buck_ops,			\
307 	.type		= REGULATOR_VOLTAGE,			\
308 	.owner		= THIS_MODULE,				\
309 	.min_uV		= S2MPS11_BUCK_MIN1,			\
310 	.uV_step	= S2MPS11_BUCK_STEP1,			\
311 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
312 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
313 	.vsel_reg	= S2MPS11_REG_B5CTRL2,			\
314 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
315 	.enable_reg	= S2MPS11_REG_B5CTRL1,			\
316 	.enable_mask	= S2MPS11_ENABLE_MASK			\
317 }
318 
319 #define regulator_desc_s2mps11_buck6_8(num) {			\
320 	.name		= "BUCK"#num,				\
321 	.id		= S2MPS11_BUCK##num,			\
322 	.ops		= &s2mps11_buck_ops,			\
323 	.type		= REGULATOR_VOLTAGE,			\
324 	.owner		= THIS_MODULE,				\
325 	.min_uV		= S2MPS11_BUCK_MIN1,			\
326 	.uV_step	= S2MPS11_BUCK_STEP1,			\
327 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
328 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
329 	.vsel_reg	= S2MPS11_REG_B6CTRL2 + (num - 6) * 2,	\
330 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
331 	.enable_reg	= S2MPS11_REG_B6CTRL1 + (num - 6) * 2,	\
332 	.enable_mask	= S2MPS11_ENABLE_MASK			\
333 }
334 
335 #define regulator_desc_s2mps11_buck9 {				\
336 	.name		= "BUCK9",				\
337 	.id		= S2MPS11_BUCK9,			\
338 	.ops		= &s2mps11_buck_ops,			\
339 	.type		= REGULATOR_VOLTAGE,			\
340 	.owner		= THIS_MODULE,				\
341 	.min_uV		= S2MPS11_BUCK_MIN3,			\
342 	.uV_step	= S2MPS11_BUCK_STEP3,			\
343 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
344 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
345 	.vsel_reg	= S2MPS11_REG_B9CTRL2,			\
346 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
347 	.enable_reg	= S2MPS11_REG_B9CTRL1,			\
348 	.enable_mask	= S2MPS11_ENABLE_MASK			\
349 }
350 
351 #define regulator_desc_s2mps11_buck10 {				\
352 	.name		= "BUCK10",				\
353 	.id		= S2MPS11_BUCK10,			\
354 	.ops		= &s2mps11_buck_ops,			\
355 	.type		= REGULATOR_VOLTAGE,			\
356 	.owner		= THIS_MODULE,				\
357 	.min_uV		= S2MPS11_BUCK_MIN2,			\
358 	.uV_step	= S2MPS11_BUCK_STEP2,			\
359 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
360 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
361 	.vsel_reg	= S2MPS11_REG_B10CTRL2,			\
362 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
363 	.enable_reg	= S2MPS11_REG_B10CTRL1,			\
364 	.enable_mask	= S2MPS11_ENABLE_MASK			\
365 }
366 
367 static const struct regulator_desc s2mps11_regulators[] = {
368 	regulator_desc_s2mps11_ldo2(1),
369 	regulator_desc_s2mps11_ldo1(2),
370 	regulator_desc_s2mps11_ldo1(3),
371 	regulator_desc_s2mps11_ldo1(4),
372 	regulator_desc_s2mps11_ldo1(5),
373 	regulator_desc_s2mps11_ldo2(6),
374 	regulator_desc_s2mps11_ldo1(7),
375 	regulator_desc_s2mps11_ldo1(8),
376 	regulator_desc_s2mps11_ldo1(9),
377 	regulator_desc_s2mps11_ldo1(10),
378 	regulator_desc_s2mps11_ldo2(11),
379 	regulator_desc_s2mps11_ldo1(12),
380 	regulator_desc_s2mps11_ldo1(13),
381 	regulator_desc_s2mps11_ldo1(14),
382 	regulator_desc_s2mps11_ldo1(15),
383 	regulator_desc_s2mps11_ldo1(16),
384 	regulator_desc_s2mps11_ldo1(17),
385 	regulator_desc_s2mps11_ldo1(18),
386 	regulator_desc_s2mps11_ldo1(19),
387 	regulator_desc_s2mps11_ldo1(20),
388 	regulator_desc_s2mps11_ldo1(21),
389 	regulator_desc_s2mps11_ldo2(22),
390 	regulator_desc_s2mps11_ldo2(23),
391 	regulator_desc_s2mps11_ldo1(24),
392 	regulator_desc_s2mps11_ldo1(25),
393 	regulator_desc_s2mps11_ldo1(26),
394 	regulator_desc_s2mps11_ldo2(27),
395 	regulator_desc_s2mps11_ldo1(28),
396 	regulator_desc_s2mps11_ldo1(29),
397 	regulator_desc_s2mps11_ldo1(30),
398 	regulator_desc_s2mps11_ldo1(31),
399 	regulator_desc_s2mps11_ldo1(32),
400 	regulator_desc_s2mps11_ldo1(33),
401 	regulator_desc_s2mps11_ldo1(34),
402 	regulator_desc_s2mps11_ldo1(35),
403 	regulator_desc_s2mps11_ldo1(36),
404 	regulator_desc_s2mps11_ldo1(37),
405 	regulator_desc_s2mps11_ldo1(38),
406 	regulator_desc_s2mps11_buck1_4(1),
407 	regulator_desc_s2mps11_buck1_4(2),
408 	regulator_desc_s2mps11_buck1_4(3),
409 	regulator_desc_s2mps11_buck1_4(4),
410 	regulator_desc_s2mps11_buck5,
411 	regulator_desc_s2mps11_buck6_8(6),
412 	regulator_desc_s2mps11_buck6_8(7),
413 	regulator_desc_s2mps11_buck6_8(8),
414 	regulator_desc_s2mps11_buck9,
415 	regulator_desc_s2mps11_buck10,
416 };
417 
418 static int s2mps14_regulator_enable(struct regulator_dev *rdev)
419 {
420 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
421 	unsigned int val;
422 
423 	switch (s2mps11->dev_type) {
424 	case S2MPS14X:
425 		if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
426 			val = S2MPS14_ENABLE_SUSPEND;
427 		else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
428 			val = S2MPS14_ENABLE_EXT_CONTROL;
429 		else
430 			val = rdev->desc->enable_mask;
431 		break;
432 	case S2MPU02:
433 		if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
434 			val = S2MPU02_ENABLE_SUSPEND;
435 		else
436 			val = rdev->desc->enable_mask;
437 		break;
438 	default:
439 		return -EINVAL;
440 	};
441 
442 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
443 			rdev->desc->enable_mask, val);
444 }
445 
446 static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
447 {
448 	int ret;
449 	unsigned int val, state;
450 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
451 	int rdev_id = rdev_get_id(rdev);
452 
453 	/* Below LDO should be always on or does not support suspend mode. */
454 	switch (s2mps11->dev_type) {
455 	case S2MPS14X:
456 		switch (rdev_id) {
457 		case S2MPS14_LDO3:
458 			return 0;
459 		default:
460 			state = S2MPS14_ENABLE_SUSPEND;
461 			break;
462 		};
463 		break;
464 	case S2MPU02:
465 		switch (rdev_id) {
466 		case S2MPU02_LDO13:
467 		case S2MPU02_LDO14:
468 		case S2MPU02_LDO15:
469 		case S2MPU02_LDO17:
470 		case S2MPU02_BUCK7:
471 			state = S2MPU02_DISABLE_SUSPEND;
472 			break;
473 		default:
474 			state = S2MPU02_ENABLE_SUSPEND;
475 			break;
476 		};
477 		break;
478 	default:
479 		return -EINVAL;
480 	};
481 
482 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
483 	if (ret < 0)
484 		return ret;
485 
486 	s2mps11->s2mps14_suspend_state |= (1 << rdev_get_id(rdev));
487 	/*
488 	 * Don't enable suspend mode if regulator is already disabled because
489 	 * this would effectively for a short time turn on the regulator after
490 	 * resuming.
491 	 * However we still want to toggle the suspend_state bit for regulator
492 	 * in case if it got enabled before suspending the system.
493 	 */
494 	if (!(val & rdev->desc->enable_mask))
495 		return 0;
496 
497 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
498 			rdev->desc->enable_mask, state);
499 }
500 
501 static struct regulator_ops s2mps14_reg_ops = {
502 	.list_voltage		= regulator_list_voltage_linear,
503 	.map_voltage		= regulator_map_voltage_linear,
504 	.is_enabled		= regulator_is_enabled_regmap,
505 	.enable			= s2mps14_regulator_enable,
506 	.disable		= regulator_disable_regmap,
507 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
508 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
509 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
510 	.set_suspend_disable	= s2mps14_regulator_set_suspend_disable,
511 };
512 
513 #define regulator_desc_s2mps14_ldo1(num) {		\
514 	.name		= "LDO"#num,			\
515 	.id		= S2MPS14_LDO##num,		\
516 	.ops		= &s2mps14_reg_ops,		\
517 	.type		= REGULATOR_VOLTAGE,		\
518 	.owner		= THIS_MODULE,			\
519 	.min_uV		= S2MPS14_LDO_MIN_800MV,	\
520 	.uV_step	= S2MPS14_LDO_STEP_25MV,	\
521 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,	\
522 	.vsel_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
523 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,	\
524 	.enable_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
525 	.enable_mask	= S2MPS14_ENABLE_MASK		\
526 }
527 #define regulator_desc_s2mps14_ldo2(num) {		\
528 	.name		= "LDO"#num,			\
529 	.id		= S2MPS14_LDO##num,		\
530 	.ops		= &s2mps14_reg_ops,		\
531 	.type		= REGULATOR_VOLTAGE,		\
532 	.owner		= THIS_MODULE,			\
533 	.min_uV		= S2MPS14_LDO_MIN_1800MV,	\
534 	.uV_step	= S2MPS14_LDO_STEP_25MV,	\
535 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,	\
536 	.vsel_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
537 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,	\
538 	.enable_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
539 	.enable_mask	= S2MPS14_ENABLE_MASK		\
540 }
541 #define regulator_desc_s2mps14_ldo3(num) {		\
542 	.name		= "LDO"#num,			\
543 	.id		= S2MPS14_LDO##num,		\
544 	.ops		= &s2mps14_reg_ops,		\
545 	.type		= REGULATOR_VOLTAGE,		\
546 	.owner		= THIS_MODULE,			\
547 	.min_uV		= S2MPS14_LDO_MIN_800MV,	\
548 	.uV_step	= S2MPS14_LDO_STEP_12_5MV,	\
549 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,	\
550 	.vsel_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
551 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,	\
552 	.enable_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
553 	.enable_mask	= S2MPS14_ENABLE_MASK		\
554 }
555 #define regulator_desc_s2mps14_buck1235(num) {			\
556 	.name		= "BUCK"#num,				\
557 	.id		= S2MPS14_BUCK##num,			\
558 	.ops		= &s2mps14_reg_ops,			\
559 	.type		= REGULATOR_VOLTAGE,			\
560 	.owner		= THIS_MODULE,				\
561 	.min_uV		= S2MPS14_BUCK1235_MIN_600MV,		\
562 	.uV_step	= S2MPS14_BUCK1235_STEP_6_25MV,		\
563 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
564 	.linear_min_sel = S2MPS14_BUCK1235_START_SEL,		\
565 	.ramp_delay	= S2MPS14_BUCK_RAMP_DELAY,		\
566 	.vsel_reg	= S2MPS14_REG_B1CTRL2 + (num - 1) * 2,	\
567 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
568 	.enable_reg	= S2MPS14_REG_B1CTRL1 + (num - 1) * 2,	\
569 	.enable_mask	= S2MPS14_ENABLE_MASK			\
570 }
571 #define regulator_desc_s2mps14_buck4(num) {			\
572 	.name		= "BUCK"#num,				\
573 	.id		= S2MPS14_BUCK##num,			\
574 	.ops		= &s2mps14_reg_ops,			\
575 	.type		= REGULATOR_VOLTAGE,			\
576 	.owner		= THIS_MODULE,				\
577 	.min_uV		= S2MPS14_BUCK4_MIN_1400MV,		\
578 	.uV_step	= S2MPS14_BUCK4_STEP_12_5MV,		\
579 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
580 	.linear_min_sel = S2MPS14_BUCK4_START_SEL,		\
581 	.ramp_delay	= S2MPS14_BUCK_RAMP_DELAY,		\
582 	.vsel_reg	= S2MPS14_REG_B1CTRL2 + (num - 1) * 2,	\
583 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
584 	.enable_reg	= S2MPS14_REG_B1CTRL1 + (num - 1) * 2,	\
585 	.enable_mask	= S2MPS14_ENABLE_MASK			\
586 }
587 
588 static const struct regulator_desc s2mps14_regulators[] = {
589 	regulator_desc_s2mps14_ldo3(1),
590 	regulator_desc_s2mps14_ldo3(2),
591 	regulator_desc_s2mps14_ldo1(3),
592 	regulator_desc_s2mps14_ldo1(4),
593 	regulator_desc_s2mps14_ldo3(5),
594 	regulator_desc_s2mps14_ldo3(6),
595 	regulator_desc_s2mps14_ldo1(7),
596 	regulator_desc_s2mps14_ldo2(8),
597 	regulator_desc_s2mps14_ldo3(9),
598 	regulator_desc_s2mps14_ldo3(10),
599 	regulator_desc_s2mps14_ldo1(11),
600 	regulator_desc_s2mps14_ldo2(12),
601 	regulator_desc_s2mps14_ldo2(13),
602 	regulator_desc_s2mps14_ldo2(14),
603 	regulator_desc_s2mps14_ldo2(15),
604 	regulator_desc_s2mps14_ldo2(16),
605 	regulator_desc_s2mps14_ldo2(17),
606 	regulator_desc_s2mps14_ldo2(18),
607 	regulator_desc_s2mps14_ldo1(19),
608 	regulator_desc_s2mps14_ldo1(20),
609 	regulator_desc_s2mps14_ldo1(21),
610 	regulator_desc_s2mps14_ldo3(22),
611 	regulator_desc_s2mps14_ldo1(23),
612 	regulator_desc_s2mps14_ldo2(24),
613 	regulator_desc_s2mps14_ldo2(25),
614 	regulator_desc_s2mps14_buck1235(1),
615 	regulator_desc_s2mps14_buck1235(2),
616 	regulator_desc_s2mps14_buck1235(3),
617 	regulator_desc_s2mps14_buck4(4),
618 	regulator_desc_s2mps14_buck1235(5),
619 };
620 
621 static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
622 		struct regulator_dev *rdev)
623 {
624 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
625 			rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
626 }
627 
628 static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
629 		struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
630 {
631 	int *gpio = s2mps11->ext_control_gpio;
632 	unsigned int i;
633 	unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
634 		S2MPS14_LDO12 };
635 
636 	for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
637 		unsigned int reg = valid_regulators[i];
638 
639 		if (!rdata[reg].init_data || !rdata[reg].of_node)
640 			continue;
641 
642 		gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
643 				"samsung,ext-control-gpios", 0);
644 		if (gpio_is_valid(gpio[reg]))
645 			dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
646 					gpio[reg], reg, rdata[reg].name);
647 	}
648 }
649 
650 static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
651 		struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
652 {
653 	struct device_node *reg_np;
654 
655 	reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
656 	if (!reg_np) {
657 		dev_err(&pdev->dev, "could not find regulators sub-node\n");
658 		return -EINVAL;
659 	}
660 
661 	of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
662 	if (s2mps11->dev_type == S2MPS14X)
663 		s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
664 
665 	of_node_put(reg_np);
666 
667 	return 0;
668 }
669 
670 static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
671 {
672 	unsigned int ramp_val, ramp_shift, ramp_reg;
673 
674 	switch (rdev_get_id(rdev)) {
675 	case S2MPU02_BUCK1:
676 		ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
677 		break;
678 	case S2MPU02_BUCK2:
679 		ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
680 		break;
681 	case S2MPU02_BUCK3:
682 		ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
683 		break;
684 	case S2MPU02_BUCK4:
685 		ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
686 		break;
687 	default:
688 		return 0;
689 	}
690 	ramp_reg = S2MPU02_REG_RAMP1;
691 	ramp_val = get_ramp_delay(ramp_delay);
692 
693 	return regmap_update_bits(rdev->regmap, ramp_reg,
694 				  S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
695 				  ramp_val << ramp_shift);
696 }
697 
698 static struct regulator_ops s2mpu02_ldo_ops = {
699 	.list_voltage		= regulator_list_voltage_linear,
700 	.map_voltage		= regulator_map_voltage_linear,
701 	.is_enabled		= regulator_is_enabled_regmap,
702 	.enable			= s2mps14_regulator_enable,
703 	.disable		= regulator_disable_regmap,
704 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
705 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
706 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
707 	.set_suspend_disable	= s2mps14_regulator_set_suspend_disable,
708 };
709 
710 static struct regulator_ops s2mpu02_buck_ops = {
711 	.list_voltage		= regulator_list_voltage_linear,
712 	.map_voltage		= regulator_map_voltage_linear,
713 	.is_enabled		= regulator_is_enabled_regmap,
714 	.enable			= s2mps14_regulator_enable,
715 	.disable		= regulator_disable_regmap,
716 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
717 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
718 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
719 	.set_suspend_disable	= s2mps14_regulator_set_suspend_disable,
720 	.set_ramp_delay		= s2mpu02_set_ramp_delay,
721 };
722 
723 #define regulator_desc_s2mpu02_ldo1(num) {		\
724 	.name		= "LDO"#num,			\
725 	.id		= S2MPU02_LDO##num,		\
726 	.ops		= &s2mpu02_ldo_ops,		\
727 	.type		= REGULATOR_VOLTAGE,		\
728 	.owner		= THIS_MODULE,			\
729 	.min_uV		= S2MPU02_LDO_MIN_900MV,	\
730 	.uV_step	= S2MPU02_LDO_STEP_12_5MV,	\
731 	.linear_min_sel	= S2MPU02_LDO_GROUP1_START_SEL,	\
732 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
733 	.vsel_reg	= S2MPU02_REG_L1CTRL,		\
734 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
735 	.enable_reg	= S2MPU02_REG_L1CTRL,		\
736 	.enable_mask	= S2MPU02_ENABLE_MASK		\
737 }
738 #define regulator_desc_s2mpu02_ldo2(num) {		\
739 	.name		= "LDO"#num,			\
740 	.id		= S2MPU02_LDO##num,		\
741 	.ops		= &s2mpu02_ldo_ops,		\
742 	.type		= REGULATOR_VOLTAGE,		\
743 	.owner		= THIS_MODULE,			\
744 	.min_uV		= S2MPU02_LDO_MIN_1050MV,	\
745 	.uV_step	= S2MPU02_LDO_STEP_25MV,	\
746 	.linear_min_sel	= S2MPU02_LDO_GROUP2_START_SEL,	\
747 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
748 	.vsel_reg	= S2MPU02_REG_L2CTRL1,		\
749 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
750 	.enable_reg	= S2MPU02_REG_L2CTRL1,		\
751 	.enable_mask	= S2MPU02_ENABLE_MASK		\
752 }
753 #define regulator_desc_s2mpu02_ldo3(num) {		\
754 	.name		= "LDO"#num,			\
755 	.id		= S2MPU02_LDO##num,		\
756 	.ops		= &s2mpu02_ldo_ops,		\
757 	.type		= REGULATOR_VOLTAGE,		\
758 	.owner		= THIS_MODULE,			\
759 	.min_uV		= S2MPU02_LDO_MIN_900MV,	\
760 	.uV_step	= S2MPU02_LDO_STEP_12_5MV,	\
761 	.linear_min_sel	= S2MPU02_LDO_GROUP1_START_SEL,	\
762 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
763 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
764 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
765 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
766 	.enable_mask	= S2MPU02_ENABLE_MASK		\
767 }
768 #define regulator_desc_s2mpu02_ldo4(num) {		\
769 	.name		= "LDO"#num,			\
770 	.id		= S2MPU02_LDO##num,		\
771 	.ops		= &s2mpu02_ldo_ops,		\
772 	.type		= REGULATOR_VOLTAGE,		\
773 	.owner		= THIS_MODULE,			\
774 	.min_uV		= S2MPU02_LDO_MIN_1050MV,	\
775 	.uV_step	= S2MPU02_LDO_STEP_25MV,	\
776 	.linear_min_sel	= S2MPU02_LDO_GROUP2_START_SEL,	\
777 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
778 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
779 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
780 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
781 	.enable_mask	= S2MPU02_ENABLE_MASK		\
782 }
783 #define regulator_desc_s2mpu02_ldo5(num) {		\
784 	.name		= "LDO"#num,			\
785 	.id		= S2MPU02_LDO##num,		\
786 	.ops		= &s2mpu02_ldo_ops,		\
787 	.type		= REGULATOR_VOLTAGE,		\
788 	.owner		= THIS_MODULE,			\
789 	.min_uV		= S2MPU02_LDO_MIN_1600MV,	\
790 	.uV_step	= S2MPU02_LDO_STEP_50MV,	\
791 	.linear_min_sel	= S2MPU02_LDO_GROUP3_START_SEL,	\
792 	.n_voltages	= S2MPU02_LDO_N_VOLTAGES,	\
793 	.vsel_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
794 	.vsel_mask	= S2MPU02_LDO_VSEL_MASK,	\
795 	.enable_reg	= S2MPU02_REG_L3CTRL + num - 3,	\
796 	.enable_mask	= S2MPU02_ENABLE_MASK		\
797 }
798 
799 #define regulator_desc_s2mpu02_buck1234(num) {			\
800 	.name		= "BUCK"#num,				\
801 	.id		= S2MPU02_BUCK##num,			\
802 	.ops		= &s2mpu02_buck_ops,			\
803 	.type		= REGULATOR_VOLTAGE,			\
804 	.owner		= THIS_MODULE,				\
805 	.min_uV		= S2MPU02_BUCK1234_MIN_600MV,		\
806 	.uV_step	= S2MPU02_BUCK1234_STEP_6_25MV,		\
807 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
808 	.linear_min_sel = S2MPU02_BUCK1234_START_SEL,		\
809 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
810 	.vsel_reg	= S2MPU02_REG_B1CTRL2 + (num - 1) * 2,	\
811 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
812 	.enable_reg	= S2MPU02_REG_B1CTRL1 + (num - 1) * 2,	\
813 	.enable_mask	= S2MPU02_ENABLE_MASK			\
814 }
815 #define regulator_desc_s2mpu02_buck5(num) {			\
816 	.name		= "BUCK"#num,				\
817 	.id		= S2MPU02_BUCK##num,			\
818 	.ops		= &s2mpu02_ldo_ops,			\
819 	.type		= REGULATOR_VOLTAGE,			\
820 	.owner		= THIS_MODULE,				\
821 	.min_uV		= S2MPU02_BUCK5_MIN_1081_25MV,		\
822 	.uV_step	= S2MPU02_BUCK5_STEP_6_25MV,		\
823 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
824 	.linear_min_sel = S2MPU02_BUCK5_START_SEL,		\
825 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
826 	.vsel_reg	= S2MPU02_REG_B5CTRL2,			\
827 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
828 	.enable_reg	= S2MPU02_REG_B5CTRL1,			\
829 	.enable_mask	= S2MPU02_ENABLE_MASK			\
830 }
831 #define regulator_desc_s2mpu02_buck6(num) {			\
832 	.name		= "BUCK"#num,				\
833 	.id		= S2MPU02_BUCK##num,			\
834 	.ops		= &s2mpu02_ldo_ops,			\
835 	.type		= REGULATOR_VOLTAGE,			\
836 	.owner		= THIS_MODULE,				\
837 	.min_uV		= S2MPU02_BUCK6_MIN_1700MV,		\
838 	.uV_step	= S2MPU02_BUCK6_STEP_2_50MV,		\
839 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
840 	.linear_min_sel = S2MPU02_BUCK6_START_SEL,		\
841 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
842 	.vsel_reg	= S2MPU02_REG_B6CTRL2,			\
843 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
844 	.enable_reg	= S2MPU02_REG_B6CTRL1,			\
845 	.enable_mask	= S2MPU02_ENABLE_MASK			\
846 }
847 #define regulator_desc_s2mpu02_buck7(num) {			\
848 	.name		= "BUCK"#num,				\
849 	.id		= S2MPU02_BUCK##num,			\
850 	.ops		= &s2mpu02_ldo_ops,			\
851 	.type		= REGULATOR_VOLTAGE,			\
852 	.owner		= THIS_MODULE,				\
853 	.min_uV		= S2MPU02_BUCK7_MIN_900MV,		\
854 	.uV_step	= S2MPU02_BUCK7_STEP_6_25MV,		\
855 	.n_voltages	= S2MPU02_BUCK_N_VOLTAGES,		\
856 	.linear_min_sel = S2MPU02_BUCK7_START_SEL,		\
857 	.ramp_delay	= S2MPU02_BUCK_RAMP_DELAY,		\
858 	.vsel_reg	= S2MPU02_REG_B7CTRL2,			\
859 	.vsel_mask	= S2MPU02_BUCK_VSEL_MASK,		\
860 	.enable_reg	= S2MPU02_REG_B7CTRL1,			\
861 	.enable_mask	= S2MPU02_ENABLE_MASK			\
862 }
863 
864 static const struct regulator_desc s2mpu02_regulators[] = {
865 	regulator_desc_s2mpu02_ldo1(1),
866 	regulator_desc_s2mpu02_ldo2(2),
867 	regulator_desc_s2mpu02_ldo4(3),
868 	regulator_desc_s2mpu02_ldo5(4),
869 	regulator_desc_s2mpu02_ldo4(5),
870 	regulator_desc_s2mpu02_ldo3(6),
871 	regulator_desc_s2mpu02_ldo3(7),
872 	regulator_desc_s2mpu02_ldo4(8),
873 	regulator_desc_s2mpu02_ldo5(9),
874 	regulator_desc_s2mpu02_ldo3(10),
875 	regulator_desc_s2mpu02_ldo4(11),
876 	regulator_desc_s2mpu02_ldo5(12),
877 	regulator_desc_s2mpu02_ldo5(13),
878 	regulator_desc_s2mpu02_ldo5(14),
879 	regulator_desc_s2mpu02_ldo5(15),
880 	regulator_desc_s2mpu02_ldo5(16),
881 	regulator_desc_s2mpu02_ldo4(17),
882 	regulator_desc_s2mpu02_ldo5(18),
883 	regulator_desc_s2mpu02_ldo3(19),
884 	regulator_desc_s2mpu02_ldo4(20),
885 	regulator_desc_s2mpu02_ldo5(21),
886 	regulator_desc_s2mpu02_ldo5(22),
887 	regulator_desc_s2mpu02_ldo5(23),
888 	regulator_desc_s2mpu02_ldo4(24),
889 	regulator_desc_s2mpu02_ldo5(25),
890 	regulator_desc_s2mpu02_ldo4(26),
891 	regulator_desc_s2mpu02_ldo5(27),
892 	regulator_desc_s2mpu02_ldo5(28),
893 	regulator_desc_s2mpu02_buck1234(1),
894 	regulator_desc_s2mpu02_buck1234(2),
895 	regulator_desc_s2mpu02_buck1234(3),
896 	regulator_desc_s2mpu02_buck1234(4),
897 	regulator_desc_s2mpu02_buck5(5),
898 	regulator_desc_s2mpu02_buck6(6),
899 	regulator_desc_s2mpu02_buck7(7),
900 };
901 
902 static int s2mps11_pmic_probe(struct platform_device *pdev)
903 {
904 	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
905 	struct sec_platform_data *pdata = NULL;
906 	struct of_regulator_match *rdata = NULL;
907 	struct regulator_config config = { };
908 	struct s2mps11_info *s2mps11;
909 	int i, ret = 0;
910 	const struct regulator_desc *regulators;
911 
912 	s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
913 				GFP_KERNEL);
914 	if (!s2mps11)
915 		return -ENOMEM;
916 
917 	s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
918 	switch (s2mps11->dev_type) {
919 	case S2MPS11X:
920 		s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
921 		regulators = s2mps11_regulators;
922 		break;
923 	case S2MPS14X:
924 		s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
925 		regulators = s2mps14_regulators;
926 		break;
927 	case S2MPU02:
928 		s2mps11->rdev_num = ARRAY_SIZE(s2mpu02_regulators);
929 		regulators = s2mpu02_regulators;
930 		break;
931 	default:
932 		dev_err(&pdev->dev, "Invalid device type: %u\n",
933 				    s2mps11->dev_type);
934 		return -EINVAL;
935 	};
936 
937 	s2mps11->ext_control_gpio = devm_kzalloc(&pdev->dev,
938 			sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
939 			GFP_KERNEL);
940 	if (!s2mps11->ext_control_gpio)
941 		return -ENOMEM;
942 	/*
943 	 * 0 is a valid GPIO so initialize all GPIO-s to negative value
944 	 * to indicate that external control won't be used for this regulator.
945 	 */
946 	for (i = 0; i < s2mps11->rdev_num; i++)
947 		s2mps11->ext_control_gpio[i] = -EINVAL;
948 
949 	if (!iodev->dev->of_node) {
950 		if (iodev->pdata) {
951 			pdata = iodev->pdata;
952 			goto common_reg;
953 		} else {
954 			dev_err(pdev->dev.parent,
955 				"Platform data or DT node not supplied\n");
956 			return -ENODEV;
957 		}
958 	}
959 
960 	rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
961 	if (!rdata)
962 		return -ENOMEM;
963 
964 	for (i = 0; i < s2mps11->rdev_num; i++)
965 		rdata[i].name = regulators[i].name;
966 
967 	ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11);
968 	if (ret)
969 		goto out;
970 
971 common_reg:
972 	platform_set_drvdata(pdev, s2mps11);
973 
974 	config.dev = &pdev->dev;
975 	config.regmap = iodev->regmap_pmic;
976 	config.driver_data = s2mps11;
977 	config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
978 	for (i = 0; i < s2mps11->rdev_num; i++) {
979 		struct regulator_dev *regulator;
980 
981 		if (pdata) {
982 			config.init_data = pdata->regulators[i].initdata;
983 			config.of_node = pdata->regulators[i].reg_node;
984 		} else {
985 			config.init_data = rdata[i].init_data;
986 			config.of_node = rdata[i].of_node;
987 		}
988 		config.ena_gpio = s2mps11->ext_control_gpio[i];
989 
990 		regulator = devm_regulator_register(&pdev->dev,
991 						&regulators[i], &config);
992 		if (IS_ERR(regulator)) {
993 			ret = PTR_ERR(regulator);
994 			dev_err(&pdev->dev, "regulator init failed for %d\n",
995 				i);
996 			goto out;
997 		}
998 
999 		if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
1000 			ret = s2mps14_pmic_enable_ext_control(s2mps11,
1001 					regulator);
1002 			if (ret < 0) {
1003 				dev_err(&pdev->dev,
1004 						"failed to enable GPIO control over %s: %d\n",
1005 						regulator->desc->name, ret);
1006 				goto out;
1007 			}
1008 		}
1009 	}
1010 
1011 out:
1012 	kfree(rdata);
1013 
1014 	return ret;
1015 }
1016 
1017 static const struct platform_device_id s2mps11_pmic_id[] = {
1018 	{ "s2mps11-pmic", S2MPS11X},
1019 	{ "s2mps14-pmic", S2MPS14X},
1020 	{ "s2mpu02-pmic", S2MPU02},
1021 	{ },
1022 };
1023 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1024 
1025 static struct platform_driver s2mps11_pmic_driver = {
1026 	.driver = {
1027 		.name = "s2mps11-pmic",
1028 		.owner = THIS_MODULE,
1029 	},
1030 	.probe = s2mps11_pmic_probe,
1031 	.id_table = s2mps11_pmic_id,
1032 };
1033 
1034 static int __init s2mps11_pmic_init(void)
1035 {
1036 	return platform_driver_register(&s2mps11_pmic_driver);
1037 }
1038 subsys_initcall(s2mps11_pmic_init);
1039 
1040 static void __exit s2mps11_pmic_exit(void)
1041 {
1042 	platform_driver_unregister(&s2mps11_pmic_driver);
1043 }
1044 module_exit(s2mps11_pmic_exit);
1045 
1046 /* Module information */
1047 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
1048 MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPU02 Regulator Driver");
1049 MODULE_LICENSE("GPL");
1050