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