]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/regulator/s2mps11.c
regulator: s2mps11: Allow GPIO 0 to be used as external control on S2MPS14
[mirror_ubuntu-jammy-kernel.git] / drivers / regulator / s2mps11.c
CommitLineData
cb74685e
SK
1/*
2 * s2mps11.c
3 *
15f77300 4 * Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
cb74685e
SK
5 * http://www.samsung.com
6 *
15f77300
KK
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.
cb74685e
SK
16 *
17 */
18
19#include <linux/bug.h>
cb74685e
SK
20#include <linux/err.h>
21#include <linux/gpio.h>
22#include <linux/slab.h>
23#include <linux/module.h>
a50c6b32 24#include <linux/of.h>
939c0277 25#include <linux/regmap.h>
cb74685e
SK
26#include <linux/platform_device.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/machine.h>
a50c6b32 29#include <linux/regulator/of_regulator.h>
97f53d71 30#include <linux/of_gpio.h>
cb74685e
SK
31#include <linux/mfd/samsung/core.h>
32#include <linux/mfd/samsung/s2mps11.h>
15f77300 33#include <linux/mfd/samsung/s2mps14.h>
a50c6b32 34
cb74685e 35struct s2mps11_info {
3e80f95b 36 unsigned int rdev_num;
cb74685e
SK
37 int ramp_delay2;
38 int ramp_delay34;
39 int ramp_delay5;
40 int ramp_delay16;
41 int ramp_delay7810;
42 int ramp_delay9;
05be09bb
KK
43 /*
44 * One bit for each S2MPS14 regulator whether the suspend mode
45 * was enabled.
46 */
47 unsigned int s2mps14_suspend_state:30;
97f53d71
KK
48 /* Array of size rdev_num with GPIO-s for external sleep control */
49 int *ext_control_gpio;
cb74685e
SK
50};
51
52static int get_ramp_delay(int ramp_delay)
53{
54 unsigned char cnt = 0;
55
90068348 56 ramp_delay /= 6250;
cb74685e
SK
57
58 while (true) {
59 ramp_delay = ramp_delay >> 1;
60 if (ramp_delay == 0)
61 break;
62 cnt++;
63 }
f8f1d48b
AL
64
65 if (cnt > 3)
66 cnt = 3;
67
cb74685e
SK
68 return cnt;
69}
70
1e1598ed
YSB
71static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
72 unsigned int old_selector,
73 unsigned int new_selector)
74{
75 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
76 unsigned int ramp_delay = 0;
77 int old_volt, new_volt;
78
d55efa4d 79 switch (rdev_get_id(rdev)) {
1e1598ed 80 case S2MPS11_BUCK2:
1e1598ed
YSB
81 ramp_delay = s2mps11->ramp_delay2;
82 break;
83 case S2MPS11_BUCK3:
1e1598ed 84 case S2MPS11_BUCK4:
1e1598ed
YSB
85 ramp_delay = s2mps11->ramp_delay34;
86 break;
87 case S2MPS11_BUCK5:
88 ramp_delay = s2mps11->ramp_delay5;
89 break;
90 case S2MPS11_BUCK6:
1e1598ed
YSB
91 case S2MPS11_BUCK1:
92 ramp_delay = s2mps11->ramp_delay16;
93 break;
94 case S2MPS11_BUCK7:
95 case S2MPS11_BUCK8:
96 case S2MPS11_BUCK10:
97 ramp_delay = s2mps11->ramp_delay7810;
98 break;
99 case S2MPS11_BUCK9:
100 ramp_delay = s2mps11->ramp_delay9;
101 }
102
103 if (ramp_delay == 0)
104 ramp_delay = rdev->desc->ramp_delay;
105
106 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
107 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
108
109 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
110}
111
939c0277
YSB
112static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
113{
114 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
115 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
116 unsigned int ramp_enable = 1, enable_shift = 0;
117 int ret;
118
d55efa4d 119 switch (rdev_get_id(rdev)) {
939c0277
YSB
120 case S2MPS11_BUCK1:
121 if (ramp_delay > s2mps11->ramp_delay16)
122 s2mps11->ramp_delay16 = ramp_delay;
123 else
124 ramp_delay = s2mps11->ramp_delay16;
125
126 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
127 break;
128 case S2MPS11_BUCK2:
129 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
130 if (!ramp_delay) {
131 ramp_enable = 0;
132 break;
133 }
134
135 s2mps11->ramp_delay2 = ramp_delay;
136 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
137 ramp_reg = S2MPS11_REG_RAMP;
138 break;
139 case S2MPS11_BUCK3:
140 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
141 if (!ramp_delay) {
142 ramp_enable = 0;
143 break;
144 }
145
146 if (ramp_delay > s2mps11->ramp_delay34)
147 s2mps11->ramp_delay34 = ramp_delay;
148 else
149 ramp_delay = s2mps11->ramp_delay34;
150
151 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
152 ramp_reg = S2MPS11_REG_RAMP;
153 break;
154 case S2MPS11_BUCK4:
155 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
156 if (!ramp_delay) {
157 ramp_enable = 0;
158 break;
159 }
160
161 if (ramp_delay > s2mps11->ramp_delay34)
162 s2mps11->ramp_delay34 = ramp_delay;
163 else
164 ramp_delay = s2mps11->ramp_delay34;
165
166 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
167 ramp_reg = S2MPS11_REG_RAMP;
168 break;
169 case S2MPS11_BUCK5:
170 s2mps11->ramp_delay5 = ramp_delay;
171 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
172 break;
173 case S2MPS11_BUCK6:
174 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
175 if (!ramp_delay) {
176 ramp_enable = 0;
177 break;
178 }
179
180 if (ramp_delay > s2mps11->ramp_delay16)
181 s2mps11->ramp_delay16 = ramp_delay;
182 else
183 ramp_delay = s2mps11->ramp_delay16;
184
185 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
186 break;
187 case S2MPS11_BUCK7:
188 case S2MPS11_BUCK8:
189 case S2MPS11_BUCK10:
190 if (ramp_delay > s2mps11->ramp_delay7810)
191 s2mps11->ramp_delay7810 = ramp_delay;
192 else
193 ramp_delay = s2mps11->ramp_delay7810;
194
195 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
196 break;
197 case S2MPS11_BUCK9:
198 s2mps11->ramp_delay9 = ramp_delay;
199 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
200 break;
201 default:
202 return 0;
203 }
204
205 if (!ramp_enable)
206 goto ramp_disable;
207
b96244fa
AL
208 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
209 1 << enable_shift, 1 << enable_shift);
210 if (ret) {
211 dev_err(&rdev->dev, "failed to enable ramp rate\n");
212 return ret;
939c0277
YSB
213 }
214
215 ramp_val = get_ramp_delay(ramp_delay);
216
f8f1d48b
AL
217 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
218 ramp_val << ramp_shift);
939c0277
YSB
219
220ramp_disable:
80853304
AL
221 return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
222 1 << enable_shift, 0);
939c0277
YSB
223}
224
cb74685e
SK
225static struct regulator_ops s2mps11_ldo_ops = {
226 .list_voltage = regulator_list_voltage_linear,
227 .map_voltage = regulator_map_voltage_linear,
228 .is_enabled = regulator_is_enabled_regmap,
229 .enable = regulator_enable_regmap,
230 .disable = regulator_disable_regmap,
231 .get_voltage_sel = regulator_get_voltage_sel_regmap,
232 .set_voltage_sel = regulator_set_voltage_sel_regmap,
233 .set_voltage_time_sel = regulator_set_voltage_time_sel,
234};
235
236static struct regulator_ops s2mps11_buck_ops = {
237 .list_voltage = regulator_list_voltage_linear,
238 .map_voltage = regulator_map_voltage_linear,
239 .is_enabled = regulator_is_enabled_regmap,
240 .enable = regulator_enable_regmap,
241 .disable = regulator_disable_regmap,
242 .get_voltage_sel = regulator_get_voltage_sel_regmap,
243 .set_voltage_sel = regulator_set_voltage_sel_regmap,
1e1598ed 244 .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel,
939c0277 245 .set_ramp_delay = s2mps11_set_ramp_delay,
cb74685e
SK
246};
247
15f77300 248#define regulator_desc_s2mps11_ldo1(num) { \
cb74685e
SK
249 .name = "LDO"#num, \
250 .id = S2MPS11_LDO##num, \
251 .ops = &s2mps11_ldo_ops, \
252 .type = REGULATOR_VOLTAGE, \
253 .owner = THIS_MODULE, \
254 .min_uV = S2MPS11_LDO_MIN, \
255 .uV_step = S2MPS11_LDO_STEP1, \
256 .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
257 .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
2693fcab 258 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
cb74685e
SK
259 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
260 .enable_mask = S2MPS11_ENABLE_MASK \
261}
15f77300 262#define regulator_desc_s2mps11_ldo2(num) { \
cb74685e
SK
263 .name = "LDO"#num, \
264 .id = S2MPS11_LDO##num, \
265 .ops = &s2mps11_ldo_ops, \
266 .type = REGULATOR_VOLTAGE, \
267 .owner = THIS_MODULE, \
268 .min_uV = S2MPS11_LDO_MIN, \
269 .uV_step = S2MPS11_LDO_STEP2, \
270 .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
271 .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
2693fcab 272 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
cb74685e
SK
273 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
274 .enable_mask = S2MPS11_ENABLE_MASK \
275}
276
15f77300 277#define regulator_desc_s2mps11_buck1_4(num) { \
cb74685e
SK
278 .name = "BUCK"#num, \
279 .id = S2MPS11_BUCK##num, \
280 .ops = &s2mps11_buck_ops, \
281 .type = REGULATOR_VOLTAGE, \
282 .owner = THIS_MODULE, \
283 .min_uV = S2MPS11_BUCK_MIN1, \
284 .uV_step = S2MPS11_BUCK_STEP1, \
285 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
90068348 286 .ramp_delay = S2MPS11_RAMP_DELAY, \
cb74685e 287 .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
2693fcab 288 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
cb74685e
SK
289 .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
290 .enable_mask = S2MPS11_ENABLE_MASK \
291}
292
15f77300 293#define regulator_desc_s2mps11_buck5 { \
cb74685e
SK
294 .name = "BUCK5", \
295 .id = S2MPS11_BUCK5, \
296 .ops = &s2mps11_buck_ops, \
297 .type = REGULATOR_VOLTAGE, \
298 .owner = THIS_MODULE, \
299 .min_uV = S2MPS11_BUCK_MIN1, \
300 .uV_step = S2MPS11_BUCK_STEP1, \
301 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
90068348 302 .ramp_delay = S2MPS11_RAMP_DELAY, \
cb74685e 303 .vsel_reg = S2MPS11_REG_B5CTRL2, \
2693fcab 304 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
cb74685e
SK
305 .enable_reg = S2MPS11_REG_B5CTRL1, \
306 .enable_mask = S2MPS11_ENABLE_MASK \
307}
308
15f77300 309#define regulator_desc_s2mps11_buck6_8(num) { \
cb74685e
SK
310 .name = "BUCK"#num, \
311 .id = S2MPS11_BUCK##num, \
312 .ops = &s2mps11_buck_ops, \
313 .type = REGULATOR_VOLTAGE, \
314 .owner = THIS_MODULE, \
315 .min_uV = S2MPS11_BUCK_MIN1, \
316 .uV_step = S2MPS11_BUCK_STEP1, \
317 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
90068348 318 .ramp_delay = S2MPS11_RAMP_DELAY, \
cb74685e 319 .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
2693fcab 320 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
cb74685e
SK
321 .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
322 .enable_mask = S2MPS11_ENABLE_MASK \
323}
324
15f77300 325#define regulator_desc_s2mps11_buck9 { \
cb74685e
SK
326 .name = "BUCK9", \
327 .id = S2MPS11_BUCK9, \
328 .ops = &s2mps11_buck_ops, \
329 .type = REGULATOR_VOLTAGE, \
330 .owner = THIS_MODULE, \
331 .min_uV = S2MPS11_BUCK_MIN3, \
332 .uV_step = S2MPS11_BUCK_STEP3, \
333 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
90068348 334 .ramp_delay = S2MPS11_RAMP_DELAY, \
cb74685e 335 .vsel_reg = S2MPS11_REG_B9CTRL2, \
2693fcab 336 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
cb74685e
SK
337 .enable_reg = S2MPS11_REG_B9CTRL1, \
338 .enable_mask = S2MPS11_ENABLE_MASK \
339}
340
15f77300 341#define regulator_desc_s2mps11_buck10 { \
cb74685e
SK
342 .name = "BUCK10", \
343 .id = S2MPS11_BUCK10, \
344 .ops = &s2mps11_buck_ops, \
345 .type = REGULATOR_VOLTAGE, \
346 .owner = THIS_MODULE, \
347 .min_uV = S2MPS11_BUCK_MIN2, \
348 .uV_step = S2MPS11_BUCK_STEP2, \
349 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
90068348 350 .ramp_delay = S2MPS11_RAMP_DELAY, \
c76edd52 351 .vsel_reg = S2MPS11_REG_B10CTRL2, \
2693fcab 352 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
c76edd52 353 .enable_reg = S2MPS11_REG_B10CTRL1, \
cb74685e
SK
354 .enable_mask = S2MPS11_ENABLE_MASK \
355}
356
0f4cc282 357static const struct regulator_desc s2mps11_regulators[] = {
15f77300
KK
358 regulator_desc_s2mps11_ldo2(1),
359 regulator_desc_s2mps11_ldo1(2),
360 regulator_desc_s2mps11_ldo1(3),
361 regulator_desc_s2mps11_ldo1(4),
362 regulator_desc_s2mps11_ldo1(5),
363 regulator_desc_s2mps11_ldo2(6),
364 regulator_desc_s2mps11_ldo1(7),
365 regulator_desc_s2mps11_ldo1(8),
366 regulator_desc_s2mps11_ldo1(9),
367 regulator_desc_s2mps11_ldo1(10),
368 regulator_desc_s2mps11_ldo2(11),
369 regulator_desc_s2mps11_ldo1(12),
370 regulator_desc_s2mps11_ldo1(13),
371 regulator_desc_s2mps11_ldo1(14),
372 regulator_desc_s2mps11_ldo1(15),
373 regulator_desc_s2mps11_ldo1(16),
374 regulator_desc_s2mps11_ldo1(17),
375 regulator_desc_s2mps11_ldo1(18),
376 regulator_desc_s2mps11_ldo1(19),
377 regulator_desc_s2mps11_ldo1(20),
378 regulator_desc_s2mps11_ldo1(21),
379 regulator_desc_s2mps11_ldo2(22),
380 regulator_desc_s2mps11_ldo2(23),
381 regulator_desc_s2mps11_ldo1(24),
382 regulator_desc_s2mps11_ldo1(25),
383 regulator_desc_s2mps11_ldo1(26),
384 regulator_desc_s2mps11_ldo2(27),
385 regulator_desc_s2mps11_ldo1(28),
386 regulator_desc_s2mps11_ldo1(29),
387 regulator_desc_s2mps11_ldo1(30),
388 regulator_desc_s2mps11_ldo1(31),
389 regulator_desc_s2mps11_ldo1(32),
390 regulator_desc_s2mps11_ldo1(33),
391 regulator_desc_s2mps11_ldo1(34),
392 regulator_desc_s2mps11_ldo1(35),
393 regulator_desc_s2mps11_ldo1(36),
394 regulator_desc_s2mps11_ldo1(37),
395 regulator_desc_s2mps11_ldo1(38),
396 regulator_desc_s2mps11_buck1_4(1),
397 regulator_desc_s2mps11_buck1_4(2),
398 regulator_desc_s2mps11_buck1_4(3),
399 regulator_desc_s2mps11_buck1_4(4),
400 regulator_desc_s2mps11_buck5,
401 regulator_desc_s2mps11_buck6_8(6),
402 regulator_desc_s2mps11_buck6_8(7),
403 regulator_desc_s2mps11_buck6_8(8),
404 regulator_desc_s2mps11_buck9,
405 regulator_desc_s2mps11_buck10,
406};
407
05be09bb
KK
408static int s2mps14_regulator_enable(struct regulator_dev *rdev)
409{
410 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
411 unsigned int val;
412
413 if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
414 val = S2MPS14_ENABLE_SUSPEND;
de5d0563 415 else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
97f53d71 416 val = S2MPS14_ENABLE_EXT_CONTROL;
05be09bb
KK
417 else
418 val = rdev->desc->enable_mask;
419
420 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
421 rdev->desc->enable_mask, val);
422}
423
424static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
425{
426 int ret;
427 unsigned int val;
428 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
429
430 /* LDO3 should be always on and does not support suspend mode */
431 if (rdev_get_id(rdev) == S2MPS14_LDO3)
432 return 0;
433
434 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
435 if (ret < 0)
436 return ret;
437
438 s2mps11->s2mps14_suspend_state |= (1 << rdev_get_id(rdev));
439 /*
440 * Don't enable suspend mode if regulator is already disabled because
441 * this would effectively for a short time turn on the regulator after
442 * resuming.
443 * However we still want to toggle the suspend_state bit for regulator
444 * in case if it got enabled before suspending the system.
445 */
446 if (!(val & rdev->desc->enable_mask))
447 return 0;
448
449 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
450 rdev->desc->enable_mask, S2MPS14_ENABLE_SUSPEND);
451}
452
15f77300
KK
453static struct regulator_ops s2mps14_reg_ops = {
454 .list_voltage = regulator_list_voltage_linear,
455 .map_voltage = regulator_map_voltage_linear,
456 .is_enabled = regulator_is_enabled_regmap,
05be09bb 457 .enable = s2mps14_regulator_enable,
15f77300
KK
458 .disable = regulator_disable_regmap,
459 .get_voltage_sel = regulator_get_voltage_sel_regmap,
460 .set_voltage_sel = regulator_set_voltage_sel_regmap,
461 .set_voltage_time_sel = regulator_set_voltage_time_sel,
05be09bb 462 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
15f77300
KK
463};
464
465#define regulator_desc_s2mps14_ldo1(num) { \
466 .name = "LDO"#num, \
467 .id = S2MPS14_LDO##num, \
468 .ops = &s2mps14_reg_ops, \
469 .type = REGULATOR_VOLTAGE, \
470 .owner = THIS_MODULE, \
471 .min_uV = S2MPS14_LDO_MIN_800MV, \
472 .uV_step = S2MPS14_LDO_STEP_25MV, \
473 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
474 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
475 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
476 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
477 .enable_mask = S2MPS14_ENABLE_MASK \
478}
479#define regulator_desc_s2mps14_ldo2(num) { \
480 .name = "LDO"#num, \
481 .id = S2MPS14_LDO##num, \
482 .ops = &s2mps14_reg_ops, \
483 .type = REGULATOR_VOLTAGE, \
484 .owner = THIS_MODULE, \
485 .min_uV = S2MPS14_LDO_MIN_1800MV, \
486 .uV_step = S2MPS14_LDO_STEP_25MV, \
487 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
488 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
489 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
490 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
491 .enable_mask = S2MPS14_ENABLE_MASK \
492}
493#define regulator_desc_s2mps14_ldo3(num) { \
494 .name = "LDO"#num, \
495 .id = S2MPS14_LDO##num, \
496 .ops = &s2mps14_reg_ops, \
497 .type = REGULATOR_VOLTAGE, \
498 .owner = THIS_MODULE, \
499 .min_uV = S2MPS14_LDO_MIN_800MV, \
500 .uV_step = S2MPS14_LDO_STEP_12_5MV, \
501 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
502 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
503 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
504 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
505 .enable_mask = S2MPS14_ENABLE_MASK \
506}
507#define regulator_desc_s2mps14_buck1235(num) { \
508 .name = "BUCK"#num, \
509 .id = S2MPS14_BUCK##num, \
510 .ops = &s2mps14_reg_ops, \
511 .type = REGULATOR_VOLTAGE, \
512 .owner = THIS_MODULE, \
513 .min_uV = S2MPS14_BUCK1235_MIN_600MV, \
514 .uV_step = S2MPS14_BUCK1235_STEP_6_25MV, \
515 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
516 .linear_min_sel = S2MPS14_BUCK1235_START_SEL, \
517 .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
518 .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
519 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
520 .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
521 .enable_mask = S2MPS14_ENABLE_MASK \
522}
523#define regulator_desc_s2mps14_buck4(num) { \
524 .name = "BUCK"#num, \
525 .id = S2MPS14_BUCK##num, \
526 .ops = &s2mps14_reg_ops, \
527 .type = REGULATOR_VOLTAGE, \
528 .owner = THIS_MODULE, \
529 .min_uV = S2MPS14_BUCK4_MIN_1400MV, \
530 .uV_step = S2MPS14_BUCK4_STEP_12_5MV, \
531 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
532 .linear_min_sel = S2MPS14_BUCK4_START_SEL, \
533 .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
534 .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
535 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
536 .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
537 .enable_mask = S2MPS14_ENABLE_MASK \
538}
539
540static const struct regulator_desc s2mps14_regulators[] = {
541 regulator_desc_s2mps14_ldo3(1),
542 regulator_desc_s2mps14_ldo3(2),
543 regulator_desc_s2mps14_ldo1(3),
544 regulator_desc_s2mps14_ldo1(4),
545 regulator_desc_s2mps14_ldo3(5),
546 regulator_desc_s2mps14_ldo3(6),
547 regulator_desc_s2mps14_ldo1(7),
548 regulator_desc_s2mps14_ldo2(8),
549 regulator_desc_s2mps14_ldo3(9),
550 regulator_desc_s2mps14_ldo3(10),
551 regulator_desc_s2mps14_ldo1(11),
552 regulator_desc_s2mps14_ldo2(12),
553 regulator_desc_s2mps14_ldo2(13),
554 regulator_desc_s2mps14_ldo2(14),
555 regulator_desc_s2mps14_ldo2(15),
556 regulator_desc_s2mps14_ldo2(16),
557 regulator_desc_s2mps14_ldo2(17),
558 regulator_desc_s2mps14_ldo2(18),
559 regulator_desc_s2mps14_ldo1(19),
560 regulator_desc_s2mps14_ldo1(20),
561 regulator_desc_s2mps14_ldo1(21),
562 regulator_desc_s2mps14_ldo3(22),
563 regulator_desc_s2mps14_ldo1(23),
564 regulator_desc_s2mps14_ldo2(24),
565 regulator_desc_s2mps14_ldo2(25),
566 regulator_desc_s2mps14_buck1235(1),
567 regulator_desc_s2mps14_buck1235(2),
568 regulator_desc_s2mps14_buck1235(3),
569 regulator_desc_s2mps14_buck4(4),
570 regulator_desc_s2mps14_buck1235(5),
cb74685e
SK
571};
572
97f53d71
KK
573static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
574 struct regulator_dev *rdev)
575{
576 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
577 rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
578}
579
580static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
01170383 581 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
97f53d71
KK
582{
583 int *gpio = s2mps11->ext_control_gpio;
584 unsigned int i;
585 unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
586 S2MPS14_LDO12 };
587
588 for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
589 unsigned int reg = valid_regulators[i];
590
591 if (!rdata[reg].init_data || !rdata[reg].of_node)
592 continue;
593
594 gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
595 "samsung,ext-control-gpios", 0);
de5d0563 596 if (gpio_is_valid(gpio[reg]))
97f53d71
KK
597 dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
598 gpio[reg], reg, rdata[reg].name);
599 }
600}
601
602static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
603 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11,
604 enum sec_device_type dev_type)
01170383
KK
605{
606 struct device_node *reg_np;
607
608 reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
609 if (!reg_np) {
610 dev_err(&pdev->dev, "could not find regulators sub-node\n");
611 return -EINVAL;
612 }
613
614 of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
97f53d71
KK
615 if (dev_type == S2MPS14X)
616 s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
617
01170383
KK
618 of_node_put(reg_np);
619
620 return 0;
621}
622
a5023574 623static int s2mps11_pmic_probe(struct platform_device *pdev)
cb74685e
SK
624{
625 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
01170383 626 struct sec_platform_data *pdata = NULL;
3e80f95b 627 struct of_regulator_match *rdata = NULL;
cb74685e 628 struct regulator_config config = { };
cb74685e 629 struct s2mps11_info *s2mps11;
3e80f95b 630 int i, ret = 0;
0f4cc282
KK
631 const struct regulator_desc *regulators;
632 enum sec_device_type dev_type;
cb74685e 633
cb74685e
SK
634 s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
635 GFP_KERNEL);
636 if (!s2mps11)
637 return -ENOMEM;
638
0f4cc282
KK
639 dev_type = platform_get_device_id(pdev)->driver_data;
640 switch (dev_type) {
641 case S2MPS11X:
642 s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
643 regulators = s2mps11_regulators;
644 break;
15f77300
KK
645 case S2MPS14X:
646 s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
647 regulators = s2mps14_regulators;
648 break;
0f4cc282
KK
649 default:
650 dev_err(&pdev->dev, "Invalid device type: %u\n", dev_type);
651 return -EINVAL;
652 };
3e80f95b 653
97f53d71
KK
654 s2mps11->ext_control_gpio = devm_kzalloc(&pdev->dev,
655 sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
656 GFP_KERNEL);
657 if (!s2mps11->ext_control_gpio)
658 return -ENOMEM;
de5d0563
KK
659 /*
660 * 0 is a valid GPIO so initialize all GPIO-s to negative value
661 * to indicate that external control won't be used for this regulator.
662 */
663 for (i = 0; i < s2mps11->rdev_num; i++)
664 s2mps11->ext_control_gpio[i] = -EINVAL;
97f53d71 665
6c683c92 666 if (!iodev->dev->of_node) {
01170383
KK
667 if (iodev->pdata) {
668 pdata = iodev->pdata;
6c683c92
YSB
669 goto common_reg;
670 } else {
671 dev_err(pdev->dev.parent,
672 "Platform data or DT node not supplied\n");
673 return -ENODEV;
674 }
675 }
a50c6b32 676
0f4cc282 677 rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
3e80f95b
KK
678 if (!rdata)
679 return -ENOMEM;
680
0f4cc282 681 for (i = 0; i < s2mps11->rdev_num; i++)
a50c6b32
YSB
682 rdata[i].name = regulators[i].name;
683
97f53d71 684 ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11, dev_type);
01170383 685 if (ret)
3e80f95b 686 goto out;
a50c6b32 687
a50c6b32
YSB
688common_reg:
689 platform_set_drvdata(pdev, s2mps11);
cb74685e 690
a50c6b32 691 config.dev = &pdev->dev;
1b1ccee1 692 config.regmap = iodev->regmap_pmic;
a50c6b32 693 config.driver_data = s2mps11;
de5d0563 694 config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
0f4cc282 695 for (i = 0; i < s2mps11->rdev_num; i++) {
31195252
KK
696 struct regulator_dev *regulator;
697
01170383 698 if (pdata) {
a50c6b32 699 config.init_data = pdata->regulators[i].initdata;
54820f58 700 config.of_node = pdata->regulators[i].reg_node;
a50c6b32
YSB
701 } else {
702 config.init_data = rdata[i].init_data;
703 config.of_node = rdata[i].of_node;
704 }
de5d0563 705 config.ena_gpio = s2mps11->ext_control_gpio[i];
97f53d71 706
31195252 707 regulator = devm_regulator_register(&pdev->dev,
d55cd794 708 &regulators[i], &config);
31195252
KK
709 if (IS_ERR(regulator)) {
710 ret = PTR_ERR(regulator);
232b2504
AL
711 dev_err(&pdev->dev, "regulator init failed for %d\n",
712 i);
3e80f95b 713 goto out;
cb74685e 714 }
97f53d71 715
de5d0563 716 if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
97f53d71
KK
717 ret = s2mps14_pmic_enable_ext_control(s2mps11,
718 regulator);
719 if (ret < 0) {
720 dev_err(&pdev->dev,
721 "failed to enable GPIO control over %s: %d\n",
722 regulator->desc->name, ret);
723 goto out;
724 }
725 }
cb74685e
SK
726 }
727
3e80f95b
KK
728out:
729 kfree(rdata);
730
731 return ret;
cb74685e
SK
732}
733
734static const struct platform_device_id s2mps11_pmic_id[] = {
3e80f95b 735 { "s2mps11-pmic", S2MPS11X},
15f77300 736 { "s2mps14-pmic", S2MPS14X},
cb74685e
SK
737 { },
738};
739MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
740
741static struct platform_driver s2mps11_pmic_driver = {
742 .driver = {
743 .name = "s2mps11-pmic",
744 .owner = THIS_MODULE,
745 },
746 .probe = s2mps11_pmic_probe,
cb74685e
SK
747 .id_table = s2mps11_pmic_id,
748};
749
750static int __init s2mps11_pmic_init(void)
751{
752 return platform_driver_register(&s2mps11_pmic_driver);
753}
754subsys_initcall(s2mps11_pmic_init);
755
756static void __exit s2mps11_pmic_exit(void)
757{
758 platform_driver_unregister(&s2mps11_pmic_driver);
759}
760module_exit(s2mps11_pmic_exit);
761
762/* Module information */
763MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
15f77300 764MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14 Regulator Driver");
cb74685e 765MODULE_LICENSE("GPL");