189038aecbf5672b03e6f82a186092cc55c28b38
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / s2mps11.c
1 /*
2  * s2mps11.c
3  *
4  * Copyright (c) 2012 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  */
13
14 #include <linux/bug.h>
15 #include <linux/err.h>
16 #include <linux/gpio.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/regmap.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/mfd/samsung/core.h>
26 #include <linux/mfd/samsung/s2mps11.h>
27
28 #define S2MPS11_REGULATOR_CNT ARRAY_SIZE(regulators)
29
30 struct s2mps11_info {
31         int ramp_delay2;
32         int ramp_delay34;
33         int ramp_delay5;
34         int ramp_delay16;
35         int ramp_delay7810;
36         int ramp_delay9;
37 };
38
39 static int get_ramp_delay(int ramp_delay)
40 {
41         unsigned char cnt = 0;
42
43         ramp_delay /= 6250;
44
45         while (true) {
46                 ramp_delay = ramp_delay >> 1;
47                 if (ramp_delay == 0)
48                         break;
49                 cnt++;
50         }
51
52         if (cnt > 3)
53                 cnt = 3;
54
55         return cnt;
56 }
57
58 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
59                                    unsigned int old_selector,
60                                    unsigned int new_selector)
61 {
62         struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
63         unsigned int ramp_delay = 0;
64         int old_volt, new_volt;
65
66         switch (rdev->desc->id) {
67         case S2MPS11_BUCK2:
68                 ramp_delay = s2mps11->ramp_delay2;
69                 break;
70         case S2MPS11_BUCK3:
71         case S2MPS11_BUCK4:
72                 ramp_delay = s2mps11->ramp_delay34;
73                 break;
74         case S2MPS11_BUCK5:
75                 ramp_delay = s2mps11->ramp_delay5;
76                 break;
77         case S2MPS11_BUCK6:
78         case S2MPS11_BUCK1:
79                 ramp_delay = s2mps11->ramp_delay16;
80                 break;
81         case S2MPS11_BUCK7:
82         case S2MPS11_BUCK8:
83         case S2MPS11_BUCK10:
84                 ramp_delay = s2mps11->ramp_delay7810;
85                 break;
86         case S2MPS11_BUCK9:
87                 ramp_delay = s2mps11->ramp_delay9;
88         }
89
90         if (ramp_delay == 0)
91                 ramp_delay = rdev->desc->ramp_delay;
92
93         old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
94         new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
95
96         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
97 }
98
99 static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
100 {
101         struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
102         unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
103         unsigned int ramp_enable = 1, enable_shift = 0;
104         int ret;
105
106         switch (rdev->desc->id) {
107         case S2MPS11_BUCK1:
108                 if (ramp_delay > s2mps11->ramp_delay16)
109                         s2mps11->ramp_delay16 = ramp_delay;
110                 else
111                         ramp_delay = s2mps11->ramp_delay16;
112
113                 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
114                 break;
115         case S2MPS11_BUCK2:
116                 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
117                 if (!ramp_delay) {
118                         ramp_enable = 0;
119                         break;
120                 }
121
122                 s2mps11->ramp_delay2 = ramp_delay;
123                 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
124                 ramp_reg = S2MPS11_REG_RAMP;
125                 break;
126         case S2MPS11_BUCK3:
127                 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
128                 if (!ramp_delay) {
129                         ramp_enable = 0;
130                         break;
131                 }
132
133                 if (ramp_delay > s2mps11->ramp_delay34)
134                         s2mps11->ramp_delay34 = ramp_delay;
135                 else
136                         ramp_delay = s2mps11->ramp_delay34;
137
138                 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
139                 ramp_reg = S2MPS11_REG_RAMP;
140                 break;
141         case S2MPS11_BUCK4:
142                 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
143                 if (!ramp_delay) {
144                         ramp_enable = 0;
145                         break;
146                 }
147
148                 if (ramp_delay > s2mps11->ramp_delay34)
149                         s2mps11->ramp_delay34 = ramp_delay;
150                 else
151                         ramp_delay = s2mps11->ramp_delay34;
152
153                 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
154                 ramp_reg = S2MPS11_REG_RAMP;
155                 break;
156         case S2MPS11_BUCK5:
157                 s2mps11->ramp_delay5 = ramp_delay;
158                 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
159                 break;
160         case S2MPS11_BUCK6:
161                 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
162                 if (!ramp_delay) {
163                         ramp_enable = 0;
164                         break;
165                 }
166
167                 if (ramp_delay > s2mps11->ramp_delay16)
168                         s2mps11->ramp_delay16 = ramp_delay;
169                 else
170                         ramp_delay = s2mps11->ramp_delay16;
171
172                 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
173                 break;
174         case S2MPS11_BUCK7:
175         case S2MPS11_BUCK8:
176         case S2MPS11_BUCK10:
177                 if (ramp_delay > s2mps11->ramp_delay7810)
178                         s2mps11->ramp_delay7810 = ramp_delay;
179                 else
180                         ramp_delay = s2mps11->ramp_delay7810;
181
182                 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
183                 break;
184         case S2MPS11_BUCK9:
185                 s2mps11->ramp_delay9 = ramp_delay;
186                 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
187                 break;
188         default:
189                 return 0;
190         }
191
192         if (!ramp_enable)
193                 goto ramp_disable;
194
195         if (enable_shift) {
196                 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
197                                         1 << enable_shift, 1 << enable_shift);
198                 if (ret) {
199                         dev_err(&rdev->dev, "failed to enable ramp rate\n");
200                         return ret;
201                 }
202         }
203
204         ramp_val = get_ramp_delay(ramp_delay);
205
206         return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
207                                   ramp_val << ramp_shift);
208
209 ramp_disable:
210         return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
211                                   1 << enable_shift, 0);
212 }
213
214 static struct regulator_ops s2mps11_ldo_ops = {
215         .list_voltage           = regulator_list_voltage_linear,
216         .map_voltage            = regulator_map_voltage_linear,
217         .is_enabled             = regulator_is_enabled_regmap,
218         .enable                 = regulator_enable_regmap,
219         .disable                = regulator_disable_regmap,
220         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
221         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
222         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
223 };
224
225 static struct regulator_ops s2mps11_buck_ops = {
226         .list_voltage           = regulator_list_voltage_linear,
227         .map_voltage            = regulator_map_voltage_linear,
228         .is_enabled             = regulator_is_enabled_regmap,
229         .enable                 = regulator_enable_regmap,
230         .disable                = regulator_disable_regmap,
231         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
232         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
233         .set_voltage_time_sel   = s2mps11_regulator_set_voltage_time_sel,
234         .set_ramp_delay         = s2mps11_set_ramp_delay,
235 };
236
237 #define regulator_desc_ldo1(num)        {               \
238         .name           = "LDO"#num,                    \
239         .id             = S2MPS11_LDO##num,             \
240         .ops            = &s2mps11_ldo_ops,             \
241         .type           = REGULATOR_VOLTAGE,            \
242         .owner          = THIS_MODULE,                  \
243         .min_uV         = S2MPS11_LDO_MIN,              \
244         .uV_step        = S2MPS11_LDO_STEP1,            \
245         .n_voltages     = S2MPS11_LDO_N_VOLTAGES,       \
246         .vsel_reg       = S2MPS11_REG_L1CTRL + num - 1, \
247         .vsel_mask      = S2MPS11_LDO_VSEL_MASK,        \
248         .enable_reg     = S2MPS11_REG_L1CTRL + num - 1, \
249         .enable_mask    = S2MPS11_ENABLE_MASK           \
250 }
251 #define regulator_desc_ldo2(num)        {               \
252         .name           = "LDO"#num,                    \
253         .id             = S2MPS11_LDO##num,             \
254         .ops            = &s2mps11_ldo_ops,             \
255         .type           = REGULATOR_VOLTAGE,            \
256         .owner          = THIS_MODULE,                  \
257         .min_uV         = S2MPS11_LDO_MIN,              \
258         .uV_step        = S2MPS11_LDO_STEP2,            \
259         .n_voltages     = S2MPS11_LDO_N_VOLTAGES,       \
260         .vsel_reg       = S2MPS11_REG_L1CTRL + num - 1, \
261         .vsel_mask      = S2MPS11_LDO_VSEL_MASK,        \
262         .enable_reg     = S2MPS11_REG_L1CTRL + num - 1, \
263         .enable_mask    = S2MPS11_ENABLE_MASK           \
264 }
265
266 #define regulator_desc_buck1_4(num)     {                       \
267         .name           = "BUCK"#num,                           \
268         .id             = S2MPS11_BUCK##num,                    \
269         .ops            = &s2mps11_buck_ops,                    \
270         .type           = REGULATOR_VOLTAGE,                    \
271         .owner          = THIS_MODULE,                          \
272         .min_uV         = S2MPS11_BUCK_MIN1,                    \
273         .uV_step        = S2MPS11_BUCK_STEP1,                   \
274         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
275         .ramp_delay     = S2MPS11_RAMP_DELAY,                   \
276         .vsel_reg       = S2MPS11_REG_B1CTRL2 + (num - 1) * 2,  \
277         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
278         .enable_reg     = S2MPS11_REG_B1CTRL1 + (num - 1) * 2,  \
279         .enable_mask    = S2MPS11_ENABLE_MASK                   \
280 }
281
282 #define regulator_desc_buck5    {                               \
283         .name           = "BUCK5",                              \
284         .id             = S2MPS11_BUCK5,                        \
285         .ops            = &s2mps11_buck_ops,                    \
286         .type           = REGULATOR_VOLTAGE,                    \
287         .owner          = THIS_MODULE,                          \
288         .min_uV         = S2MPS11_BUCK_MIN1,                    \
289         .uV_step        = S2MPS11_BUCK_STEP1,                   \
290         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
291         .ramp_delay     = S2MPS11_RAMP_DELAY,                   \
292         .vsel_reg       = S2MPS11_REG_B5CTRL2,                  \
293         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
294         .enable_reg     = S2MPS11_REG_B5CTRL1,                  \
295         .enable_mask    = S2MPS11_ENABLE_MASK                   \
296 }
297
298 #define regulator_desc_buck6_8(num)     {                       \
299         .name           = "BUCK"#num,                           \
300         .id             = S2MPS11_BUCK##num,                    \
301         .ops            = &s2mps11_buck_ops,                    \
302         .type           = REGULATOR_VOLTAGE,                    \
303         .owner          = THIS_MODULE,                          \
304         .min_uV         = S2MPS11_BUCK_MIN1,                    \
305         .uV_step        = S2MPS11_BUCK_STEP1,                   \
306         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
307         .ramp_delay     = S2MPS11_RAMP_DELAY,                   \
308         .vsel_reg       = S2MPS11_REG_B6CTRL2 + (num - 6) * 2,  \
309         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
310         .enable_reg     = S2MPS11_REG_B6CTRL1 + (num - 6) * 2,  \
311         .enable_mask    = S2MPS11_ENABLE_MASK                   \
312 }
313
314 #define regulator_desc_buck9    {                               \
315         .name           = "BUCK9",                              \
316         .id             = S2MPS11_BUCK9,                        \
317         .ops            = &s2mps11_buck_ops,                    \
318         .type           = REGULATOR_VOLTAGE,                    \
319         .owner          = THIS_MODULE,                          \
320         .min_uV         = S2MPS11_BUCK_MIN3,                    \
321         .uV_step        = S2MPS11_BUCK_STEP3,                   \
322         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
323         .ramp_delay     = S2MPS11_RAMP_DELAY,                   \
324         .vsel_reg       = S2MPS11_REG_B9CTRL2,                  \
325         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
326         .enable_reg     = S2MPS11_REG_B9CTRL1,                  \
327         .enable_mask    = S2MPS11_ENABLE_MASK                   \
328 }
329
330 #define regulator_desc_buck10   {                               \
331         .name           = "BUCK10",                             \
332         .id             = S2MPS11_BUCK10,                       \
333         .ops            = &s2mps11_buck_ops,                    \
334         .type           = REGULATOR_VOLTAGE,                    \
335         .owner          = THIS_MODULE,                          \
336         .min_uV         = S2MPS11_BUCK_MIN2,                    \
337         .uV_step        = S2MPS11_BUCK_STEP2,                   \
338         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
339         .ramp_delay     = S2MPS11_RAMP_DELAY,                   \
340         .vsel_reg       = S2MPS11_REG_B10CTRL2,                 \
341         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
342         .enable_reg     = S2MPS11_REG_B10CTRL1,                 \
343         .enable_mask    = S2MPS11_ENABLE_MASK                   \
344 }
345
346 static const struct regulator_desc regulators[] = {
347         regulator_desc_ldo2(1),
348         regulator_desc_ldo1(2),
349         regulator_desc_ldo1(3),
350         regulator_desc_ldo1(4),
351         regulator_desc_ldo1(5),
352         regulator_desc_ldo2(6),
353         regulator_desc_ldo1(7),
354         regulator_desc_ldo1(8),
355         regulator_desc_ldo1(9),
356         regulator_desc_ldo1(10),
357         regulator_desc_ldo2(11),
358         regulator_desc_ldo1(12),
359         regulator_desc_ldo1(13),
360         regulator_desc_ldo1(14),
361         regulator_desc_ldo1(15),
362         regulator_desc_ldo1(16),
363         regulator_desc_ldo1(17),
364         regulator_desc_ldo1(18),
365         regulator_desc_ldo1(19),
366         regulator_desc_ldo1(20),
367         regulator_desc_ldo1(21),
368         regulator_desc_ldo2(22),
369         regulator_desc_ldo2(23),
370         regulator_desc_ldo1(24),
371         regulator_desc_ldo1(25),
372         regulator_desc_ldo1(26),
373         regulator_desc_ldo2(27),
374         regulator_desc_ldo1(28),
375         regulator_desc_ldo1(29),
376         regulator_desc_ldo1(30),
377         regulator_desc_ldo1(31),
378         regulator_desc_ldo1(32),
379         regulator_desc_ldo1(33),
380         regulator_desc_ldo1(34),
381         regulator_desc_ldo1(35),
382         regulator_desc_ldo1(36),
383         regulator_desc_ldo1(37),
384         regulator_desc_ldo1(38),
385         regulator_desc_buck1_4(1),
386         regulator_desc_buck1_4(2),
387         regulator_desc_buck1_4(3),
388         regulator_desc_buck1_4(4),
389         regulator_desc_buck5,
390         regulator_desc_buck6_8(6),
391         regulator_desc_buck6_8(7),
392         regulator_desc_buck6_8(8),
393         regulator_desc_buck9,
394         regulator_desc_buck10,
395 };
396
397 static int s2mps11_pmic_probe(struct platform_device *pdev)
398 {
399         struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
400         struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
401         struct of_regulator_match rdata[S2MPS11_REGULATOR_MAX];
402         struct device_node *reg_np = NULL;
403         struct regulator_config config = { };
404         struct s2mps11_info *s2mps11;
405         int i, ret;
406
407         s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
408                                 GFP_KERNEL);
409         if (!s2mps11)
410                 return -ENOMEM;
411
412         if (!iodev->dev->of_node) {
413                 if (pdata) {
414                         goto common_reg;
415                 } else {
416                         dev_err(pdev->dev.parent,
417                                 "Platform data or DT node not supplied\n");
418                         return -ENODEV;
419                 }
420         }
421
422         for (i = 0; i < S2MPS11_REGULATOR_CNT; i++)
423                 rdata[i].name = regulators[i].name;
424
425         reg_np = of_find_node_by_name(iodev->dev->of_node, "regulators");
426         if (!reg_np) {
427                 dev_err(&pdev->dev, "could not find regulators sub-node\n");
428                 return -EINVAL;
429         }
430
431         of_regulator_match(&pdev->dev, reg_np, rdata, S2MPS11_REGULATOR_MAX);
432
433 common_reg:
434         platform_set_drvdata(pdev, s2mps11);
435
436         config.dev = &pdev->dev;
437         config.regmap = iodev->regmap_pmic;
438         config.driver_data = s2mps11;
439         for (i = 0; i < S2MPS11_REGULATOR_MAX; i++) {
440                 struct regulator_dev *regulator;
441
442                 if (!reg_np) {
443                         config.init_data = pdata->regulators[i].initdata;
444                         config.of_node = pdata->regulators[i].reg_node;
445                 } else {
446                         config.init_data = rdata[i].init_data;
447                         config.of_node = rdata[i].of_node;
448                 }
449
450                 regulator = devm_regulator_register(&pdev->dev,
451                                                 &regulators[i], &config);
452                 if (IS_ERR(regulator)) {
453                         ret = PTR_ERR(regulator);
454                         dev_err(&pdev->dev, "regulator init failed for %d\n",
455                                 i);
456                         return ret;
457                 }
458         }
459
460         return 0;
461 }
462
463 static const struct platform_device_id s2mps11_pmic_id[] = {
464         { "s2mps11-pmic", 0},
465         { },
466 };
467 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
468
469 static struct platform_driver s2mps11_pmic_driver = {
470         .driver = {
471                 .name = "s2mps11-pmic",
472                 .owner = THIS_MODULE,
473         },
474         .probe = s2mps11_pmic_probe,
475         .id_table = s2mps11_pmic_id,
476 };
477
478 static int __init s2mps11_pmic_init(void)
479 {
480         return platform_driver_register(&s2mps11_pmic_driver);
481 }
482 subsys_initcall(s2mps11_pmic_init);
483
484 static void __exit s2mps11_pmic_exit(void)
485 {
486         platform_driver_unregister(&s2mps11_pmic_driver);
487 }
488 module_exit(s2mps11_pmic_exit);
489
490 /* Module information */
491 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
492 MODULE_DESCRIPTION("SAMSUNG S2MPS11 Regulator Driver");
493 MODULE_LICENSE("GPL");