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