]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/regulator/s2mpa01.c
Merge tag 'v3.15' into next
[karo-tx-linux.git] / drivers / regulator / s2mpa01.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd
3  *              http://www.samsung.com
4  *
5  *  This program is free software; you can redistribute  it and/or modify it
6  *  under  the terms of  the GNU General  Public License as published by the
7  *  Free Software Foundation;  either version 2 of the  License, or (at your
8  *  option) any later version.
9  *
10  */
11
12 #include <linux/bug.h>
13 #include <linux/err.h>
14 #include <linux/gpio.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/regmap.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/mfd/samsung/core.h>
24 #include <linux/mfd/samsung/s2mpa01.h>
25
26 #define S2MPA01_REGULATOR_CNT ARRAY_SIZE(regulators)
27
28 struct s2mpa01_info {
29         int ramp_delay24;
30         int ramp_delay3;
31         int ramp_delay5;
32         int ramp_delay16;
33         int ramp_delay7;
34         int ramp_delay8910;
35 };
36
37 static int get_ramp_delay(int ramp_delay)
38 {
39         unsigned char cnt = 0;
40
41         ramp_delay /= 6250;
42
43         while (true) {
44                 ramp_delay = ramp_delay >> 1;
45                 if (ramp_delay == 0)
46                         break;
47                 cnt++;
48         }
49
50         if (cnt > 3)
51                 cnt = 3;
52
53         return cnt;
54 }
55
56 static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
57                                    unsigned int old_selector,
58                                    unsigned int new_selector)
59 {
60         struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
61         unsigned int ramp_delay = 0;
62         int old_volt, new_volt;
63
64         switch (rdev->desc->id) {
65         case S2MPA01_BUCK2:
66         case S2MPA01_BUCK4:
67                 ramp_delay = s2mpa01->ramp_delay24;
68                 break;
69         case S2MPA01_BUCK3:
70                 ramp_delay = s2mpa01->ramp_delay3;
71                 break;
72         case S2MPA01_BUCK5:
73                 ramp_delay = s2mpa01->ramp_delay5;
74                 break;
75         case S2MPA01_BUCK1:
76         case S2MPA01_BUCK6:
77                 ramp_delay = s2mpa01->ramp_delay16;
78                 break;
79         case S2MPA01_BUCK7:
80                 ramp_delay = s2mpa01->ramp_delay7;
81                 break;
82         case S2MPA01_BUCK8:
83         case S2MPA01_BUCK9:
84         case S2MPA01_BUCK10:
85                 ramp_delay = s2mpa01->ramp_delay8910;
86                 break;
87         }
88
89         if (ramp_delay == 0)
90                 ramp_delay = rdev->desc->ramp_delay;
91
92         old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
93         new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
94
95         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
96 }
97
98 static int s2mpa01_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
99 {
100         struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
101         unsigned int ramp_val, ramp_shift, ramp_reg = S2MPA01_REG_RAMP2;
102         unsigned int ramp_enable = 1, enable_shift = 0;
103         int ret;
104
105         switch (rdev->desc->id) {
106         case S2MPA01_BUCK1:
107                 enable_shift = S2MPA01_BUCK1_RAMP_EN_SHIFT;
108                 if (!ramp_delay) {
109                         ramp_enable = 0;
110                         break;
111                 }
112
113                 if (ramp_delay > s2mpa01->ramp_delay16)
114                         s2mpa01->ramp_delay16 = ramp_delay;
115                 else
116                         ramp_delay = s2mpa01->ramp_delay16;
117
118                 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
119                 ramp_reg = S2MPA01_REG_RAMP1;
120                 break;
121         case S2MPA01_BUCK2:
122                 enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
123                 if (!ramp_delay) {
124                         ramp_enable = 0;
125                         break;
126                 }
127
128                 if (ramp_delay > s2mpa01->ramp_delay24)
129                         s2mpa01->ramp_delay24 = ramp_delay;
130                 else
131                         ramp_delay = s2mpa01->ramp_delay24;
132
133                 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
134                 ramp_reg = S2MPA01_REG_RAMP1;
135                 break;
136         case S2MPA01_BUCK3:
137                 enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
138                 if (!ramp_delay) {
139                         ramp_enable = 0;
140                         break;
141                 }
142
143                 s2mpa01->ramp_delay3 = ramp_delay;
144                 ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
145                 ramp_reg = S2MPA01_REG_RAMP1;
146                 break;
147         case S2MPA01_BUCK4:
148                 enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
149                 if (!ramp_delay) {
150                         ramp_enable = 0;
151                         break;
152                 }
153
154                 if (ramp_delay > s2mpa01->ramp_delay24)
155                         s2mpa01->ramp_delay24 = ramp_delay;
156                 else
157                         ramp_delay = s2mpa01->ramp_delay24;
158
159                 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
160                 ramp_reg = S2MPA01_REG_RAMP1;
161                 break;
162         case S2MPA01_BUCK5:
163                 s2mpa01->ramp_delay5 = ramp_delay;
164                 ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
165                 break;
166         case S2MPA01_BUCK6:
167                 if (ramp_delay > s2mpa01->ramp_delay16)
168                         s2mpa01->ramp_delay16 = ramp_delay;
169                 else
170                         ramp_delay = s2mpa01->ramp_delay16;
171
172                 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
173                 break;
174         case S2MPA01_BUCK7:
175                 s2mpa01->ramp_delay7 = ramp_delay;
176                 ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
177                 break;
178         case S2MPA01_BUCK8:
179         case S2MPA01_BUCK9:
180         case S2MPA01_BUCK10:
181                 if (ramp_delay > s2mpa01->ramp_delay8910)
182                         s2mpa01->ramp_delay8910 = ramp_delay;
183                 else
184                         ramp_delay = s2mpa01->ramp_delay8910;
185
186                 ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
187                 break;
188         default:
189                 return 0;
190         }
191
192         if (!ramp_enable)
193                 goto ramp_disable;
194
195         ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
196                                  1 << enable_shift, 1 << enable_shift);
197         if (ret) {
198                 dev_err(&rdev->dev, "failed to enable ramp rate\n");
199                 return ret;
200         }
201
202         ramp_val = get_ramp_delay(ramp_delay);
203
204         return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
205                                   ramp_val << ramp_shift);
206
207 ramp_disable:
208         return regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
209                                   1 << enable_shift, 0);
210 }
211
212 static struct regulator_ops s2mpa01_ldo_ops = {
213         .list_voltage           = regulator_list_voltage_linear,
214         .map_voltage            = regulator_map_voltage_linear,
215         .is_enabled             = regulator_is_enabled_regmap,
216         .enable                 = regulator_enable_regmap,
217         .disable                = regulator_disable_regmap,
218         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
219         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
220         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
221 };
222
223 static struct regulator_ops s2mpa01_buck_ops = {
224         .list_voltage           = regulator_list_voltage_linear,
225         .map_voltage            = regulator_map_voltage_linear,
226         .is_enabled             = regulator_is_enabled_regmap,
227         .enable                 = regulator_enable_regmap,
228         .disable                = regulator_disable_regmap,
229         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
230         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
231         .set_voltage_time_sel   = s2mpa01_regulator_set_voltage_time_sel,
232         .set_ramp_delay         = s2mpa01_set_ramp_delay,
233 };
234
235 #define regulator_desc_ldo1(num)        {               \
236         .name           = "LDO"#num,                    \
237         .id             = S2MPA01_LDO##num,             \
238         .ops            = &s2mpa01_ldo_ops,             \
239         .type           = REGULATOR_VOLTAGE,            \
240         .owner          = THIS_MODULE,                  \
241         .min_uV         = S2MPA01_LDO_MIN,              \
242         .uV_step        = S2MPA01_LDO_STEP1,            \
243         .n_voltages     = S2MPA01_LDO_N_VOLTAGES,       \
244         .vsel_reg       = S2MPA01_REG_L1CTRL + num - 1, \
245         .vsel_mask      = S2MPA01_LDO_VSEL_MASK,        \
246         .enable_reg     = S2MPA01_REG_L1CTRL + num - 1, \
247         .enable_mask    = S2MPA01_ENABLE_MASK           \
248 }
249 #define regulator_desc_ldo2(num)        {               \
250         .name           = "LDO"#num,                    \
251         .id             = S2MPA01_LDO##num,             \
252         .ops            = &s2mpa01_ldo_ops,             \
253         .type           = REGULATOR_VOLTAGE,            \
254         .owner          = THIS_MODULE,                  \
255         .min_uV         = S2MPA01_LDO_MIN,              \
256         .uV_step        = S2MPA01_LDO_STEP2,            \
257         .n_voltages     = S2MPA01_LDO_N_VOLTAGES,       \
258         .vsel_reg       = S2MPA01_REG_L1CTRL + num - 1, \
259         .vsel_mask      = S2MPA01_LDO_VSEL_MASK,        \
260         .enable_reg     = S2MPA01_REG_L1CTRL + num - 1, \
261         .enable_mask    = S2MPA01_ENABLE_MASK           \
262 }
263
264 #define regulator_desc_buck1_4(num)     {                       \
265         .name           = "BUCK"#num,                           \
266         .id             = S2MPA01_BUCK##num,                    \
267         .ops            = &s2mpa01_buck_ops,                    \
268         .type           = REGULATOR_VOLTAGE,                    \
269         .owner          = THIS_MODULE,                          \
270         .min_uV         = S2MPA01_BUCK_MIN1,                    \
271         .uV_step        = S2MPA01_BUCK_STEP1,                   \
272         .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
273         .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
274         .vsel_reg       = S2MPA01_REG_B1CTRL2 + (num - 1) * 2,  \
275         .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
276         .enable_reg     = S2MPA01_REG_B1CTRL1 + (num - 1) * 2,  \
277         .enable_mask    = S2MPA01_ENABLE_MASK                   \
278 }
279
280 #define regulator_desc_buck5    {                               \
281         .name           = "BUCK5",                              \
282         .id             = S2MPA01_BUCK5,                        \
283         .ops            = &s2mpa01_buck_ops,                    \
284         .type           = REGULATOR_VOLTAGE,                    \
285         .owner          = THIS_MODULE,                          \
286         .min_uV         = S2MPA01_BUCK_MIN2,                    \
287         .uV_step        = S2MPA01_BUCK_STEP1,                   \
288         .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
289         .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
290         .vsel_reg       = S2MPA01_REG_B5CTRL2,                  \
291         .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
292         .enable_reg     = S2MPA01_REG_B5CTRL1,                  \
293         .enable_mask    = S2MPA01_ENABLE_MASK                   \
294 }
295
296 #define regulator_desc_buck6_7(num)     {                       \
297         .name           = "BUCK"#num,                           \
298         .id             = S2MPA01_BUCK##num,                    \
299         .ops            = &s2mpa01_buck_ops,                    \
300         .type           = REGULATOR_VOLTAGE,                    \
301         .owner          = THIS_MODULE,                          \
302         .min_uV         = S2MPA01_BUCK_MIN1,                    \
303         .uV_step        = S2MPA01_BUCK_STEP1,                   \
304         .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
305         .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
306         .vsel_reg       = S2MPA01_REG_B6CTRL2 + (num - 6) * 2,  \
307         .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
308         .enable_reg     = S2MPA01_REG_B6CTRL1 + (num - 6) * 2,  \
309         .enable_mask    = S2MPA01_ENABLE_MASK                   \
310 }
311
312 #define regulator_desc_buck8    {                               \
313         .name           = "BUCK8",                              \
314         .id             = S2MPA01_BUCK8,                        \
315         .ops            = &s2mpa01_buck_ops,                    \
316         .type           = REGULATOR_VOLTAGE,                    \
317         .owner          = THIS_MODULE,                          \
318         .min_uV         = S2MPA01_BUCK_MIN2,                    \
319         .uV_step        = S2MPA01_BUCK_STEP2,                   \
320         .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
321         .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
322         .vsel_reg       = S2MPA01_REG_B8CTRL2,                  \
323         .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
324         .enable_reg     = S2MPA01_REG_B8CTRL1,                  \
325         .enable_mask    = S2MPA01_ENABLE_MASK                   \
326 }
327
328 #define regulator_desc_buck9    {                               \
329         .name           = "BUCK9",                              \
330         .id             = S2MPA01_BUCK9,                        \
331         .ops            = &s2mpa01_buck_ops,                    \
332         .type           = REGULATOR_VOLTAGE,                    \
333         .owner          = THIS_MODULE,                          \
334         .min_uV         = S2MPA01_BUCK_MIN4,                    \
335         .uV_step        = S2MPA01_BUCK_STEP2,                   \
336         .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
337         .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
338         .vsel_reg       = S2MPA01_REG_B9CTRL2,                  \
339         .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
340         .enable_reg     = S2MPA01_REG_B9CTRL1,                  \
341         .enable_mask    = S2MPA01_ENABLE_MASK                   \
342 }
343
344 #define regulator_desc_buck10   {                               \
345         .name           = "BUCK10",                             \
346         .id             = S2MPA01_BUCK10,                       \
347         .ops            = &s2mpa01_buck_ops,                    \
348         .type           = REGULATOR_VOLTAGE,                    \
349         .owner          = THIS_MODULE,                          \
350         .min_uV         = S2MPA01_BUCK_MIN3,                    \
351         .uV_step        = S2MPA01_BUCK_STEP2,                   \
352         .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
353         .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
354         .vsel_reg       = S2MPA01_REG_B10CTRL2,                 \
355         .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
356         .enable_reg     = S2MPA01_REG_B10CTRL1,                 \
357         .enable_mask    = S2MPA01_ENABLE_MASK                   \
358 }
359
360 static struct regulator_desc regulators[] = {
361         regulator_desc_ldo2(1),
362         regulator_desc_ldo1(2),
363         regulator_desc_ldo1(3),
364         regulator_desc_ldo1(4),
365         regulator_desc_ldo1(5),
366         regulator_desc_ldo2(6),
367         regulator_desc_ldo1(7),
368         regulator_desc_ldo1(8),
369         regulator_desc_ldo1(9),
370         regulator_desc_ldo1(10),
371         regulator_desc_ldo2(11),
372         regulator_desc_ldo1(12),
373         regulator_desc_ldo1(13),
374         regulator_desc_ldo1(14),
375         regulator_desc_ldo1(15),
376         regulator_desc_ldo1(16),
377         regulator_desc_ldo1(17),
378         regulator_desc_ldo1(18),
379         regulator_desc_ldo1(19),
380         regulator_desc_ldo1(20),
381         regulator_desc_ldo1(21),
382         regulator_desc_ldo2(22),
383         regulator_desc_ldo2(23),
384         regulator_desc_ldo1(24),
385         regulator_desc_ldo1(25),
386         regulator_desc_ldo1(26),
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_7(6),
393         regulator_desc_buck6_7(7),
394         regulator_desc_buck8,
395         regulator_desc_buck9,
396         regulator_desc_buck10,
397 };
398
399 static int s2mpa01_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[S2MPA01_REGULATOR_MAX];
404         struct device_node *reg_np = NULL;
405         struct regulator_config config = { };
406         struct s2mpa01_info *s2mpa01;
407         int i;
408
409         s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
410         if (!s2mpa01)
411                 return -ENOMEM;
412
413         for (i = 0; i < S2MPA01_REGULATOR_CNT; i++)
414                 rdata[i].name = regulators[i].name;
415
416         if (iodev->dev->of_node) {
417                 reg_np = of_get_child_by_name(iodev->dev->of_node,
418                                                         "regulators");
419                         if (!reg_np) {
420                                 dev_err(&pdev->dev,
421                                         "could not find regulators sub-node\n");
422                                 return -EINVAL;
423                         }
424
425                 of_regulator_match(&pdev->dev, reg_np, rdata,
426                                                 S2MPA01_REGULATOR_MAX);
427                 of_node_put(reg_np);
428         }
429
430         platform_set_drvdata(pdev, s2mpa01);
431
432         config.dev = &pdev->dev;
433         config.regmap = iodev->regmap_pmic;
434         config.driver_data = s2mpa01;
435
436         for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
437                 struct regulator_dev *rdev;
438                 if (pdata)
439                         config.init_data = pdata->regulators[i].initdata;
440                 else
441                         config.init_data = rdata[i].init_data;
442
443                 if (reg_np)
444                         config.of_node = rdata[i].of_node;
445
446                 rdev = devm_regulator_register(&pdev->dev,
447                                                 &regulators[i], &config);
448                 if (IS_ERR(rdev)) {
449                         dev_err(&pdev->dev, "regulator init failed for %d\n",
450                                 i);
451                         return PTR_ERR(rdev);
452                 }
453         }
454
455         return 0;
456 }
457
458 static const struct platform_device_id s2mpa01_pmic_id[] = {
459         { "s2mpa01-pmic", 0},
460         { },
461 };
462 MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
463
464 static struct platform_driver s2mpa01_pmic_driver = {
465         .driver = {
466                 .name = "s2mpa01-pmic",
467                 .owner = THIS_MODULE,
468         },
469         .probe = s2mpa01_pmic_probe,
470         .id_table = s2mpa01_pmic_id,
471 };
472
473 module_platform_driver(s2mpa01_pmic_driver);
474
475 /* Module information */
476 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
477 MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
478 MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
479 MODULE_LICENSE("GPL");