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