4 * Copyright (c) 2012 Samsung Electronics Co., Ltd
5 * http://www.samsung.com
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.
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>
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>
28 #define S2MPS11_REGULATOR_CNT ARRAY_SIZE(regulators)
31 struct regulator_dev
*rdev
[S2MPS11_REGULATOR_MAX
];
41 static int get_ramp_delay(int ramp_delay
)
43 unsigned char cnt
= 0;
48 ramp_delay
= ramp_delay
>> 1;
60 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
61 unsigned int old_selector
,
62 unsigned int new_selector
)
64 struct s2mps11_info
*s2mps11
= rdev_get_drvdata(rdev
);
65 unsigned int ramp_delay
= 0;
66 int old_volt
, new_volt
;
68 switch (rdev
->desc
->id
) {
70 ramp_delay
= s2mps11
->ramp_delay2
;
74 ramp_delay
= s2mps11
->ramp_delay34
;
77 ramp_delay
= s2mps11
->ramp_delay5
;
81 ramp_delay
= s2mps11
->ramp_delay16
;
86 ramp_delay
= s2mps11
->ramp_delay7810
;
89 ramp_delay
= s2mps11
->ramp_delay9
;
93 ramp_delay
= rdev
->desc
->ramp_delay
;
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
);
98 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
101 static int s2mps11_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
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;
108 switch (rdev
->desc
->id
) {
110 if (ramp_delay
> s2mps11
->ramp_delay16
)
111 s2mps11
->ramp_delay16
= ramp_delay
;
113 ramp_delay
= s2mps11
->ramp_delay16
;
115 ramp_shift
= S2MPS11_BUCK16_RAMP_SHIFT
;
118 enable_shift
= S2MPS11_BUCK2_RAMP_EN_SHIFT
;
124 s2mps11
->ramp_delay2
= ramp_delay
;
125 ramp_shift
= S2MPS11_BUCK2_RAMP_SHIFT
;
126 ramp_reg
= S2MPS11_REG_RAMP
;
129 enable_shift
= S2MPS11_BUCK3_RAMP_EN_SHIFT
;
135 if (ramp_delay
> s2mps11
->ramp_delay34
)
136 s2mps11
->ramp_delay34
= ramp_delay
;
138 ramp_delay
= s2mps11
->ramp_delay34
;
140 ramp_shift
= S2MPS11_BUCK34_RAMP_SHIFT
;
141 ramp_reg
= S2MPS11_REG_RAMP
;
144 enable_shift
= S2MPS11_BUCK4_RAMP_EN_SHIFT
;
150 if (ramp_delay
> s2mps11
->ramp_delay34
)
151 s2mps11
->ramp_delay34
= ramp_delay
;
153 ramp_delay
= s2mps11
->ramp_delay34
;
155 ramp_shift
= S2MPS11_BUCK34_RAMP_SHIFT
;
156 ramp_reg
= S2MPS11_REG_RAMP
;
159 s2mps11
->ramp_delay5
= ramp_delay
;
160 ramp_shift
= S2MPS11_BUCK5_RAMP_SHIFT
;
163 enable_shift
= S2MPS11_BUCK6_RAMP_EN_SHIFT
;
169 if (ramp_delay
> s2mps11
->ramp_delay16
)
170 s2mps11
->ramp_delay16
= ramp_delay
;
172 ramp_delay
= s2mps11
->ramp_delay16
;
174 ramp_shift
= S2MPS11_BUCK16_RAMP_SHIFT
;
179 if (ramp_delay
> s2mps11
->ramp_delay7810
)
180 s2mps11
->ramp_delay7810
= ramp_delay
;
182 ramp_delay
= s2mps11
->ramp_delay7810
;
184 ramp_shift
= S2MPS11_BUCK7810_RAMP_SHIFT
;
187 s2mps11
->ramp_delay9
= ramp_delay
;
188 ramp_shift
= S2MPS11_BUCK9_RAMP_SHIFT
;
198 ret
= regmap_update_bits(rdev
->regmap
, S2MPS11_REG_RAMP
,
199 1 << enable_shift
, 1 << enable_shift
);
201 dev_err(&rdev
->dev
, "failed to enable ramp rate\n");
206 ramp_val
= get_ramp_delay(ramp_delay
);
208 return regmap_update_bits(rdev
->regmap
, ramp_reg
, 0x3 << ramp_shift
,
209 ramp_val
<< ramp_shift
);
212 return regmap_update_bits(rdev
->regmap
, S2MPS11_REG_RAMP
,
213 1 << enable_shift
, 0);
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
,
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
,
239 #define regulator_desc_ldo1(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 \
253 #define regulator_desc_ldo2(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 \
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 \
284 #define regulator_desc_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 \
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 \
316 #define regulator_desc_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 \
332 #define regulator_desc_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 \
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
,
399 static int s2mps11_pmic_probe(struct platform_device
*pdev
)
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
;
409 s2mps11
= devm_kzalloc(&pdev
->dev
, sizeof(struct s2mps11_info
),
414 if (!iodev
->dev
->of_node
) {
418 dev_err(pdev
->dev
.parent
,
419 "Platform data or DT node not supplied\n");
424 for (i
= 0; i
< S2MPS11_REGULATOR_CNT
; i
++)
425 rdata
[i
].name
= regulators
[i
].name
;
427 reg_np
= of_find_node_by_name(iodev
->dev
->of_node
, "regulators");
429 dev_err(&pdev
->dev
, "could not find regulators sub-node\n");
433 of_regulator_match(&pdev
->dev
, reg_np
, rdata
, S2MPS11_REGULATOR_MAX
);
436 platform_set_drvdata(pdev
, s2mps11
);
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
++) {
443 config
.init_data
= pdata
->regulators
[i
].initdata
;
444 config
.of_node
= pdata
->regulators
[i
].reg_node
;
446 config
.init_data
= rdata
[i
].init_data
;
447 config
.of_node
= rdata
[i
].of_node
;
450 s2mps11
->rdev
[i
] = devm_regulator_register(&pdev
->dev
,
451 ®ulators
[i
], &config
);
452 if (IS_ERR(s2mps11
->rdev
[i
])) {
453 ret
= PTR_ERR(s2mps11
->rdev
[i
]);
454 dev_err(&pdev
->dev
, "regulator init failed for %d\n",
463 static const struct platform_device_id s2mps11_pmic_id
[] = {
464 { "s2mps11-pmic", 0},
467 MODULE_DEVICE_TABLE(platform
, s2mps11_pmic_id
);
469 static struct platform_driver s2mps11_pmic_driver
= {
471 .name
= "s2mps11-pmic",
472 .owner
= THIS_MODULE
,
474 .probe
= s2mps11_pmic_probe
,
475 .id_table
= s2mps11_pmic_id
,
478 static int __init
s2mps11_pmic_init(void)
480 return platform_driver_register(&s2mps11_pmic_driver
);
482 subsys_initcall(s2mps11_pmic_init
);
484 static void __exit
s2mps11_pmic_exit(void)
486 platform_driver_unregister(&s2mps11_pmic_driver
);
488 module_exit(s2mps11_pmic_exit
);
490 /* Module information */
491 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
492 MODULE_DESCRIPTION("SAMSUNG S2MPS11 Regulator Driver");
493 MODULE_LICENSE("GPL");