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