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