Merge remote-tracking branches 'regulator/fix/88pm800', 'regulator/fix/max8973',...
[linux-drm-fsl-dcu.git] / 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_buck6_10(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 static const struct regulator_desc s2mps11_regulators[] = {
325         regulator_desc_s2mps11_ldo(1, STEP_25_MV),
326         regulator_desc_s2mps11_ldo(2, STEP_50_MV),
327         regulator_desc_s2mps11_ldo(3, STEP_50_MV),
328         regulator_desc_s2mps11_ldo(4, STEP_50_MV),
329         regulator_desc_s2mps11_ldo(5, STEP_50_MV),
330         regulator_desc_s2mps11_ldo(6, STEP_25_MV),
331         regulator_desc_s2mps11_ldo(7, STEP_50_MV),
332         regulator_desc_s2mps11_ldo(8, STEP_50_MV),
333         regulator_desc_s2mps11_ldo(9, STEP_50_MV),
334         regulator_desc_s2mps11_ldo(10, STEP_50_MV),
335         regulator_desc_s2mps11_ldo(11, STEP_25_MV),
336         regulator_desc_s2mps11_ldo(12, STEP_50_MV),
337         regulator_desc_s2mps11_ldo(13, STEP_50_MV),
338         regulator_desc_s2mps11_ldo(14, STEP_50_MV),
339         regulator_desc_s2mps11_ldo(15, STEP_50_MV),
340         regulator_desc_s2mps11_ldo(16, STEP_50_MV),
341         regulator_desc_s2mps11_ldo(17, STEP_50_MV),
342         regulator_desc_s2mps11_ldo(18, STEP_50_MV),
343         regulator_desc_s2mps11_ldo(19, STEP_50_MV),
344         regulator_desc_s2mps11_ldo(20, STEP_50_MV),
345         regulator_desc_s2mps11_ldo(21, STEP_50_MV),
346         regulator_desc_s2mps11_ldo(22, STEP_25_MV),
347         regulator_desc_s2mps11_ldo(23, STEP_25_MV),
348         regulator_desc_s2mps11_ldo(24, STEP_50_MV),
349         regulator_desc_s2mps11_ldo(25, STEP_50_MV),
350         regulator_desc_s2mps11_ldo(26, STEP_50_MV),
351         regulator_desc_s2mps11_ldo(27, STEP_25_MV),
352         regulator_desc_s2mps11_ldo(28, STEP_50_MV),
353         regulator_desc_s2mps11_ldo(29, STEP_50_MV),
354         regulator_desc_s2mps11_ldo(30, STEP_50_MV),
355         regulator_desc_s2mps11_ldo(31, STEP_50_MV),
356         regulator_desc_s2mps11_ldo(32, STEP_50_MV),
357         regulator_desc_s2mps11_ldo(33, STEP_50_MV),
358         regulator_desc_s2mps11_ldo(34, STEP_50_MV),
359         regulator_desc_s2mps11_ldo(35, STEP_50_MV),
360         regulator_desc_s2mps11_ldo(36, STEP_50_MV),
361         regulator_desc_s2mps11_ldo(37, STEP_50_MV),
362         regulator_desc_s2mps11_ldo(38, STEP_50_MV),
363         regulator_desc_s2mps11_buck1_4(1),
364         regulator_desc_s2mps11_buck1_4(2),
365         regulator_desc_s2mps11_buck1_4(3),
366         regulator_desc_s2mps11_buck1_4(4),
367         regulator_desc_s2mps11_buck5,
368         regulator_desc_s2mps11_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
369         regulator_desc_s2mps11_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
370         regulator_desc_s2mps11_buck6_10(8, MIN_600_MV, STEP_6_25_MV),
371         regulator_desc_s2mps11_buck6_10(9, MIN_3000_MV, STEP_25_MV),
372         regulator_desc_s2mps11_buck6_10(10, MIN_750_MV, STEP_12_5_MV),
373 };
374
375 static struct regulator_ops s2mps14_reg_ops;
376
377 #define regulator_desc_s2mps13_ldo(num, min, step, min_sel) {   \
378         .name           = "LDO"#num,                            \
379         .id             = S2MPS13_LDO##num,                     \
380         .ops            = &s2mps14_reg_ops,                     \
381         .type           = REGULATOR_VOLTAGE,                    \
382         .owner          = THIS_MODULE,                          \
383         .min_uV         = min,                                  \
384         .uV_step        = step,                                 \
385         .linear_min_sel = min_sel,                              \
386         .n_voltages     = S2MPS14_LDO_N_VOLTAGES,               \
387         .vsel_reg       = S2MPS13_REG_L1CTRL + num - 1,         \
388         .vsel_mask      = S2MPS14_LDO_VSEL_MASK,                \
389         .enable_reg     = S2MPS13_REG_L1CTRL + num - 1,         \
390         .enable_mask    = S2MPS14_ENABLE_MASK                   \
391 }
392
393 #define regulator_desc_s2mps13_buck(num, min, step, min_sel) {  \
394         .name           = "BUCK"#num,                           \
395         .id             = S2MPS13_BUCK##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_BUCK_N_VOLTAGES,              \
403         .ramp_delay     = S2MPS13_BUCK_RAMP_DELAY,              \
404         .vsel_reg       = S2MPS13_REG_B1OUT + (num - 1) * 2,    \
405         .vsel_mask      = S2MPS14_BUCK_VSEL_MASK,               \
406         .enable_reg     = S2MPS13_REG_B1CTRL + (num - 1) * 2,   \
407         .enable_mask    = S2MPS14_ENABLE_MASK                   \
408 }
409
410 #define regulator_desc_s2mps13_buck7(num, min, step, min_sel) { \
411         .name           = "BUCK"#num,                           \
412         .id             = S2MPS13_BUCK##num,                    \
413         .ops            = &s2mps14_reg_ops,                     \
414         .type           = REGULATOR_VOLTAGE,                    \
415         .owner          = THIS_MODULE,                          \
416         .min_uV         = min,                                  \
417         .uV_step        = step,                                 \
418         .linear_min_sel = min_sel,                              \
419         .n_voltages     = S2MPS14_BUCK_N_VOLTAGES,              \
420         .ramp_delay     = S2MPS13_BUCK_RAMP_DELAY,              \
421         .vsel_reg       = S2MPS13_REG_B1OUT + (num) * 2 - 1,    \
422         .vsel_mask      = S2MPS14_BUCK_VSEL_MASK,               \
423         .enable_reg     = S2MPS13_REG_B1CTRL + (num - 1) * 2,   \
424         .enable_mask    = S2MPS14_ENABLE_MASK                   \
425 }
426
427 #define regulator_desc_s2mps13_buck8_10(num, min, step, min_sel) {      \
428         .name           = "BUCK"#num,                           \
429         .id             = S2MPS13_BUCK##num,                    \
430         .ops            = &s2mps14_reg_ops,                     \
431         .type           = REGULATOR_VOLTAGE,                    \
432         .owner          = THIS_MODULE,                          \
433         .min_uV         = min,                                  \
434         .uV_step        = step,                                 \
435         .linear_min_sel = min_sel,                              \
436         .n_voltages     = S2MPS14_BUCK_N_VOLTAGES,              \
437         .ramp_delay     = S2MPS13_BUCK_RAMP_DELAY,              \
438         .vsel_reg       = S2MPS13_REG_B1OUT + (num) * 2 - 1,    \
439         .vsel_mask      = S2MPS14_BUCK_VSEL_MASK,               \
440         .enable_reg     = S2MPS13_REG_B1CTRL + (num) * 2 - 1,   \
441         .enable_mask    = S2MPS14_ENABLE_MASK                   \
442 }
443
444 static const struct regulator_desc s2mps13_regulators[] = {
445         regulator_desc_s2mps13_ldo(1,  MIN_800_MV,  STEP_12_5_MV, 0x00),
446         regulator_desc_s2mps13_ldo(2,  MIN_1400_MV, STEP_50_MV,   0x0C),
447         regulator_desc_s2mps13_ldo(3,  MIN_1000_MV, STEP_25_MV,   0x08),
448         regulator_desc_s2mps13_ldo(4,  MIN_800_MV,  STEP_12_5_MV, 0x00),
449         regulator_desc_s2mps13_ldo(5,  MIN_800_MV,  STEP_12_5_MV, 0x00),
450         regulator_desc_s2mps13_ldo(6,  MIN_800_MV,  STEP_12_5_MV, 0x00),
451         regulator_desc_s2mps13_ldo(7,  MIN_1000_MV, STEP_25_MV,   0x08),
452         regulator_desc_s2mps13_ldo(8,  MIN_1000_MV, STEP_25_MV,   0x08),
453         regulator_desc_s2mps13_ldo(9,  MIN_1000_MV, STEP_25_MV,   0x08),
454         regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV,   0x0C),
455         regulator_desc_s2mps13_ldo(11, MIN_800_MV,  STEP_25_MV,   0x10),
456         regulator_desc_s2mps13_ldo(12, MIN_800_MV,  STEP_25_MV,   0x10),
457         regulator_desc_s2mps13_ldo(13, MIN_800_MV,  STEP_25_MV,   0x10),
458         regulator_desc_s2mps13_ldo(14, MIN_800_MV,  STEP_12_5_MV, 0x00),
459         regulator_desc_s2mps13_ldo(15, MIN_800_MV,  STEP_12_5_MV, 0x00),
460         regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV,   0x0C),
461         regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV,   0x0C),
462         regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV,   0x08),
463         regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV,   0x08),
464         regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV,   0x0C),
465         regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV,   0x08),
466         regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV,   0x08),
467         regulator_desc_s2mps13_ldo(23, MIN_800_MV,  STEP_12_5_MV, 0x00),
468         regulator_desc_s2mps13_ldo(24, MIN_800_MV,  STEP_12_5_MV, 0x00),
469         regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV,   0x0C),
470         regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV,   0x0C),
471         regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV,   0x0C),
472         regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV,   0x08),
473         regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV,   0x0C),
474         regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV,   0x0C),
475         regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV,   0x08),
476         regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV,   0x08),
477         regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV,   0x0C),
478         regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV,   0x08),
479         regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV,   0x0C),
480         regulator_desc_s2mps13_ldo(36, MIN_800_MV,  STEP_12_5_MV, 0x00),
481         regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV,   0x08),
482         regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV,   0x0C),
483         regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV,   0x08),
484         regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV,   0x0C),
485         regulator_desc_s2mps13_buck(1,  MIN_500_MV,  STEP_6_25_MV, 0x10),
486         regulator_desc_s2mps13_buck(2,  MIN_500_MV,  STEP_6_25_MV, 0x10),
487         regulator_desc_s2mps13_buck(3,  MIN_500_MV,  STEP_6_25_MV, 0x10),
488         regulator_desc_s2mps13_buck(4,  MIN_500_MV,  STEP_6_25_MV, 0x10),
489         regulator_desc_s2mps13_buck(5,  MIN_500_MV,  STEP_6_25_MV, 0x10),
490         regulator_desc_s2mps13_buck(6,  MIN_500_MV,  STEP_6_25_MV, 0x10),
491         regulator_desc_s2mps13_buck7(7,  MIN_500_MV,  STEP_6_25_MV, 0x10),
492         regulator_desc_s2mps13_buck8_10(8,  MIN_1000_MV, STEP_12_5_MV, 0x20),
493         regulator_desc_s2mps13_buck8_10(9,  MIN_1000_MV, STEP_12_5_MV, 0x20),
494         regulator_desc_s2mps13_buck8_10(10, MIN_500_MV,  STEP_6_25_MV, 0x10),
495 };
496
497 static int s2mps14_regulator_enable(struct regulator_dev *rdev)
498 {
499         struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
500         unsigned int val;
501
502         switch (s2mps11->dev_type) {
503         case S2MPS13X:
504         case S2MPS14X:
505                 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state))
506                         val = S2MPS14_ENABLE_SUSPEND;
507                 else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
508                         val = S2MPS14_ENABLE_EXT_CONTROL;
509                 else
510                         val = rdev->desc->enable_mask;
511                 break;
512         case S2MPU02:
513                 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state))
514                         val = S2MPU02_ENABLE_SUSPEND;
515                 else
516                         val = rdev->desc->enable_mask;
517                 break;
518         default:
519                 return -EINVAL;
520         }
521
522         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
523                         rdev->desc->enable_mask, val);
524 }
525
526 static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
527 {
528         int ret;
529         unsigned int val, state;
530         struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
531         int rdev_id = rdev_get_id(rdev);
532
533         /* Below LDO should be always on or does not support suspend mode. */
534         switch (s2mps11->dev_type) {
535         case S2MPS13X:
536         case S2MPS14X:
537                 switch (rdev_id) {
538                 case S2MPS14_LDO3:
539                         return 0;
540                 default:
541                         state = S2MPS14_ENABLE_SUSPEND;
542                         break;
543                 }
544                 break;
545         case S2MPU02:
546                 switch (rdev_id) {
547                 case S2MPU02_LDO13:
548                 case S2MPU02_LDO14:
549                 case S2MPU02_LDO15:
550                 case S2MPU02_LDO17:
551                 case S2MPU02_BUCK7:
552                         state = S2MPU02_DISABLE_SUSPEND;
553                         break;
554                 default:
555                         state = S2MPU02_ENABLE_SUSPEND;
556                         break;
557                 }
558                 break;
559         default:
560                 return -EINVAL;
561         }
562
563         ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
564         if (ret < 0)
565                 return ret;
566
567         set_bit(rdev_get_id(rdev), s2mps11->suspend_state);
568         /*
569          * Don't enable suspend mode if regulator is already disabled because
570          * this would effectively for a short time turn on the regulator after
571          * resuming.
572          * However we still want to toggle the suspend_state bit for regulator
573          * in case if it got enabled before suspending the system.
574          */
575         if (!(val & rdev->desc->enable_mask))
576                 return 0;
577
578         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
579                         rdev->desc->enable_mask, state);
580 }
581
582 static struct regulator_ops s2mps14_reg_ops = {
583         .list_voltage           = regulator_list_voltage_linear,
584         .map_voltage            = regulator_map_voltage_linear,
585         .is_enabled             = regulator_is_enabled_regmap,
586         .enable                 = s2mps14_regulator_enable,
587         .disable                = regulator_disable_regmap,
588         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
589         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
590         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
591         .set_suspend_disable    = s2mps14_regulator_set_suspend_disable,
592 };
593
594 #define regulator_desc_s2mps14_ldo(num, min, step) {    \
595         .name           = "LDO"#num,                    \
596         .id             = S2MPS14_LDO##num,             \
597         .ops            = &s2mps14_reg_ops,             \
598         .type           = REGULATOR_VOLTAGE,            \
599         .owner          = THIS_MODULE,                  \
600         .min_uV         = min,                          \
601         .uV_step        = step,                         \
602         .n_voltages     = S2MPS14_LDO_N_VOLTAGES,       \
603         .vsel_reg       = S2MPS14_REG_L1CTRL + num - 1, \
604         .vsel_mask      = S2MPS14_LDO_VSEL_MASK,        \
605         .enable_reg     = S2MPS14_REG_L1CTRL + num - 1, \
606         .enable_mask    = S2MPS14_ENABLE_MASK           \
607 }
608
609 #define regulator_desc_s2mps14_buck(num, min, step, min_sel) {  \
610         .name           = "BUCK"#num,                           \
611         .id             = S2MPS14_BUCK##num,                    \
612         .ops            = &s2mps14_reg_ops,                     \
613         .type           = REGULATOR_VOLTAGE,                    \
614         .owner          = THIS_MODULE,                          \
615         .min_uV         = min,                                  \
616         .uV_step        = step,                                 \
617         .n_voltages     = S2MPS14_BUCK_N_VOLTAGES,              \
618         .linear_min_sel = min_sel,                              \
619         .ramp_delay     = S2MPS14_BUCK_RAMP_DELAY,              \
620         .vsel_reg       = S2MPS14_REG_B1CTRL2 + (num - 1) * 2,  \
621         .vsel_mask      = S2MPS14_BUCK_VSEL_MASK,               \
622         .enable_reg     = S2MPS14_REG_B1CTRL1 + (num - 1) * 2,  \
623         .enable_mask    = S2MPS14_ENABLE_MASK                   \
624 }
625
626 static const struct regulator_desc s2mps14_regulators[] = {
627         regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
628         regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
629         regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
630         regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
631         regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
632         regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
633         regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
634         regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
635         regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
636         regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
637         regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
638         regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
639         regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
640         regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
641         regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
642         regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
643         regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
644         regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
645         regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
646         regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
647         regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
648         regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
649         regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
650         regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
651         regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
652         regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV,
653                                     S2MPS14_BUCK1235_START_SEL),
654         regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV,
655                                     S2MPS14_BUCK1235_START_SEL),
656         regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV,
657                                     S2MPS14_BUCK1235_START_SEL),
658         regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV,
659                                     S2MPS14_BUCK4_START_SEL),
660         regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV,
661                                     S2MPS14_BUCK1235_START_SEL),
662 };
663
664 static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
665                 struct regulator_dev *rdev)
666 {
667         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
668                         rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
669 }
670
671 static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
672                 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
673 {
674         int *gpio = s2mps11->ext_control_gpio;
675         unsigned int i;
676         unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
677                 S2MPS14_LDO12 };
678
679         for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
680                 unsigned int reg = valid_regulators[i];
681
682                 if (!rdata[reg].init_data || !rdata[reg].of_node)
683                         continue;
684
685                 gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
686                                 "samsung,ext-control-gpios", 0);
687                 if (gpio_is_valid(gpio[reg]))
688                         dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
689                                         gpio[reg], reg, rdata[reg].name);
690         }
691 }
692
693 static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
694                 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
695 {
696         struct device_node *reg_np;
697
698         reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
699         if (!reg_np) {
700                 dev_err(&pdev->dev, "could not find regulators sub-node\n");
701                 return -EINVAL;
702         }
703
704         of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
705         if (s2mps11->dev_type == S2MPS14X)
706                 s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
707
708         of_node_put(reg_np);
709
710         return 0;
711 }
712
713 static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
714 {
715         unsigned int ramp_val, ramp_shift, ramp_reg;
716
717         switch (rdev_get_id(rdev)) {
718         case S2MPU02_BUCK1:
719                 ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
720                 break;
721         case S2MPU02_BUCK2:
722                 ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
723                 break;
724         case S2MPU02_BUCK3:
725                 ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
726                 break;
727         case S2MPU02_BUCK4:
728                 ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
729                 break;
730         default:
731                 return 0;
732         }
733         ramp_reg = S2MPU02_REG_RAMP1;
734         ramp_val = get_ramp_delay(ramp_delay);
735
736         return regmap_update_bits(rdev->regmap, ramp_reg,
737                                   S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
738                                   ramp_val << ramp_shift);
739 }
740
741 static struct regulator_ops s2mpu02_ldo_ops = {
742         .list_voltage           = regulator_list_voltage_linear,
743         .map_voltage            = regulator_map_voltage_linear,
744         .is_enabled             = regulator_is_enabled_regmap,
745         .enable                 = s2mps14_regulator_enable,
746         .disable                = regulator_disable_regmap,
747         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
748         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
749         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
750         .set_suspend_disable    = s2mps14_regulator_set_suspend_disable,
751 };
752
753 static struct regulator_ops s2mpu02_buck_ops = {
754         .list_voltage           = regulator_list_voltage_linear,
755         .map_voltage            = regulator_map_voltage_linear,
756         .is_enabled             = regulator_is_enabled_regmap,
757         .enable                 = s2mps14_regulator_enable,
758         .disable                = regulator_disable_regmap,
759         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
760         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
761         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
762         .set_suspend_disable    = s2mps14_regulator_set_suspend_disable,
763         .set_ramp_delay         = s2mpu02_set_ramp_delay,
764 };
765
766 #define regulator_desc_s2mpu02_ldo1(num) {              \
767         .name           = "LDO"#num,                    \
768         .id             = S2MPU02_LDO##num,             \
769         .ops            = &s2mpu02_ldo_ops,             \
770         .type           = REGULATOR_VOLTAGE,            \
771         .owner          = THIS_MODULE,                  \
772         .min_uV         = S2MPU02_LDO_MIN_900MV,        \
773         .uV_step        = S2MPU02_LDO_STEP_12_5MV,      \
774         .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
775         .n_voltages     = S2MPU02_LDO_N_VOLTAGES,       \
776         .vsel_reg       = S2MPU02_REG_L1CTRL,           \
777         .vsel_mask      = S2MPU02_LDO_VSEL_MASK,        \
778         .enable_reg     = S2MPU02_REG_L1CTRL,           \
779         .enable_mask    = S2MPU02_ENABLE_MASK           \
780 }
781 #define regulator_desc_s2mpu02_ldo2(num) {              \
782         .name           = "LDO"#num,                    \
783         .id             = S2MPU02_LDO##num,             \
784         .ops            = &s2mpu02_ldo_ops,             \
785         .type           = REGULATOR_VOLTAGE,            \
786         .owner          = THIS_MODULE,                  \
787         .min_uV         = S2MPU02_LDO_MIN_1050MV,       \
788         .uV_step        = S2MPU02_LDO_STEP_25MV,        \
789         .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
790         .n_voltages     = S2MPU02_LDO_N_VOLTAGES,       \
791         .vsel_reg       = S2MPU02_REG_L2CTRL1,          \
792         .vsel_mask      = S2MPU02_LDO_VSEL_MASK,        \
793         .enable_reg     = S2MPU02_REG_L2CTRL1,          \
794         .enable_mask    = S2MPU02_ENABLE_MASK           \
795 }
796 #define regulator_desc_s2mpu02_ldo3(num) {              \
797         .name           = "LDO"#num,                    \
798         .id             = S2MPU02_LDO##num,             \
799         .ops            = &s2mpu02_ldo_ops,             \
800         .type           = REGULATOR_VOLTAGE,            \
801         .owner          = THIS_MODULE,                  \
802         .min_uV         = S2MPU02_LDO_MIN_900MV,        \
803         .uV_step        = S2MPU02_LDO_STEP_12_5MV,      \
804         .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
805         .n_voltages     = S2MPU02_LDO_N_VOLTAGES,       \
806         .vsel_reg       = S2MPU02_REG_L3CTRL + num - 3, \
807         .vsel_mask      = S2MPU02_LDO_VSEL_MASK,        \
808         .enable_reg     = S2MPU02_REG_L3CTRL + num - 3, \
809         .enable_mask    = S2MPU02_ENABLE_MASK           \
810 }
811 #define regulator_desc_s2mpu02_ldo4(num) {              \
812         .name           = "LDO"#num,                    \
813         .id             = S2MPU02_LDO##num,             \
814         .ops            = &s2mpu02_ldo_ops,             \
815         .type           = REGULATOR_VOLTAGE,            \
816         .owner          = THIS_MODULE,                  \
817         .min_uV         = S2MPU02_LDO_MIN_1050MV,       \
818         .uV_step        = S2MPU02_LDO_STEP_25MV,        \
819         .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
820         .n_voltages     = S2MPU02_LDO_N_VOLTAGES,       \
821         .vsel_reg       = S2MPU02_REG_L3CTRL + num - 3, \
822         .vsel_mask      = S2MPU02_LDO_VSEL_MASK,        \
823         .enable_reg     = S2MPU02_REG_L3CTRL + num - 3, \
824         .enable_mask    = S2MPU02_ENABLE_MASK           \
825 }
826 #define regulator_desc_s2mpu02_ldo5(num) {              \
827         .name           = "LDO"#num,                    \
828         .id             = S2MPU02_LDO##num,             \
829         .ops            = &s2mpu02_ldo_ops,             \
830         .type           = REGULATOR_VOLTAGE,            \
831         .owner          = THIS_MODULE,                  \
832         .min_uV         = S2MPU02_LDO_MIN_1600MV,       \
833         .uV_step        = S2MPU02_LDO_STEP_50MV,        \
834         .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \
835         .n_voltages     = S2MPU02_LDO_N_VOLTAGES,       \
836         .vsel_reg       = S2MPU02_REG_L3CTRL + num - 3, \
837         .vsel_mask      = S2MPU02_LDO_VSEL_MASK,        \
838         .enable_reg     = S2MPU02_REG_L3CTRL + num - 3, \
839         .enable_mask    = S2MPU02_ENABLE_MASK           \
840 }
841
842 #define regulator_desc_s2mpu02_buck1234(num) {                  \
843         .name           = "BUCK"#num,                           \
844         .id             = S2MPU02_BUCK##num,                    \
845         .ops            = &s2mpu02_buck_ops,                    \
846         .type           = REGULATOR_VOLTAGE,                    \
847         .owner          = THIS_MODULE,                          \
848         .min_uV         = S2MPU02_BUCK1234_MIN_600MV,           \
849         .uV_step        = S2MPU02_BUCK1234_STEP_6_25MV,         \
850         .n_voltages     = S2MPU02_BUCK_N_VOLTAGES,              \
851         .linear_min_sel = S2MPU02_BUCK1234_START_SEL,           \
852         .ramp_delay     = S2MPU02_BUCK_RAMP_DELAY,              \
853         .vsel_reg       = S2MPU02_REG_B1CTRL2 + (num - 1) * 2,  \
854         .vsel_mask      = S2MPU02_BUCK_VSEL_MASK,               \
855         .enable_reg     = S2MPU02_REG_B1CTRL1 + (num - 1) * 2,  \
856         .enable_mask    = S2MPU02_ENABLE_MASK                   \
857 }
858 #define regulator_desc_s2mpu02_buck5(num) {                     \
859         .name           = "BUCK"#num,                           \
860         .id             = S2MPU02_BUCK##num,                    \
861         .ops            = &s2mpu02_ldo_ops,                     \
862         .type           = REGULATOR_VOLTAGE,                    \
863         .owner          = THIS_MODULE,                          \
864         .min_uV         = S2MPU02_BUCK5_MIN_1081_25MV,          \
865         .uV_step        = S2MPU02_BUCK5_STEP_6_25MV,            \
866         .n_voltages     = S2MPU02_BUCK_N_VOLTAGES,              \
867         .linear_min_sel = S2MPU02_BUCK5_START_SEL,              \
868         .ramp_delay     = S2MPU02_BUCK_RAMP_DELAY,              \
869         .vsel_reg       = S2MPU02_REG_B5CTRL2,                  \
870         .vsel_mask      = S2MPU02_BUCK_VSEL_MASK,               \
871         .enable_reg     = S2MPU02_REG_B5CTRL1,                  \
872         .enable_mask    = S2MPU02_ENABLE_MASK                   \
873 }
874 #define regulator_desc_s2mpu02_buck6(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_BUCK6_MIN_1700MV,             \
881         .uV_step        = S2MPU02_BUCK6_STEP_2_50MV,            \
882         .n_voltages     = S2MPU02_BUCK_N_VOLTAGES,              \
883         .linear_min_sel = S2MPU02_BUCK6_START_SEL,              \
884         .ramp_delay     = S2MPU02_BUCK_RAMP_DELAY,              \
885         .vsel_reg       = S2MPU02_REG_B6CTRL2,                  \
886         .vsel_mask      = S2MPU02_BUCK_VSEL_MASK,               \
887         .enable_reg     = S2MPU02_REG_B6CTRL1,                  \
888         .enable_mask    = S2MPU02_ENABLE_MASK                   \
889 }
890 #define regulator_desc_s2mpu02_buck7(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_BUCK7_MIN_900MV,              \
897         .uV_step        = S2MPU02_BUCK7_STEP_6_25MV,            \
898         .n_voltages     = S2MPU02_BUCK_N_VOLTAGES,              \
899         .linear_min_sel = S2MPU02_BUCK7_START_SEL,              \
900         .ramp_delay     = S2MPU02_BUCK_RAMP_DELAY,              \
901         .vsel_reg       = S2MPU02_REG_B7CTRL2,                  \
902         .vsel_mask      = S2MPU02_BUCK_VSEL_MASK,               \
903         .enable_reg     = S2MPU02_REG_B7CTRL1,                  \
904         .enable_mask    = S2MPU02_ENABLE_MASK                   \
905 }
906
907 static const struct regulator_desc s2mpu02_regulators[] = {
908         regulator_desc_s2mpu02_ldo1(1),
909         regulator_desc_s2mpu02_ldo2(2),
910         regulator_desc_s2mpu02_ldo4(3),
911         regulator_desc_s2mpu02_ldo5(4),
912         regulator_desc_s2mpu02_ldo4(5),
913         regulator_desc_s2mpu02_ldo3(6),
914         regulator_desc_s2mpu02_ldo3(7),
915         regulator_desc_s2mpu02_ldo4(8),
916         regulator_desc_s2mpu02_ldo5(9),
917         regulator_desc_s2mpu02_ldo3(10),
918         regulator_desc_s2mpu02_ldo4(11),
919         regulator_desc_s2mpu02_ldo5(12),
920         regulator_desc_s2mpu02_ldo5(13),
921         regulator_desc_s2mpu02_ldo5(14),
922         regulator_desc_s2mpu02_ldo5(15),
923         regulator_desc_s2mpu02_ldo5(16),
924         regulator_desc_s2mpu02_ldo4(17),
925         regulator_desc_s2mpu02_ldo5(18),
926         regulator_desc_s2mpu02_ldo3(19),
927         regulator_desc_s2mpu02_ldo4(20),
928         regulator_desc_s2mpu02_ldo5(21),
929         regulator_desc_s2mpu02_ldo5(22),
930         regulator_desc_s2mpu02_ldo5(23),
931         regulator_desc_s2mpu02_ldo4(24),
932         regulator_desc_s2mpu02_ldo5(25),
933         regulator_desc_s2mpu02_ldo4(26),
934         regulator_desc_s2mpu02_ldo5(27),
935         regulator_desc_s2mpu02_ldo5(28),
936         regulator_desc_s2mpu02_buck1234(1),
937         regulator_desc_s2mpu02_buck1234(2),
938         regulator_desc_s2mpu02_buck1234(3),
939         regulator_desc_s2mpu02_buck1234(4),
940         regulator_desc_s2mpu02_buck5(5),
941         regulator_desc_s2mpu02_buck6(6),
942         regulator_desc_s2mpu02_buck7(7),
943 };
944
945 static int s2mps11_pmic_probe(struct platform_device *pdev)
946 {
947         struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
948         struct sec_platform_data *pdata = NULL;
949         struct of_regulator_match *rdata = NULL;
950         struct regulator_config config = { };
951         struct s2mps11_info *s2mps11;
952         int i, ret = 0;
953         const struct regulator_desc *regulators;
954
955         s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
956                                 GFP_KERNEL);
957         if (!s2mps11)
958                 return -ENOMEM;
959
960         s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
961         switch (s2mps11->dev_type) {
962         case S2MPS11X:
963                 s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
964                 regulators = s2mps11_regulators;
965                 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
966                 break;
967         case S2MPS13X:
968                 s2mps11->rdev_num = ARRAY_SIZE(s2mps13_regulators);
969                 regulators = s2mps13_regulators;
970                 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
971                 break;
972         case S2MPS14X:
973                 s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
974                 regulators = s2mps14_regulators;
975                 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
976                 break;
977         case S2MPU02:
978                 s2mps11->rdev_num = ARRAY_SIZE(s2mpu02_regulators);
979                 regulators = s2mpu02_regulators;
980                 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
981                 break;
982         default:
983                 dev_err(&pdev->dev, "Invalid device type: %u\n",
984                                     s2mps11->dev_type);
985                 return -EINVAL;
986         }
987
988         s2mps11->ext_control_gpio = devm_kmalloc(&pdev->dev,
989                         sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
990                         GFP_KERNEL);
991         if (!s2mps11->ext_control_gpio)
992                 return -ENOMEM;
993         /*
994          * 0 is a valid GPIO so initialize all GPIO-s to negative value
995          * to indicate that external control won't be used for this regulator.
996          */
997         for (i = 0; i < s2mps11->rdev_num; i++)
998                 s2mps11->ext_control_gpio[i] = -EINVAL;
999
1000         if (!iodev->dev->of_node) {
1001                 if (iodev->pdata) {
1002                         pdata = iodev->pdata;
1003                         goto common_reg;
1004                 } else {
1005                         dev_err(pdev->dev.parent,
1006                                 "Platform data or DT node not supplied\n");
1007                         return -ENODEV;
1008                 }
1009         }
1010
1011         rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
1012         if (!rdata)
1013                 return -ENOMEM;
1014
1015         for (i = 0; i < s2mps11->rdev_num; i++)
1016                 rdata[i].name = regulators[i].name;
1017
1018         ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11);
1019         if (ret)
1020                 goto out;
1021
1022 common_reg:
1023         platform_set_drvdata(pdev, s2mps11);
1024
1025         config.dev = &pdev->dev;
1026         config.regmap = iodev->regmap_pmic;
1027         config.driver_data = s2mps11;
1028         config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
1029         config.ena_gpio_initialized = true;
1030         for (i = 0; i < s2mps11->rdev_num; i++) {
1031                 struct regulator_dev *regulator;
1032
1033                 if (pdata) {
1034                         config.init_data = pdata->regulators[i].initdata;
1035                         config.of_node = pdata->regulators[i].reg_node;
1036                 } else {
1037                         config.init_data = rdata[i].init_data;
1038                         config.of_node = rdata[i].of_node;
1039                 }
1040                 config.ena_gpio = s2mps11->ext_control_gpio[i];
1041
1042                 regulator = devm_regulator_register(&pdev->dev,
1043                                                 &regulators[i], &config);
1044                 if (IS_ERR(regulator)) {
1045                         ret = PTR_ERR(regulator);
1046                         dev_err(&pdev->dev, "regulator init failed for %d\n",
1047                                 i);
1048                         goto out;
1049                 }
1050
1051                 if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
1052                         ret = s2mps14_pmic_enable_ext_control(s2mps11,
1053                                         regulator);
1054                         if (ret < 0) {
1055                                 dev_err(&pdev->dev,
1056                                                 "failed to enable GPIO control over %s: %d\n",
1057                                                 regulator->desc->name, ret);
1058                                 goto out;
1059                         }
1060                 }
1061         }
1062
1063 out:
1064         kfree(rdata);
1065
1066         return ret;
1067 }
1068
1069 static const struct platform_device_id s2mps11_pmic_id[] = {
1070         { "s2mps11-pmic", S2MPS11X},
1071         { "s2mps13-pmic", S2MPS13X},
1072         { "s2mps14-pmic", S2MPS14X},
1073         { "s2mpu02-pmic", S2MPU02},
1074         { },
1075 };
1076 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1077
1078 static struct platform_driver s2mps11_pmic_driver = {
1079         .driver = {
1080                 .name = "s2mps11-pmic",
1081         },
1082         .probe = s2mps11_pmic_probe,
1083         .id_table = s2mps11_pmic_id,
1084 };
1085
1086 static int __init s2mps11_pmic_init(void)
1087 {
1088         return platform_driver_register(&s2mps11_pmic_driver);
1089 }
1090 subsys_initcall(s2mps11_pmic_init);
1091
1092 static void __exit s2mps11_pmic_exit(void)
1093 {
1094         platform_driver_unregister(&s2mps11_pmic_driver);
1095 }
1096 module_exit(s2mps11_pmic_exit);
1097
1098 /* Module information */
1099 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
1100 MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPU02 Regulator Driver");
1101 MODULE_LICENSE("GPL");