xref: /openbmc/linux/drivers/regulator/s2mps11.c (revision afb46f79)
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/mfd/samsung/core.h>
31 #include <linux/mfd/samsung/s2mps11.h>
32 #include <linux/mfd/samsung/s2mps14.h>
33 
34 struct s2mps11_info {
35 	unsigned int rdev_num;
36 	int ramp_delay2;
37 	int ramp_delay34;
38 	int ramp_delay5;
39 	int ramp_delay16;
40 	int ramp_delay7810;
41 	int ramp_delay9;
42 	/*
43 	 * One bit for each S2MPS14 regulator whether the suspend mode
44 	 * was enabled.
45 	 */
46 	unsigned int s2mps14_suspend_state:30;
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 	ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
206 				 1 << enable_shift, 1 << enable_shift);
207 	if (ret) {
208 		dev_err(&rdev->dev, "failed to enable ramp rate\n");
209 		return ret;
210 	}
211 
212 	ramp_val = get_ramp_delay(ramp_delay);
213 
214 	return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
215 				  ramp_val << ramp_shift);
216 
217 ramp_disable:
218 	return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
219 				  1 << enable_shift, 0);
220 }
221 
222 static struct regulator_ops s2mps11_ldo_ops = {
223 	.list_voltage		= regulator_list_voltage_linear,
224 	.map_voltage		= regulator_map_voltage_linear,
225 	.is_enabled		= regulator_is_enabled_regmap,
226 	.enable			= regulator_enable_regmap,
227 	.disable		= regulator_disable_regmap,
228 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
229 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
230 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
231 };
232 
233 static struct regulator_ops s2mps11_buck_ops = {
234 	.list_voltage		= regulator_list_voltage_linear,
235 	.map_voltage		= regulator_map_voltage_linear,
236 	.is_enabled		= regulator_is_enabled_regmap,
237 	.enable			= regulator_enable_regmap,
238 	.disable		= regulator_disable_regmap,
239 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
240 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
241 	.set_voltage_time_sel	= s2mps11_regulator_set_voltage_time_sel,
242 	.set_ramp_delay		= s2mps11_set_ramp_delay,
243 };
244 
245 #define regulator_desc_s2mps11_ldo1(num)	{		\
246 	.name		= "LDO"#num,			\
247 	.id		= S2MPS11_LDO##num,		\
248 	.ops		= &s2mps11_ldo_ops,		\
249 	.type		= REGULATOR_VOLTAGE,		\
250 	.owner		= THIS_MODULE,			\
251 	.min_uV		= S2MPS11_LDO_MIN,		\
252 	.uV_step	= S2MPS11_LDO_STEP1,		\
253 	.n_voltages	= S2MPS11_LDO_N_VOLTAGES,	\
254 	.vsel_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
255 	.vsel_mask	= S2MPS11_LDO_VSEL_MASK,	\
256 	.enable_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
257 	.enable_mask	= S2MPS11_ENABLE_MASK		\
258 }
259 #define regulator_desc_s2mps11_ldo2(num) {		\
260 	.name		= "LDO"#num,			\
261 	.id		= S2MPS11_LDO##num,		\
262 	.ops		= &s2mps11_ldo_ops,		\
263 	.type		= REGULATOR_VOLTAGE,		\
264 	.owner		= THIS_MODULE,			\
265 	.min_uV		= S2MPS11_LDO_MIN,		\
266 	.uV_step	= S2MPS11_LDO_STEP2,		\
267 	.n_voltages	= S2MPS11_LDO_N_VOLTAGES,	\
268 	.vsel_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
269 	.vsel_mask	= S2MPS11_LDO_VSEL_MASK,	\
270 	.enable_reg	= S2MPS11_REG_L1CTRL + num - 1,	\
271 	.enable_mask	= S2MPS11_ENABLE_MASK		\
272 }
273 
274 #define regulator_desc_s2mps11_buck1_4(num) {			\
275 	.name		= "BUCK"#num,				\
276 	.id		= S2MPS11_BUCK##num,			\
277 	.ops		= &s2mps11_buck_ops,			\
278 	.type		= REGULATOR_VOLTAGE,			\
279 	.owner		= THIS_MODULE,				\
280 	.min_uV		= S2MPS11_BUCK_MIN1,			\
281 	.uV_step	= S2MPS11_BUCK_STEP1,			\
282 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
283 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
284 	.vsel_reg	= S2MPS11_REG_B1CTRL2 + (num - 1) * 2,	\
285 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
286 	.enable_reg	= S2MPS11_REG_B1CTRL1 + (num - 1) * 2,	\
287 	.enable_mask	= S2MPS11_ENABLE_MASK			\
288 }
289 
290 #define regulator_desc_s2mps11_buck5 {				\
291 	.name		= "BUCK5",				\
292 	.id		= S2MPS11_BUCK5,			\
293 	.ops		= &s2mps11_buck_ops,			\
294 	.type		= REGULATOR_VOLTAGE,			\
295 	.owner		= THIS_MODULE,				\
296 	.min_uV		= S2MPS11_BUCK_MIN1,			\
297 	.uV_step	= S2MPS11_BUCK_STEP1,			\
298 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
299 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
300 	.vsel_reg	= S2MPS11_REG_B5CTRL2,			\
301 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
302 	.enable_reg	= S2MPS11_REG_B5CTRL1,			\
303 	.enable_mask	= S2MPS11_ENABLE_MASK			\
304 }
305 
306 #define regulator_desc_s2mps11_buck6_8(num) {			\
307 	.name		= "BUCK"#num,				\
308 	.id		= S2MPS11_BUCK##num,			\
309 	.ops		= &s2mps11_buck_ops,			\
310 	.type		= REGULATOR_VOLTAGE,			\
311 	.owner		= THIS_MODULE,				\
312 	.min_uV		= S2MPS11_BUCK_MIN1,			\
313 	.uV_step	= S2MPS11_BUCK_STEP1,			\
314 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
315 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
316 	.vsel_reg	= S2MPS11_REG_B6CTRL2 + (num - 6) * 2,	\
317 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
318 	.enable_reg	= S2MPS11_REG_B6CTRL1 + (num - 6) * 2,	\
319 	.enable_mask	= S2MPS11_ENABLE_MASK			\
320 }
321 
322 #define regulator_desc_s2mps11_buck9 {				\
323 	.name		= "BUCK9",				\
324 	.id		= S2MPS11_BUCK9,			\
325 	.ops		= &s2mps11_buck_ops,			\
326 	.type		= REGULATOR_VOLTAGE,			\
327 	.owner		= THIS_MODULE,				\
328 	.min_uV		= S2MPS11_BUCK_MIN3,			\
329 	.uV_step	= S2MPS11_BUCK_STEP3,			\
330 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
331 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
332 	.vsel_reg	= S2MPS11_REG_B9CTRL2,			\
333 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
334 	.enable_reg	= S2MPS11_REG_B9CTRL1,			\
335 	.enable_mask	= S2MPS11_ENABLE_MASK			\
336 }
337 
338 #define regulator_desc_s2mps11_buck10 {				\
339 	.name		= "BUCK10",				\
340 	.id		= S2MPS11_BUCK10,			\
341 	.ops		= &s2mps11_buck_ops,			\
342 	.type		= REGULATOR_VOLTAGE,			\
343 	.owner		= THIS_MODULE,				\
344 	.min_uV		= S2MPS11_BUCK_MIN2,			\
345 	.uV_step	= S2MPS11_BUCK_STEP2,			\
346 	.n_voltages	= S2MPS11_BUCK_N_VOLTAGES,		\
347 	.ramp_delay	= S2MPS11_RAMP_DELAY,			\
348 	.vsel_reg	= S2MPS11_REG_B10CTRL2,			\
349 	.vsel_mask	= S2MPS11_BUCK_VSEL_MASK,		\
350 	.enable_reg	= S2MPS11_REG_B10CTRL1,			\
351 	.enable_mask	= S2MPS11_ENABLE_MASK			\
352 }
353 
354 static const struct regulator_desc s2mps11_regulators[] = {
355 	regulator_desc_s2mps11_ldo2(1),
356 	regulator_desc_s2mps11_ldo1(2),
357 	regulator_desc_s2mps11_ldo1(3),
358 	regulator_desc_s2mps11_ldo1(4),
359 	regulator_desc_s2mps11_ldo1(5),
360 	regulator_desc_s2mps11_ldo2(6),
361 	regulator_desc_s2mps11_ldo1(7),
362 	regulator_desc_s2mps11_ldo1(8),
363 	regulator_desc_s2mps11_ldo1(9),
364 	regulator_desc_s2mps11_ldo1(10),
365 	regulator_desc_s2mps11_ldo2(11),
366 	regulator_desc_s2mps11_ldo1(12),
367 	regulator_desc_s2mps11_ldo1(13),
368 	regulator_desc_s2mps11_ldo1(14),
369 	regulator_desc_s2mps11_ldo1(15),
370 	regulator_desc_s2mps11_ldo1(16),
371 	regulator_desc_s2mps11_ldo1(17),
372 	regulator_desc_s2mps11_ldo1(18),
373 	regulator_desc_s2mps11_ldo1(19),
374 	regulator_desc_s2mps11_ldo1(20),
375 	regulator_desc_s2mps11_ldo1(21),
376 	regulator_desc_s2mps11_ldo2(22),
377 	regulator_desc_s2mps11_ldo2(23),
378 	regulator_desc_s2mps11_ldo1(24),
379 	regulator_desc_s2mps11_ldo1(25),
380 	regulator_desc_s2mps11_ldo1(26),
381 	regulator_desc_s2mps11_ldo2(27),
382 	regulator_desc_s2mps11_ldo1(28),
383 	regulator_desc_s2mps11_ldo1(29),
384 	regulator_desc_s2mps11_ldo1(30),
385 	regulator_desc_s2mps11_ldo1(31),
386 	regulator_desc_s2mps11_ldo1(32),
387 	regulator_desc_s2mps11_ldo1(33),
388 	regulator_desc_s2mps11_ldo1(34),
389 	regulator_desc_s2mps11_ldo1(35),
390 	regulator_desc_s2mps11_ldo1(36),
391 	regulator_desc_s2mps11_ldo1(37),
392 	regulator_desc_s2mps11_ldo1(38),
393 	regulator_desc_s2mps11_buck1_4(1),
394 	regulator_desc_s2mps11_buck1_4(2),
395 	regulator_desc_s2mps11_buck1_4(3),
396 	regulator_desc_s2mps11_buck1_4(4),
397 	regulator_desc_s2mps11_buck5,
398 	regulator_desc_s2mps11_buck6_8(6),
399 	regulator_desc_s2mps11_buck6_8(7),
400 	regulator_desc_s2mps11_buck6_8(8),
401 	regulator_desc_s2mps11_buck9,
402 	regulator_desc_s2mps11_buck10,
403 };
404 
405 static int s2mps14_regulator_enable(struct regulator_dev *rdev)
406 {
407 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
408 	unsigned int val;
409 
410 	if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
411 		val = S2MPS14_ENABLE_SUSPEND;
412 	else
413 		val = rdev->desc->enable_mask;
414 
415 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
416 			rdev->desc->enable_mask, val);
417 }
418 
419 static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
420 {
421 	int ret;
422 	unsigned int val;
423 	struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
424 
425 	/* LDO3 should be always on and does not support suspend mode */
426 	if (rdev_get_id(rdev) == S2MPS14_LDO3)
427 		return 0;
428 
429 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
430 	if (ret < 0)
431 		return ret;
432 
433 	s2mps11->s2mps14_suspend_state |= (1 << rdev_get_id(rdev));
434 	/*
435 	 * Don't enable suspend mode if regulator is already disabled because
436 	 * this would effectively for a short time turn on the regulator after
437 	 * resuming.
438 	 * However we still want to toggle the suspend_state bit for regulator
439 	 * in case if it got enabled before suspending the system.
440 	 */
441 	if (!(val & rdev->desc->enable_mask))
442 		return 0;
443 
444 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
445 			rdev->desc->enable_mask, S2MPS14_ENABLE_SUSPEND);
446 }
447 
448 static struct regulator_ops s2mps14_reg_ops = {
449 	.list_voltage		= regulator_list_voltage_linear,
450 	.map_voltage		= regulator_map_voltage_linear,
451 	.is_enabled		= regulator_is_enabled_regmap,
452 	.enable			= s2mps14_regulator_enable,
453 	.disable		= regulator_disable_regmap,
454 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
455 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
456 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
457 	.set_suspend_disable	= s2mps14_regulator_set_suspend_disable,
458 };
459 
460 #define regulator_desc_s2mps14_ldo1(num) {		\
461 	.name		= "LDO"#num,			\
462 	.id		= S2MPS14_LDO##num,		\
463 	.ops		= &s2mps14_reg_ops,		\
464 	.type		= REGULATOR_VOLTAGE,		\
465 	.owner		= THIS_MODULE,			\
466 	.min_uV		= S2MPS14_LDO_MIN_800MV,	\
467 	.uV_step	= S2MPS14_LDO_STEP_25MV,	\
468 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,	\
469 	.vsel_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
470 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,	\
471 	.enable_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
472 	.enable_mask	= S2MPS14_ENABLE_MASK		\
473 }
474 #define regulator_desc_s2mps14_ldo2(num) {		\
475 	.name		= "LDO"#num,			\
476 	.id		= S2MPS14_LDO##num,		\
477 	.ops		= &s2mps14_reg_ops,		\
478 	.type		= REGULATOR_VOLTAGE,		\
479 	.owner		= THIS_MODULE,			\
480 	.min_uV		= S2MPS14_LDO_MIN_1800MV,	\
481 	.uV_step	= S2MPS14_LDO_STEP_25MV,	\
482 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,	\
483 	.vsel_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
484 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,	\
485 	.enable_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
486 	.enable_mask	= S2MPS14_ENABLE_MASK		\
487 }
488 #define regulator_desc_s2mps14_ldo3(num) {		\
489 	.name		= "LDO"#num,			\
490 	.id		= S2MPS14_LDO##num,		\
491 	.ops		= &s2mps14_reg_ops,		\
492 	.type		= REGULATOR_VOLTAGE,		\
493 	.owner		= THIS_MODULE,			\
494 	.min_uV		= S2MPS14_LDO_MIN_800MV,	\
495 	.uV_step	= S2MPS14_LDO_STEP_12_5MV,	\
496 	.n_voltages	= S2MPS14_LDO_N_VOLTAGES,	\
497 	.vsel_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
498 	.vsel_mask	= S2MPS14_LDO_VSEL_MASK,	\
499 	.enable_reg	= S2MPS14_REG_L1CTRL + num - 1,	\
500 	.enable_mask	= S2MPS14_ENABLE_MASK		\
501 }
502 #define regulator_desc_s2mps14_buck1235(num) {			\
503 	.name		= "BUCK"#num,				\
504 	.id		= S2MPS14_BUCK##num,			\
505 	.ops		= &s2mps14_reg_ops,			\
506 	.type		= REGULATOR_VOLTAGE,			\
507 	.owner		= THIS_MODULE,				\
508 	.min_uV		= S2MPS14_BUCK1235_MIN_600MV,		\
509 	.uV_step	= S2MPS14_BUCK1235_STEP_6_25MV,		\
510 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
511 	.linear_min_sel = S2MPS14_BUCK1235_START_SEL,		\
512 	.ramp_delay	= S2MPS14_BUCK_RAMP_DELAY,		\
513 	.vsel_reg	= S2MPS14_REG_B1CTRL2 + (num - 1) * 2,	\
514 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
515 	.enable_reg	= S2MPS14_REG_B1CTRL1 + (num - 1) * 2,	\
516 	.enable_mask	= S2MPS14_ENABLE_MASK			\
517 }
518 #define regulator_desc_s2mps14_buck4(num) {			\
519 	.name		= "BUCK"#num,				\
520 	.id		= S2MPS14_BUCK##num,			\
521 	.ops		= &s2mps14_reg_ops,			\
522 	.type		= REGULATOR_VOLTAGE,			\
523 	.owner		= THIS_MODULE,				\
524 	.min_uV		= S2MPS14_BUCK4_MIN_1400MV,		\
525 	.uV_step	= S2MPS14_BUCK4_STEP_12_5MV,		\
526 	.n_voltages	= S2MPS14_BUCK_N_VOLTAGES,		\
527 	.linear_min_sel = S2MPS14_BUCK4_START_SEL,		\
528 	.ramp_delay	= S2MPS14_BUCK_RAMP_DELAY,		\
529 	.vsel_reg	= S2MPS14_REG_B1CTRL2 + (num - 1) * 2,	\
530 	.vsel_mask	= S2MPS14_BUCK_VSEL_MASK,		\
531 	.enable_reg	= S2MPS14_REG_B1CTRL1 + (num - 1) * 2,	\
532 	.enable_mask	= S2MPS14_ENABLE_MASK			\
533 }
534 
535 static const struct regulator_desc s2mps14_regulators[] = {
536 	regulator_desc_s2mps14_ldo3(1),
537 	regulator_desc_s2mps14_ldo3(2),
538 	regulator_desc_s2mps14_ldo1(3),
539 	regulator_desc_s2mps14_ldo1(4),
540 	regulator_desc_s2mps14_ldo3(5),
541 	regulator_desc_s2mps14_ldo3(6),
542 	regulator_desc_s2mps14_ldo1(7),
543 	regulator_desc_s2mps14_ldo2(8),
544 	regulator_desc_s2mps14_ldo3(9),
545 	regulator_desc_s2mps14_ldo3(10),
546 	regulator_desc_s2mps14_ldo1(11),
547 	regulator_desc_s2mps14_ldo2(12),
548 	regulator_desc_s2mps14_ldo2(13),
549 	regulator_desc_s2mps14_ldo2(14),
550 	regulator_desc_s2mps14_ldo2(15),
551 	regulator_desc_s2mps14_ldo2(16),
552 	regulator_desc_s2mps14_ldo2(17),
553 	regulator_desc_s2mps14_ldo2(18),
554 	regulator_desc_s2mps14_ldo1(19),
555 	regulator_desc_s2mps14_ldo1(20),
556 	regulator_desc_s2mps14_ldo1(21),
557 	regulator_desc_s2mps14_ldo3(22),
558 	regulator_desc_s2mps14_ldo1(23),
559 	regulator_desc_s2mps14_ldo2(24),
560 	regulator_desc_s2mps14_ldo2(25),
561 	regulator_desc_s2mps14_buck1235(1),
562 	regulator_desc_s2mps14_buck1235(2),
563 	regulator_desc_s2mps14_buck1235(3),
564 	regulator_desc_s2mps14_buck4(4),
565 	regulator_desc_s2mps14_buck1235(5),
566 };
567 
568 static int s2mps11_pmic_probe(struct platform_device *pdev)
569 {
570 	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
571 	struct sec_platform_data *pdata = iodev->pdata;
572 	struct of_regulator_match *rdata = NULL;
573 	struct device_node *reg_np = NULL;
574 	struct regulator_config config = { };
575 	struct s2mps11_info *s2mps11;
576 	int i, ret = 0;
577 	const struct regulator_desc *regulators;
578 	enum sec_device_type dev_type;
579 
580 	s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
581 				GFP_KERNEL);
582 	if (!s2mps11)
583 		return -ENOMEM;
584 
585 	dev_type = platform_get_device_id(pdev)->driver_data;
586 	switch (dev_type) {
587 	case S2MPS11X:
588 		s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
589 		regulators = s2mps11_regulators;
590 		break;
591 	case S2MPS14X:
592 		s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
593 		regulators = s2mps14_regulators;
594 		break;
595 	default:
596 		dev_err(&pdev->dev, "Invalid device type: %u\n", dev_type);
597 		return -EINVAL;
598 	};
599 
600 	if (!iodev->dev->of_node) {
601 		if (pdata) {
602 			goto common_reg;
603 		} else {
604 			dev_err(pdev->dev.parent,
605 				"Platform data or DT node not supplied\n");
606 			return -ENODEV;
607 		}
608 	}
609 
610 	rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
611 	if (!rdata)
612 		return -ENOMEM;
613 
614 	for (i = 0; i < s2mps11->rdev_num; i++)
615 		rdata[i].name = regulators[i].name;
616 
617 	reg_np = of_get_child_by_name(iodev->dev->of_node, "regulators");
618 	if (!reg_np) {
619 		dev_err(&pdev->dev, "could not find regulators sub-node\n");
620 		ret = -EINVAL;
621 		goto out;
622 	}
623 
624 	of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
625 	of_node_put(reg_np);
626 
627 common_reg:
628 	platform_set_drvdata(pdev, s2mps11);
629 
630 	config.dev = &pdev->dev;
631 	config.regmap = iodev->regmap_pmic;
632 	config.driver_data = s2mps11;
633 	for (i = 0; i < s2mps11->rdev_num; i++) {
634 		struct regulator_dev *regulator;
635 
636 		if (!reg_np) {
637 			config.init_data = pdata->regulators[i].initdata;
638 			config.of_node = pdata->regulators[i].reg_node;
639 		} else {
640 			config.init_data = rdata[i].init_data;
641 			config.of_node = rdata[i].of_node;
642 		}
643 
644 		regulator = devm_regulator_register(&pdev->dev,
645 						&regulators[i], &config);
646 		if (IS_ERR(regulator)) {
647 			ret = PTR_ERR(regulator);
648 			dev_err(&pdev->dev, "regulator init failed for %d\n",
649 				i);
650 			goto out;
651 		}
652 	}
653 
654 out:
655 	kfree(rdata);
656 
657 	return ret;
658 }
659 
660 static const struct platform_device_id s2mps11_pmic_id[] = {
661 	{ "s2mps11-pmic", S2MPS11X},
662 	{ "s2mps14-pmic", S2MPS14X},
663 	{ },
664 };
665 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
666 
667 static struct platform_driver s2mps11_pmic_driver = {
668 	.driver = {
669 		.name = "s2mps11-pmic",
670 		.owner = THIS_MODULE,
671 	},
672 	.probe = s2mps11_pmic_probe,
673 	.id_table = s2mps11_pmic_id,
674 };
675 
676 static int __init s2mps11_pmic_init(void)
677 {
678 	return platform_driver_register(&s2mps11_pmic_driver);
679 }
680 subsys_initcall(s2mps11_pmic_init);
681 
682 static void __exit s2mps11_pmic_exit(void)
683 {
684 	platform_driver_unregister(&s2mps11_pmic_driver);
685 }
686 module_exit(s2mps11_pmic_exit);
687 
688 /* Module information */
689 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
690 MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14 Regulator Driver");
691 MODULE_LICENSE("GPL");
692