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