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
get_ramp_delay(int ramp_delay)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
s2mps11_regulator_set_voltage_time_sel(struct regulator_dev * rdev,unsigned int old_selector,unsigned int new_selector)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
s2mps11_set_ramp_delay(struct regulator_dev * rdev,int ramp_delay)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_ldo(num, step) { \
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 = MIN_800_MV, \
265 .uV_step = step, \
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
273 #define regulator_desc_s2mps11_buck1_4(num) { \
274 .name = "BUCK"#num, \
275 .id = S2MPS11_BUCK##num, \
276 .ops = &s2mps11_buck_ops, \
277 .type = REGULATOR_VOLTAGE, \
278 .owner = THIS_MODULE, \
279 .min_uV = MIN_600_MV, \
280 .uV_step = STEP_6_25_MV, \
281 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
282 .ramp_delay = S2MPS11_RAMP_DELAY, \
283 .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
284 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
285 .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
286 .enable_mask = S2MPS11_ENABLE_MASK \
287 }
288
289 #define regulator_desc_s2mps11_buck5 { \
290 .name = "BUCK5", \
291 .id = S2MPS11_BUCK5, \
292 .ops = &s2mps11_buck_ops, \
293 .type = REGULATOR_VOLTAGE, \
294 .owner = THIS_MODULE, \
295 .min_uV = MIN_600_MV, \
296 .uV_step = STEP_6_25_MV, \
297 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
298 .ramp_delay = S2MPS11_RAMP_DELAY, \
299 .vsel_reg = S2MPS11_REG_B5CTRL2, \
300 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
301 .enable_reg = S2MPS11_REG_B5CTRL1, \
302 .enable_mask = S2MPS11_ENABLE_MASK \
303 }
304
305 #define regulator_desc_s2mps11_buck67810(num, min, step) { \
306 .name = "BUCK"#num, \
307 .id = S2MPS11_BUCK##num, \
308 .ops = &s2mps11_buck_ops, \
309 .type = REGULATOR_VOLTAGE, \
310 .owner = THIS_MODULE, \
311 .min_uV = min, \
312 .uV_step = step, \
313 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
314 .ramp_delay = S2MPS11_RAMP_DELAY, \
315 .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
316 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
317 .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
318 .enable_mask = S2MPS11_ENABLE_MASK \
319 }
320
321 #define regulator_desc_s2mps11_buck9 { \
322 .name = "BUCK9", \
323 .id = S2MPS11_BUCK9, \
324 .ops = &s2mps11_buck_ops, \
325 .type = REGULATOR_VOLTAGE, \
326 .owner = THIS_MODULE, \
327 .min_uV = MIN_3000_MV, \
328 .uV_step = STEP_25_MV, \
329 .n_voltages = S2MPS11_BUCK9_N_VOLTAGES, \
330 .ramp_delay = S2MPS11_RAMP_DELAY, \
331 .vsel_reg = S2MPS11_REG_B9CTRL2, \
332 .vsel_mask = S2MPS11_BUCK9_VSEL_MASK, \
333 .enable_reg = S2MPS11_REG_B9CTRL1, \
334 .enable_mask = S2MPS11_ENABLE_MASK \
335 }
336
337 static const struct regulator_desc s2mps11_regulators[] = {
338 regulator_desc_s2mps11_ldo(1, STEP_25_MV),
339 regulator_desc_s2mps11_ldo(2, STEP_50_MV),
340 regulator_desc_s2mps11_ldo(3, STEP_50_MV),
341 regulator_desc_s2mps11_ldo(4, STEP_50_MV),
342 regulator_desc_s2mps11_ldo(5, STEP_50_MV),
343 regulator_desc_s2mps11_ldo(6, STEP_25_MV),
344 regulator_desc_s2mps11_ldo(7, STEP_50_MV),
345 regulator_desc_s2mps11_ldo(8, STEP_50_MV),
346 regulator_desc_s2mps11_ldo(9, STEP_50_MV),
347 regulator_desc_s2mps11_ldo(10, STEP_50_MV),
348 regulator_desc_s2mps11_ldo(11, STEP_25_MV),
349 regulator_desc_s2mps11_ldo(12, STEP_50_MV),
350 regulator_desc_s2mps11_ldo(13, STEP_50_MV),
351 regulator_desc_s2mps11_ldo(14, STEP_50_MV),
352 regulator_desc_s2mps11_ldo(15, STEP_50_MV),
353 regulator_desc_s2mps11_ldo(16, STEP_50_MV),
354 regulator_desc_s2mps11_ldo(17, STEP_50_MV),
355 regulator_desc_s2mps11_ldo(18, STEP_50_MV),
356 regulator_desc_s2mps11_ldo(19, STEP_50_MV),
357 regulator_desc_s2mps11_ldo(20, STEP_50_MV),
358 regulator_desc_s2mps11_ldo(21, STEP_50_MV),
359 regulator_desc_s2mps11_ldo(22, STEP_25_MV),
360 regulator_desc_s2mps11_ldo(23, STEP_25_MV),
361 regulator_desc_s2mps11_ldo(24, STEP_50_MV),
362 regulator_desc_s2mps11_ldo(25, STEP_50_MV),
363 regulator_desc_s2mps11_ldo(26, STEP_50_MV),
364 regulator_desc_s2mps11_ldo(27, STEP_25_MV),
365 regulator_desc_s2mps11_ldo(28, STEP_50_MV),
366 regulator_desc_s2mps11_ldo(29, STEP_50_MV),
367 regulator_desc_s2mps11_ldo(30, STEP_50_MV),
368 regulator_desc_s2mps11_ldo(31, STEP_50_MV),
369 regulator_desc_s2mps11_ldo(32, STEP_50_MV),
370 regulator_desc_s2mps11_ldo(33, STEP_50_MV),
371 regulator_desc_s2mps11_ldo(34, STEP_50_MV),
372 regulator_desc_s2mps11_ldo(35, STEP_50_MV),
373 regulator_desc_s2mps11_ldo(36, STEP_50_MV),
374 regulator_desc_s2mps11_ldo(37, STEP_50_MV),
375 regulator_desc_s2mps11_ldo(38, STEP_50_MV),
376 regulator_desc_s2mps11_buck1_4(1),
377 regulator_desc_s2mps11_buck1_4(2),
378 regulator_desc_s2mps11_buck1_4(3),
379 regulator_desc_s2mps11_buck1_4(4),
380 regulator_desc_s2mps11_buck5,
381 regulator_desc_s2mps11_buck67810(6, MIN_600_MV, STEP_6_25_MV),
382 regulator_desc_s2mps11_buck67810(7, MIN_600_MV, STEP_6_25_MV),
383 regulator_desc_s2mps11_buck67810(8, MIN_600_MV, STEP_6_25_MV),
384 regulator_desc_s2mps11_buck9,
385 regulator_desc_s2mps11_buck67810(10, MIN_750_MV, STEP_12_5_MV),
386 };
387
s2mps14_regulator_enable(struct regulator_dev * rdev)388 static int s2mps14_regulator_enable(struct regulator_dev *rdev)
389 {
390 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
391 unsigned int val;
392
393 switch (s2mps11->dev_type) {
394 case S2MPS14X:
395 if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
396 val = S2MPS14_ENABLE_SUSPEND;
397 else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
398 val = S2MPS14_ENABLE_EXT_CONTROL;
399 else
400 val = rdev->desc->enable_mask;
401 break;
402 case S2MPU02:
403 if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
404 val = S2MPU02_ENABLE_SUSPEND;
405 else
406 val = rdev->desc->enable_mask;
407 break;
408 default:
409 return -EINVAL;
410 };
411
412 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
413 rdev->desc->enable_mask, val);
414 }
415
s2mps14_regulator_set_suspend_disable(struct regulator_dev * rdev)416 static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
417 {
418 int ret;
419 unsigned int val, state;
420 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
421 int rdev_id = rdev_get_id(rdev);
422
423 /* Below LDO should be always on or does not support suspend mode. */
424 switch (s2mps11->dev_type) {
425 case S2MPS14X:
426 switch (rdev_id) {
427 case S2MPS14_LDO3:
428 return 0;
429 default:
430 state = S2MPS14_ENABLE_SUSPEND;
431 break;
432 };
433 break;
434 case S2MPU02:
435 switch (rdev_id) {
436 case S2MPU02_LDO13:
437 case S2MPU02_LDO14:
438 case S2MPU02_LDO15:
439 case S2MPU02_LDO17:
440 case S2MPU02_BUCK7:
441 state = S2MPU02_DISABLE_SUSPEND;
442 break;
443 default:
444 state = S2MPU02_ENABLE_SUSPEND;
445 break;
446 };
447 break;
448 default:
449 return -EINVAL;
450 };
451
452 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
453 if (ret < 0)
454 return ret;
455
456 s2mps11->s2mps14_suspend_state |= (1 << rdev_get_id(rdev));
457 /*
458 * Don't enable suspend mode if regulator is already disabled because
459 * this would effectively for a short time turn on the regulator after
460 * resuming.
461 * However we still want to toggle the suspend_state bit for regulator
462 * in case if it got enabled before suspending the system.
463 */
464 if (!(val & rdev->desc->enable_mask))
465 return 0;
466
467 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
468 rdev->desc->enable_mask, state);
469 }
470
471 static struct regulator_ops s2mps14_reg_ops = {
472 .list_voltage = regulator_list_voltage_linear,
473 .map_voltage = regulator_map_voltage_linear,
474 .is_enabled = regulator_is_enabled_regmap,
475 .enable = s2mps14_regulator_enable,
476 .disable = regulator_disable_regmap,
477 .get_voltage_sel = regulator_get_voltage_sel_regmap,
478 .set_voltage_sel = regulator_set_voltage_sel_regmap,
479 .set_voltage_time_sel = regulator_set_voltage_time_sel,
480 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
481 };
482
483 #define regulator_desc_s2mps14_ldo(num, min, step) { \
484 .name = "LDO"#num, \
485 .id = S2MPS14_LDO##num, \
486 .ops = &s2mps14_reg_ops, \
487 .type = REGULATOR_VOLTAGE, \
488 .owner = THIS_MODULE, \
489 .min_uV = min, \
490 .uV_step = step, \
491 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
492 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
493 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
494 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
495 .enable_mask = S2MPS14_ENABLE_MASK \
496 }
497
498 #define regulator_desc_s2mps14_buck(num, min, step, min_sel) { \
499 .name = "BUCK"#num, \
500 .id = S2MPS14_BUCK##num, \
501 .ops = &s2mps14_reg_ops, \
502 .type = REGULATOR_VOLTAGE, \
503 .owner = THIS_MODULE, \
504 .min_uV = min, \
505 .uV_step = step, \
506 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
507 .linear_min_sel = min_sel, \
508 .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
509 .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
510 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
511 .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
512 .enable_mask = S2MPS14_ENABLE_MASK \
513 }
514
515 static const struct regulator_desc s2mps14_regulators[] = {
516 regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
517 regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
518 regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
519 regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
520 regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
521 regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
522 regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
523 regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
524 regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
525 regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
526 regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
527 regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
528 regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
529 regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
530 regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
531 regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
532 regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
533 regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
534 regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
535 regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
536 regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
537 regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
538 regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
539 regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
540 regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
541 regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV,
542 S2MPS14_BUCK1235_START_SEL),
543 regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV,
544 S2MPS14_BUCK1235_START_SEL),
545 regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV,
546 S2MPS14_BUCK1235_START_SEL),
547 regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV,
548 S2MPS14_BUCK4_START_SEL),
549 regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV,
550 S2MPS14_BUCK1235_START_SEL),
551 };
552
s2mps14_pmic_enable_ext_control(struct s2mps11_info * s2mps11,struct regulator_dev * rdev)553 static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
554 struct regulator_dev *rdev)
555 {
556 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
557 rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
558 }
559
s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device * pdev,struct of_regulator_match * rdata,struct s2mps11_info * s2mps11)560 static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
561 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
562 {
563 int *gpio = s2mps11->ext_control_gpio;
564 unsigned int i;
565 unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
566 S2MPS14_LDO12 };
567
568 for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
569 unsigned int reg = valid_regulators[i];
570
571 if (!rdata[reg].init_data || !rdata[reg].of_node)
572 continue;
573
574 gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
575 "samsung,ext-control-gpios", 0);
576 if (gpio_is_valid(gpio[reg]))
577 dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
578 gpio[reg], reg, rdata[reg].name);
579 }
580 }
581
s2mps11_pmic_dt_parse(struct platform_device * pdev,struct of_regulator_match * rdata,struct s2mps11_info * s2mps11)582 static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
583 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
584 {
585 struct device_node *reg_np;
586
587 reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
588 if (!reg_np) {
589 dev_err(&pdev->dev, "could not find regulators sub-node\n");
590 return -EINVAL;
591 }
592
593 of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
594 if (s2mps11->dev_type == S2MPS14X)
595 s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
596
597 of_node_put(reg_np);
598
599 return 0;
600 }
601
s2mpu02_set_ramp_delay(struct regulator_dev * rdev,int ramp_delay)602 static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
603 {
604 unsigned int ramp_val, ramp_shift, ramp_reg;
605
606 switch (rdev_get_id(rdev)) {
607 case S2MPU02_BUCK1:
608 ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
609 break;
610 case S2MPU02_BUCK2:
611 ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
612 break;
613 case S2MPU02_BUCK3:
614 ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
615 break;
616 case S2MPU02_BUCK4:
617 ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
618 break;
619 default:
620 return 0;
621 }
622 ramp_reg = S2MPU02_REG_RAMP1;
623 ramp_val = get_ramp_delay(ramp_delay);
624
625 return regmap_update_bits(rdev->regmap, ramp_reg,
626 S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
627 ramp_val << ramp_shift);
628 }
629
630 static struct regulator_ops s2mpu02_ldo_ops = {
631 .list_voltage = regulator_list_voltage_linear,
632 .map_voltage = regulator_map_voltage_linear,
633 .is_enabled = regulator_is_enabled_regmap,
634 .enable = s2mps14_regulator_enable,
635 .disable = regulator_disable_regmap,
636 .get_voltage_sel = regulator_get_voltage_sel_regmap,
637 .set_voltage_sel = regulator_set_voltage_sel_regmap,
638 .set_voltage_time_sel = regulator_set_voltage_time_sel,
639 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
640 };
641
642 static struct regulator_ops s2mpu02_buck_ops = {
643 .list_voltage = regulator_list_voltage_linear,
644 .map_voltage = regulator_map_voltage_linear,
645 .is_enabled = regulator_is_enabled_regmap,
646 .enable = s2mps14_regulator_enable,
647 .disable = regulator_disable_regmap,
648 .get_voltage_sel = regulator_get_voltage_sel_regmap,
649 .set_voltage_sel = regulator_set_voltage_sel_regmap,
650 .set_voltage_time_sel = regulator_set_voltage_time_sel,
651 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
652 .set_ramp_delay = s2mpu02_set_ramp_delay,
653 };
654
655 #define regulator_desc_s2mpu02_ldo1(num) { \
656 .name = "LDO"#num, \
657 .id = S2MPU02_LDO##num, \
658 .ops = &s2mpu02_ldo_ops, \
659 .type = REGULATOR_VOLTAGE, \
660 .owner = THIS_MODULE, \
661 .min_uV = S2MPU02_LDO_MIN_900MV, \
662 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
663 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
664 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
665 .vsel_reg = S2MPU02_REG_L1CTRL, \
666 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
667 .enable_reg = S2MPU02_REG_L1CTRL, \
668 .enable_mask = S2MPU02_ENABLE_MASK \
669 }
670 #define regulator_desc_s2mpu02_ldo2(num) { \
671 .name = "LDO"#num, \
672 .id = S2MPU02_LDO##num, \
673 .ops = &s2mpu02_ldo_ops, \
674 .type = REGULATOR_VOLTAGE, \
675 .owner = THIS_MODULE, \
676 .min_uV = S2MPU02_LDO_MIN_1050MV, \
677 .uV_step = S2MPU02_LDO_STEP_25MV, \
678 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
679 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
680 .vsel_reg = S2MPU02_REG_L2CTRL1, \
681 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
682 .enable_reg = S2MPU02_REG_L2CTRL1, \
683 .enable_mask = S2MPU02_ENABLE_MASK \
684 }
685 #define regulator_desc_s2mpu02_ldo3(num) { \
686 .name = "LDO"#num, \
687 .id = S2MPU02_LDO##num, \
688 .ops = &s2mpu02_ldo_ops, \
689 .type = REGULATOR_VOLTAGE, \
690 .owner = THIS_MODULE, \
691 .min_uV = S2MPU02_LDO_MIN_900MV, \
692 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
693 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
694 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
695 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
696 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
697 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
698 .enable_mask = S2MPU02_ENABLE_MASK \
699 }
700 #define regulator_desc_s2mpu02_ldo4(num) { \
701 .name = "LDO"#num, \
702 .id = S2MPU02_LDO##num, \
703 .ops = &s2mpu02_ldo_ops, \
704 .type = REGULATOR_VOLTAGE, \
705 .owner = THIS_MODULE, \
706 .min_uV = S2MPU02_LDO_MIN_1050MV, \
707 .uV_step = S2MPU02_LDO_STEP_25MV, \
708 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
709 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
710 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
711 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
712 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
713 .enable_mask = S2MPU02_ENABLE_MASK \
714 }
715 #define regulator_desc_s2mpu02_ldo5(num) { \
716 .name = "LDO"#num, \
717 .id = S2MPU02_LDO##num, \
718 .ops = &s2mpu02_ldo_ops, \
719 .type = REGULATOR_VOLTAGE, \
720 .owner = THIS_MODULE, \
721 .min_uV = S2MPU02_LDO_MIN_1600MV, \
722 .uV_step = S2MPU02_LDO_STEP_50MV, \
723 .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \
724 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
725 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
726 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
727 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
728 .enable_mask = S2MPU02_ENABLE_MASK \
729 }
730
731 #define regulator_desc_s2mpu02_buck1234(num) { \
732 .name = "BUCK"#num, \
733 .id = S2MPU02_BUCK##num, \
734 .ops = &s2mpu02_buck_ops, \
735 .type = REGULATOR_VOLTAGE, \
736 .owner = THIS_MODULE, \
737 .min_uV = S2MPU02_BUCK1234_MIN_600MV, \
738 .uV_step = S2MPU02_BUCK1234_STEP_6_25MV, \
739 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
740 .linear_min_sel = S2MPU02_BUCK1234_START_SEL, \
741 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
742 .vsel_reg = S2MPU02_REG_B1CTRL2 + (num - 1) * 2, \
743 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
744 .enable_reg = S2MPU02_REG_B1CTRL1 + (num - 1) * 2, \
745 .enable_mask = S2MPU02_ENABLE_MASK \
746 }
747 #define regulator_desc_s2mpu02_buck5(num) { \
748 .name = "BUCK"#num, \
749 .id = S2MPU02_BUCK##num, \
750 .ops = &s2mpu02_ldo_ops, \
751 .type = REGULATOR_VOLTAGE, \
752 .owner = THIS_MODULE, \
753 .min_uV = S2MPU02_BUCK5_MIN_1081_25MV, \
754 .uV_step = S2MPU02_BUCK5_STEP_6_25MV, \
755 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
756 .linear_min_sel = S2MPU02_BUCK5_START_SEL, \
757 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
758 .vsel_reg = S2MPU02_REG_B5CTRL2, \
759 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
760 .enable_reg = S2MPU02_REG_B5CTRL1, \
761 .enable_mask = S2MPU02_ENABLE_MASK \
762 }
763 #define regulator_desc_s2mpu02_buck6(num) { \
764 .name = "BUCK"#num, \
765 .id = S2MPU02_BUCK##num, \
766 .ops = &s2mpu02_ldo_ops, \
767 .type = REGULATOR_VOLTAGE, \
768 .owner = THIS_MODULE, \
769 .min_uV = S2MPU02_BUCK6_MIN_1700MV, \
770 .uV_step = S2MPU02_BUCK6_STEP_2_50MV, \
771 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
772 .linear_min_sel = S2MPU02_BUCK6_START_SEL, \
773 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
774 .vsel_reg = S2MPU02_REG_B6CTRL2, \
775 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
776 .enable_reg = S2MPU02_REG_B6CTRL1, \
777 .enable_mask = S2MPU02_ENABLE_MASK \
778 }
779 #define regulator_desc_s2mpu02_buck7(num) { \
780 .name = "BUCK"#num, \
781 .id = S2MPU02_BUCK##num, \
782 .ops = &s2mpu02_ldo_ops, \
783 .type = REGULATOR_VOLTAGE, \
784 .owner = THIS_MODULE, \
785 .min_uV = S2MPU02_BUCK7_MIN_900MV, \
786 .uV_step = S2MPU02_BUCK7_STEP_6_25MV, \
787 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
788 .linear_min_sel = S2MPU02_BUCK7_START_SEL, \
789 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
790 .vsel_reg = S2MPU02_REG_B7CTRL2, \
791 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
792 .enable_reg = S2MPU02_REG_B7CTRL1, \
793 .enable_mask = S2MPU02_ENABLE_MASK \
794 }
795
796 static const struct regulator_desc s2mpu02_regulators[] = {
797 regulator_desc_s2mpu02_ldo1(1),
798 regulator_desc_s2mpu02_ldo2(2),
799 regulator_desc_s2mpu02_ldo4(3),
800 regulator_desc_s2mpu02_ldo5(4),
801 regulator_desc_s2mpu02_ldo4(5),
802 regulator_desc_s2mpu02_ldo3(6),
803 regulator_desc_s2mpu02_ldo3(7),
804 regulator_desc_s2mpu02_ldo4(8),
805 regulator_desc_s2mpu02_ldo5(9),
806 regulator_desc_s2mpu02_ldo3(10),
807 regulator_desc_s2mpu02_ldo4(11),
808 regulator_desc_s2mpu02_ldo5(12),
809 regulator_desc_s2mpu02_ldo5(13),
810 regulator_desc_s2mpu02_ldo5(14),
811 regulator_desc_s2mpu02_ldo5(15),
812 regulator_desc_s2mpu02_ldo5(16),
813 regulator_desc_s2mpu02_ldo4(17),
814 regulator_desc_s2mpu02_ldo5(18),
815 regulator_desc_s2mpu02_ldo3(19),
816 regulator_desc_s2mpu02_ldo4(20),
817 regulator_desc_s2mpu02_ldo5(21),
818 regulator_desc_s2mpu02_ldo5(22),
819 regulator_desc_s2mpu02_ldo5(23),
820 regulator_desc_s2mpu02_ldo4(24),
821 regulator_desc_s2mpu02_ldo5(25),
822 regulator_desc_s2mpu02_ldo4(26),
823 regulator_desc_s2mpu02_ldo5(27),
824 regulator_desc_s2mpu02_ldo5(28),
825 regulator_desc_s2mpu02_buck1234(1),
826 regulator_desc_s2mpu02_buck1234(2),
827 regulator_desc_s2mpu02_buck1234(3),
828 regulator_desc_s2mpu02_buck1234(4),
829 regulator_desc_s2mpu02_buck5(5),
830 regulator_desc_s2mpu02_buck6(6),
831 regulator_desc_s2mpu02_buck7(7),
832 };
833
s2mps11_pmic_probe(struct platform_device * pdev)834 static int s2mps11_pmic_probe(struct platform_device *pdev)
835 {
836 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
837 struct sec_platform_data *pdata = NULL;
838 struct of_regulator_match *rdata = NULL;
839 struct regulator_config config = { };
840 struct s2mps11_info *s2mps11;
841 int i, ret = 0;
842 const struct regulator_desc *regulators;
843
844 s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
845 GFP_KERNEL);
846 if (!s2mps11)
847 return -ENOMEM;
848
849 s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
850 switch (s2mps11->dev_type) {
851 case S2MPS11X:
852 s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
853 regulators = s2mps11_regulators;
854 break;
855 case S2MPS14X:
856 s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
857 regulators = s2mps14_regulators;
858 break;
859 case S2MPU02:
860 s2mps11->rdev_num = ARRAY_SIZE(s2mpu02_regulators);
861 regulators = s2mpu02_regulators;
862 break;
863 default:
864 dev_err(&pdev->dev, "Invalid device type: %u\n",
865 s2mps11->dev_type);
866 return -EINVAL;
867 };
868
869 s2mps11->ext_control_gpio = devm_kzalloc(&pdev->dev,
870 sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
871 GFP_KERNEL);
872 if (!s2mps11->ext_control_gpio)
873 return -ENOMEM;
874 /*
875 * 0 is a valid GPIO so initialize all GPIO-s to negative value
876 * to indicate that external control won't be used for this regulator.
877 */
878 for (i = 0; i < s2mps11->rdev_num; i++)
879 s2mps11->ext_control_gpio[i] = -EINVAL;
880
881 if (!iodev->dev->of_node) {
882 if (iodev->pdata) {
883 pdata = iodev->pdata;
884 goto common_reg;
885 } else {
886 dev_err(pdev->dev.parent,
887 "Platform data or DT node not supplied\n");
888 return -ENODEV;
889 }
890 }
891
892 rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
893 if (!rdata)
894 return -ENOMEM;
895
896 for (i = 0; i < s2mps11->rdev_num; i++)
897 rdata[i].name = regulators[i].name;
898
899 ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11);
900 if (ret)
901 goto out;
902
903 common_reg:
904 platform_set_drvdata(pdev, s2mps11);
905
906 config.dev = &pdev->dev;
907 config.regmap = iodev->regmap_pmic;
908 config.driver_data = s2mps11;
909 config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
910 for (i = 0; i < s2mps11->rdev_num; i++) {
911 struct regulator_dev *regulator;
912
913 if (pdata) {
914 config.init_data = pdata->regulators[i].initdata;
915 config.of_node = pdata->regulators[i].reg_node;
916 } else {
917 config.init_data = rdata[i].init_data;
918 config.of_node = rdata[i].of_node;
919 }
920 config.ena_gpio = s2mps11->ext_control_gpio[i];
921
922 regulator = devm_regulator_register(&pdev->dev,
923 ®ulators[i], &config);
924 if (IS_ERR(regulator)) {
925 ret = PTR_ERR(regulator);
926 dev_err(&pdev->dev, "regulator init failed for %d\n",
927 i);
928 goto out;
929 }
930
931 if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
932 ret = s2mps14_pmic_enable_ext_control(s2mps11,
933 regulator);
934 if (ret < 0) {
935 dev_err(&pdev->dev,
936 "failed to enable GPIO control over %s: %d\n",
937 regulator->desc->name, ret);
938 goto out;
939 }
940 }
941 }
942
943 out:
944 kfree(rdata);
945
946 return ret;
947 }
948
949 static const struct platform_device_id s2mps11_pmic_id[] = {
950 { "s2mps11-pmic", S2MPS11X},
951 { "s2mps14-pmic", S2MPS14X},
952 { "s2mpu02-pmic", S2MPU02},
953 { },
954 };
955 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
956
957 static struct platform_driver s2mps11_pmic_driver = {
958 .driver = {
959 .name = "s2mps11-pmic",
960 .owner = THIS_MODULE,
961 },
962 .probe = s2mps11_pmic_probe,
963 .id_table = s2mps11_pmic_id,
964 };
965
s2mps11_pmic_init(void)966 static int __init s2mps11_pmic_init(void)
967 {
968 return platform_driver_register(&s2mps11_pmic_driver);
969 }
970 subsys_initcall(s2mps11_pmic_init);
971
s2mps11_pmic_exit(void)972 static void __exit s2mps11_pmic_exit(void)
973 {
974 platform_driver_unregister(&s2mps11_pmic_driver);
975 }
976 module_exit(s2mps11_pmic_exit);
977
978 /* Module information */
979 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
980 MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPU02 Regulator Driver");
981 MODULE_LICENSE("GPL");
982