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>
29 unsigned int rdev_num;
38 static int get_ramp_delay(int ramp_delay)
40 unsigned char cnt = 0;
45 ramp_delay = ramp_delay >> 1;
57 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
58 unsigned int old_selector,
59 unsigned int new_selector)
61 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
62 unsigned int ramp_delay = 0;
63 int old_volt, new_volt;
65 switch (rdev->desc->id) {
67 ramp_delay = s2mps11->ramp_delay2;
71 ramp_delay = s2mps11->ramp_delay34;
74 ramp_delay = s2mps11->ramp_delay5;
78 ramp_delay = s2mps11->ramp_delay16;
83 ramp_delay = s2mps11->ramp_delay7810;
86 ramp_delay = s2mps11->ramp_delay9;
90 ramp_delay = rdev->desc->ramp_delay;
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);
95 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
98 static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
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;
105 switch (rdev->desc->id) {
107 if (ramp_delay > s2mps11->ramp_delay16)
108 s2mps11->ramp_delay16 = ramp_delay;
110 ramp_delay = s2mps11->ramp_delay16;
112 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
115 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
121 s2mps11->ramp_delay2 = ramp_delay;
122 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
123 ramp_reg = S2MPS11_REG_RAMP;
126 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
132 if (ramp_delay > s2mps11->ramp_delay34)
133 s2mps11->ramp_delay34 = ramp_delay;
135 ramp_delay = s2mps11->ramp_delay34;
137 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
138 ramp_reg = S2MPS11_REG_RAMP;
141 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
147 if (ramp_delay > s2mps11->ramp_delay34)
148 s2mps11->ramp_delay34 = ramp_delay;
150 ramp_delay = s2mps11->ramp_delay34;
152 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
153 ramp_reg = S2MPS11_REG_RAMP;
156 s2mps11->ramp_delay5 = ramp_delay;
157 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
160 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
166 if (ramp_delay > s2mps11->ramp_delay16)
167 s2mps11->ramp_delay16 = ramp_delay;
169 ramp_delay = s2mps11->ramp_delay16;
171 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
176 if (ramp_delay > s2mps11->ramp_delay7810)
177 s2mps11->ramp_delay7810 = ramp_delay;
179 ramp_delay = s2mps11->ramp_delay7810;
181 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
184 s2mps11->ramp_delay9 = ramp_delay;
185 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
195 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
196 1 << enable_shift, 1 << enable_shift);
198 dev_err(&rdev->dev, "failed to enable ramp rate\n");
203 ramp_val = get_ramp_delay(ramp_delay);
205 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
206 ramp_val << ramp_shift);
209 return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
210 1 << enable_shift, 0);
213 static 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,
224 static 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,
232 .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel,
233 .set_ramp_delay = s2mps11_set_ramp_delay,
236 #define regulator_desc_ldo1(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, \
246 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
247 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
248 .enable_mask = S2MPS11_ENABLE_MASK \
250 #define regulator_desc_ldo2(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, \
260 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
261 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
262 .enable_mask = S2MPS11_ENABLE_MASK \
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, \
274 .ramp_delay = S2MPS11_RAMP_DELAY, \
275 .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
276 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
277 .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
278 .enable_mask = S2MPS11_ENABLE_MASK \
281 #define regulator_desc_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, \
290 .ramp_delay = S2MPS11_RAMP_DELAY, \
291 .vsel_reg = S2MPS11_REG_B5CTRL2, \
292 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
293 .enable_reg = S2MPS11_REG_B5CTRL1, \
294 .enable_mask = S2MPS11_ENABLE_MASK \
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, \
306 .ramp_delay = S2MPS11_RAMP_DELAY, \
307 .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
308 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
309 .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
310 .enable_mask = S2MPS11_ENABLE_MASK \
313 #define regulator_desc_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, \
322 .ramp_delay = S2MPS11_RAMP_DELAY, \
323 .vsel_reg = S2MPS11_REG_B9CTRL2, \
324 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
325 .enable_reg = S2MPS11_REG_B9CTRL1, \
326 .enable_mask = S2MPS11_ENABLE_MASK \
329 #define regulator_desc_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, \
338 .ramp_delay = S2MPS11_RAMP_DELAY, \
339 .vsel_reg = S2MPS11_REG_B10CTRL2, \
340 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
341 .enable_reg = S2MPS11_REG_B10CTRL1, \
342 .enable_mask = S2MPS11_ENABLE_MASK \
345 static const struct regulator_desc s2mps11_regulators[] = {
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,
396 static int s2mps11_pmic_probe(struct platform_device *pdev)
398 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
399 struct sec_platform_data *pdata = iodev->pdata;
400 struct of_regulator_match *rdata = NULL;
401 struct device_node *reg_np = NULL;
402 struct regulator_config config = { };
403 struct s2mps11_info *s2mps11;
405 const struct regulator_desc *regulators;
406 enum sec_device_type dev_type;
408 s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
413 dev_type = platform_get_device_id(pdev)->driver_data;
416 s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
417 regulators = s2mps11_regulators;
420 dev_err(&pdev->dev, "Invalid device type: %u\n", dev_type);
424 if (!iodev->dev->of_node) {
428 dev_err(pdev->dev.parent,
429 "Platform data or DT node not supplied\n");
434 rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
438 for (i = 0; i < s2mps11->rdev_num; i++)
439 rdata[i].name = regulators[i].name;
441 reg_np = of_find_node_by_name(iodev->dev->of_node, "regulators");
443 dev_err(&pdev->dev, "could not find regulators sub-node\n");
448 of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
451 platform_set_drvdata(pdev, s2mps11);
453 config.dev = &pdev->dev;
454 config.regmap = iodev->regmap_pmic;
455 config.driver_data = s2mps11;
456 for (i = 0; i < s2mps11->rdev_num; i++) {
457 struct regulator_dev *regulator;
460 config.init_data = pdata->regulators[i].initdata;
461 config.of_node = pdata->regulators[i].reg_node;
463 config.init_data = rdata[i].init_data;
464 config.of_node = rdata[i].of_node;
467 regulator = devm_regulator_register(&pdev->dev,
468 ®ulators[i], &config);
469 if (IS_ERR(regulator)) {
470 ret = PTR_ERR(regulator);
471 dev_err(&pdev->dev, "regulator init failed for %d\n",
483 static const struct platform_device_id s2mps11_pmic_id[] = {
484 { "s2mps11-pmic", S2MPS11X},
487 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
489 static struct platform_driver s2mps11_pmic_driver = {
491 .name = "s2mps11-pmic",
492 .owner = THIS_MODULE,
494 .probe = s2mps11_pmic_probe,
495 .id_table = s2mps11_pmic_id,
498 static int __init s2mps11_pmic_init(void)
500 return platform_driver_register(&s2mps11_pmic_driver);
502 subsys_initcall(s2mps11_pmic_init);
504 static void __exit s2mps11_pmic_exit(void)
506 platform_driver_unregister(&s2mps11_pmic_driver);
508 module_exit(s2mps11_pmic_exit);
510 /* Module information */
511 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
512 MODULE_DESCRIPTION("SAMSUNG S2MPS11 Regulator Driver");
513 MODULE_LICENSE("GPL");