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