]> Pileus Git - ~andy/linux/blob - drivers/regulator/s2mps11.c
Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[~andy/linux] / 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/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/mfd/samsung/core.h>
23 #include <linux/mfd/samsung/s2mps11.h>
24
25 struct s2mps11_info {
26         struct regulator_dev *rdev[S2MPS11_REGULATOR_MAX];
27
28         int ramp_delay2;
29         int ramp_delay34;
30         int ramp_delay5;
31         int ramp_delay16;
32         int ramp_delay7810;
33         int ramp_delay9;
34
35         bool buck6_ramp;
36         bool buck2_ramp;
37         bool buck3_ramp;
38         bool buck4_ramp;
39 };
40
41 static int get_ramp_delay(int ramp_delay)
42 {
43         unsigned char cnt = 0;
44
45         ramp_delay /= 6;
46
47         while (true) {
48                 ramp_delay = ramp_delay >> 1;
49                 if (ramp_delay == 0)
50                         break;
51                 cnt++;
52         }
53         return cnt;
54 }
55
56 static struct regulator_ops s2mps11_ldo_ops = {
57         .list_voltage           = regulator_list_voltage_linear,
58         .map_voltage            = regulator_map_voltage_linear,
59         .is_enabled             = regulator_is_enabled_regmap,
60         .enable                 = regulator_enable_regmap,
61         .disable                = regulator_disable_regmap,
62         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
63         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
64         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
65 };
66
67 static struct regulator_ops s2mps11_buck_ops = {
68         .list_voltage           = regulator_list_voltage_linear,
69         .map_voltage            = regulator_map_voltage_linear,
70         .is_enabled             = regulator_is_enabled_regmap,
71         .enable                 = regulator_enable_regmap,
72         .disable                = regulator_disable_regmap,
73         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
74         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
75         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
76 };
77
78 #define regulator_desc_ldo1(num)        {               \
79         .name           = "LDO"#num,                    \
80         .id             = S2MPS11_LDO##num,             \
81         .ops            = &s2mps11_ldo_ops,             \
82         .type           = REGULATOR_VOLTAGE,            \
83         .owner          = THIS_MODULE,                  \
84         .min_uV         = S2MPS11_LDO_MIN,              \
85         .uV_step        = S2MPS11_LDO_STEP1,            \
86         .n_voltages     = S2MPS11_LDO_N_VOLTAGES,       \
87         .vsel_reg       = S2MPS11_REG_L1CTRL + num - 1, \
88         .vsel_mask      = S2MPS11_LDO_VSEL_MASK,        \
89         .enable_reg     = S2MPS11_REG_L1CTRL + num - 1, \
90         .enable_mask    = S2MPS11_ENABLE_MASK           \
91 }
92 #define regulator_desc_ldo2(num)        {               \
93         .name           = "LDO"#num,                    \
94         .id             = S2MPS11_LDO##num,             \
95         .ops            = &s2mps11_ldo_ops,             \
96         .type           = REGULATOR_VOLTAGE,            \
97         .owner          = THIS_MODULE,                  \
98         .min_uV         = S2MPS11_LDO_MIN,              \
99         .uV_step        = S2MPS11_LDO_STEP2,            \
100         .n_voltages     = S2MPS11_LDO_N_VOLTAGES,       \
101         .vsel_reg       = S2MPS11_REG_L1CTRL + num - 1, \
102         .vsel_mask      = S2MPS11_LDO_VSEL_MASK,        \
103         .enable_reg     = S2MPS11_REG_L1CTRL + num - 1, \
104         .enable_mask    = S2MPS11_ENABLE_MASK           \
105 }
106
107 #define regulator_desc_buck1_4(num)     {                       \
108         .name           = "BUCK"#num,                           \
109         .id             = S2MPS11_BUCK##num,                    \
110         .ops            = &s2mps11_buck_ops,                    \
111         .type           = REGULATOR_VOLTAGE,                    \
112         .owner          = THIS_MODULE,                          \
113         .min_uV         = S2MPS11_BUCK_MIN1,                    \
114         .uV_step        = S2MPS11_BUCK_STEP1,                   \
115         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
116         .vsel_reg       = S2MPS11_REG_B1CTRL2 + (num - 1) * 2,  \
117         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
118         .enable_reg     = S2MPS11_REG_B1CTRL1 + (num - 1) * 2,  \
119         .enable_mask    = S2MPS11_ENABLE_MASK                   \
120 }
121
122 #define regulator_desc_buck5    {                               \
123         .name           = "BUCK5",                              \
124         .id             = S2MPS11_BUCK5,                        \
125         .ops            = &s2mps11_buck_ops,                    \
126         .type           = REGULATOR_VOLTAGE,                    \
127         .owner          = THIS_MODULE,                          \
128         .min_uV         = S2MPS11_BUCK_MIN1,                    \
129         .uV_step        = S2MPS11_BUCK_STEP1,                   \
130         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
131         .vsel_reg       = S2MPS11_REG_B5CTRL2,                  \
132         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
133         .enable_reg     = S2MPS11_REG_B5CTRL1,                  \
134         .enable_mask    = S2MPS11_ENABLE_MASK                   \
135 }
136
137 #define regulator_desc_buck6_8(num)     {                       \
138         .name           = "BUCK"#num,                           \
139         .id             = S2MPS11_BUCK##num,                    \
140         .ops            = &s2mps11_buck_ops,                    \
141         .type           = REGULATOR_VOLTAGE,                    \
142         .owner          = THIS_MODULE,                          \
143         .min_uV         = S2MPS11_BUCK_MIN1,                    \
144         .uV_step        = S2MPS11_BUCK_STEP1,                   \
145         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
146         .vsel_reg       = S2MPS11_REG_B6CTRL2 + (num - 6) * 2,  \
147         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
148         .enable_reg     = S2MPS11_REG_B6CTRL1 + (num - 6) * 2,  \
149         .enable_mask    = S2MPS11_ENABLE_MASK                   \
150 }
151
152 #define regulator_desc_buck9    {                               \
153         .name           = "BUCK9",                              \
154         .id             = S2MPS11_BUCK9,                        \
155         .ops            = &s2mps11_buck_ops,                    \
156         .type           = REGULATOR_VOLTAGE,                    \
157         .owner          = THIS_MODULE,                          \
158         .min_uV         = S2MPS11_BUCK_MIN3,                    \
159         .uV_step        = S2MPS11_BUCK_STEP3,                   \
160         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
161         .vsel_reg       = S2MPS11_REG_B9CTRL2,                  \
162         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
163         .enable_reg     = S2MPS11_REG_B9CTRL1,                  \
164         .enable_mask    = S2MPS11_ENABLE_MASK                   \
165 }
166
167 #define regulator_desc_buck10   {                               \
168         .name           = "BUCK10",                             \
169         .id             = S2MPS11_BUCK10,                       \
170         .ops            = &s2mps11_buck_ops,                    \
171         .type           = REGULATOR_VOLTAGE,                    \
172         .owner          = THIS_MODULE,                          \
173         .min_uV         = S2MPS11_BUCK_MIN2,                    \
174         .uV_step        = S2MPS11_BUCK_STEP2,                   \
175         .n_voltages     = S2MPS11_BUCK_N_VOLTAGES,              \
176         .vsel_reg       = S2MPS11_REG_B10CTRL2,                 \
177         .vsel_mask      = S2MPS11_BUCK_VSEL_MASK,               \
178         .enable_reg     = S2MPS11_REG_B10CTRL1,                 \
179         .enable_mask    = S2MPS11_ENABLE_MASK                   \
180 }
181
182 static struct regulator_desc regulators[] = {
183         regulator_desc_ldo2(1),
184         regulator_desc_ldo1(2),
185         regulator_desc_ldo1(3),
186         regulator_desc_ldo1(4),
187         regulator_desc_ldo1(5),
188         regulator_desc_ldo2(6),
189         regulator_desc_ldo1(7),
190         regulator_desc_ldo1(8),
191         regulator_desc_ldo1(9),
192         regulator_desc_ldo1(10),
193         regulator_desc_ldo2(11),
194         regulator_desc_ldo1(12),
195         regulator_desc_ldo1(13),
196         regulator_desc_ldo1(14),
197         regulator_desc_ldo1(15),
198         regulator_desc_ldo1(16),
199         regulator_desc_ldo1(17),
200         regulator_desc_ldo1(18),
201         regulator_desc_ldo1(19),
202         regulator_desc_ldo1(20),
203         regulator_desc_ldo1(21),
204         regulator_desc_ldo2(22),
205         regulator_desc_ldo2(23),
206         regulator_desc_ldo1(24),
207         regulator_desc_ldo1(25),
208         regulator_desc_ldo1(26),
209         regulator_desc_ldo2(27),
210         regulator_desc_ldo1(28),
211         regulator_desc_ldo1(29),
212         regulator_desc_ldo1(30),
213         regulator_desc_ldo1(31),
214         regulator_desc_ldo1(32),
215         regulator_desc_ldo1(33),
216         regulator_desc_ldo1(34),
217         regulator_desc_ldo1(35),
218         regulator_desc_ldo1(36),
219         regulator_desc_ldo1(37),
220         regulator_desc_ldo1(38),
221         regulator_desc_buck1_4(1),
222         regulator_desc_buck1_4(2),
223         regulator_desc_buck1_4(3),
224         regulator_desc_buck1_4(4),
225         regulator_desc_buck5,
226         regulator_desc_buck6_8(6),
227         regulator_desc_buck6_8(7),
228         regulator_desc_buck6_8(8),
229         regulator_desc_buck9,
230         regulator_desc_buck10,
231 };
232
233 static int s2mps11_pmic_probe(struct platform_device *pdev)
234 {
235         struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
236         struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
237         struct regulator_config config = { };
238         struct s2mps11_info *s2mps11;
239         int i, ret;
240         unsigned char ramp_enable, ramp_reg = 0;
241
242         if (!pdata) {
243                 dev_err(pdev->dev.parent, "Platform data not supplied\n");
244                 return -ENODEV;
245         }
246
247         s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
248                                 GFP_KERNEL);
249         if (!s2mps11)
250                 return -ENOMEM;
251
252         platform_set_drvdata(pdev, s2mps11);
253
254         s2mps11->ramp_delay2 = pdata->buck2_ramp_delay;
255         s2mps11->ramp_delay34 = pdata->buck34_ramp_delay;
256         s2mps11->ramp_delay5 = pdata->buck5_ramp_delay;
257         s2mps11->ramp_delay16 = pdata->buck16_ramp_delay;
258         s2mps11->ramp_delay7810 = pdata->buck7810_ramp_delay;
259         s2mps11->ramp_delay9 = pdata->buck9_ramp_delay;
260
261         s2mps11->buck6_ramp = pdata->buck6_ramp_enable;
262         s2mps11->buck2_ramp = pdata->buck2_ramp_enable;
263         s2mps11->buck3_ramp = pdata->buck3_ramp_enable;
264         s2mps11->buck4_ramp = pdata->buck4_ramp_enable;
265
266         ramp_enable = (s2mps11->buck2_ramp << 3) | (s2mps11->buck3_ramp << 2) |
267                 (s2mps11->buck4_ramp << 1) | s2mps11->buck6_ramp ;
268
269         if (ramp_enable) {
270                 if (s2mps11->buck2_ramp)
271                         ramp_reg |= get_ramp_delay(s2mps11->ramp_delay2) << 6;
272                 if (s2mps11->buck3_ramp || s2mps11->buck4_ramp)
273                         ramp_reg |= get_ramp_delay(s2mps11->ramp_delay34) << 4;
274                 sec_reg_write(iodev, S2MPS11_REG_RAMP, ramp_reg | ramp_enable);
275         }
276
277         ramp_reg &= 0x00;
278         ramp_reg |= get_ramp_delay(s2mps11->ramp_delay5) << 6;
279         ramp_reg |= get_ramp_delay(s2mps11->ramp_delay16) << 4;
280         ramp_reg |= get_ramp_delay(s2mps11->ramp_delay7810) << 2;
281         ramp_reg |= get_ramp_delay(s2mps11->ramp_delay9);
282         sec_reg_write(iodev, S2MPS11_REG_RAMP_BUCK, ramp_reg);
283
284         for (i = 0; i < S2MPS11_REGULATOR_MAX; i++) {
285
286                 config.dev = &pdev->dev;
287                 config.regmap = iodev->regmap;
288                 config.init_data = pdata->regulators[i].initdata;
289                 config.driver_data = s2mps11;
290
291                 s2mps11->rdev[i] = regulator_register(&regulators[i], &config);
292                 if (IS_ERR(s2mps11->rdev[i])) {
293                         ret = PTR_ERR(s2mps11->rdev[i]);
294                         dev_err(&pdev->dev, "regulator init failed for %d\n",
295                                 i);
296                         s2mps11->rdev[i] = NULL;
297                         goto err;
298                 }
299         }
300
301         return 0;
302 err:
303         for (i = 0; i < S2MPS11_REGULATOR_MAX; i++)
304                 regulator_unregister(s2mps11->rdev[i]);
305
306         return ret;
307 }
308
309 static int s2mps11_pmic_remove(struct platform_device *pdev)
310 {
311         struct s2mps11_info *s2mps11 = platform_get_drvdata(pdev);
312         int i;
313
314         for (i = 0; i < S2MPS11_REGULATOR_MAX; i++)
315                 regulator_unregister(s2mps11->rdev[i]);
316
317         return 0;
318 }
319
320 static const struct platform_device_id s2mps11_pmic_id[] = {
321         { "s2mps11-pmic", 0},
322         { },
323 };
324 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
325
326 static struct platform_driver s2mps11_pmic_driver = {
327         .driver = {
328                 .name = "s2mps11-pmic",
329                 .owner = THIS_MODULE,
330         },
331         .probe = s2mps11_pmic_probe,
332         .remove = s2mps11_pmic_remove,
333         .id_table = s2mps11_pmic_id,
334 };
335
336 static int __init s2mps11_pmic_init(void)
337 {
338         return platform_driver_register(&s2mps11_pmic_driver);
339 }
340 subsys_initcall(s2mps11_pmic_init);
341
342 static void __exit s2mps11_pmic_exit(void)
343 {
344         platform_driver_unregister(&s2mps11_pmic_driver);
345 }
346 module_exit(s2mps11_pmic_exit);
347
348 /* Module information */
349 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
350 MODULE_DESCRIPTION("SAMSUNG S2MPS11 Regulator Driver");
351 MODULE_LICENSE("GPL");