2 * Copyright (c) 2013 Samsung Electronics Co., Ltd
3 * http://www.samsung.com
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
12 #include <linux/bug.h>
13 #include <linux/err.h>
14 #include <linux/gpio.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
18 #include <linux/regmap.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/mfd/samsung/core.h>
24 #include <linux/mfd/samsung/s2mpa01.h>
26 #define S2MPA01_REGULATOR_CNT ARRAY_SIZE(regulators)
37 static int get_ramp_delay(int ramp_delay)
39 unsigned char cnt = 0;
44 ramp_delay = ramp_delay >> 1;
56 static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
57 unsigned int old_selector,
58 unsigned int new_selector)
60 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
61 unsigned int ramp_delay = 0;
62 int old_volt, new_volt;
64 switch (rdev->desc->id) {
67 ramp_delay = s2mpa01->ramp_delay24;
70 ramp_delay = s2mpa01->ramp_delay3;
73 ramp_delay = s2mpa01->ramp_delay5;
77 ramp_delay = s2mpa01->ramp_delay16;
80 ramp_delay = s2mpa01->ramp_delay7;
85 ramp_delay = s2mpa01->ramp_delay8910;
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 s2mpa01_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
100 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
101 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPA01_REG_RAMP2;
102 unsigned int ramp_enable = 1, enable_shift = 0;
105 switch (rdev->desc->id) {
107 enable_shift = S2MPA01_BUCK1_RAMP_EN_SHIFT;
113 if (ramp_delay > s2mpa01->ramp_delay16)
114 s2mpa01->ramp_delay16 = ramp_delay;
116 ramp_delay = s2mpa01->ramp_delay16;
118 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
119 ramp_reg = S2MPA01_REG_RAMP1;
122 enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
128 if (ramp_delay > s2mpa01->ramp_delay24)
129 s2mpa01->ramp_delay24 = ramp_delay;
131 ramp_delay = s2mpa01->ramp_delay24;
133 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
134 ramp_reg = S2MPA01_REG_RAMP1;
137 enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
143 s2mpa01->ramp_delay3 = ramp_delay;
144 ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
145 ramp_reg = S2MPA01_REG_RAMP1;
148 enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
154 if (ramp_delay > s2mpa01->ramp_delay24)
155 s2mpa01->ramp_delay24 = ramp_delay;
157 ramp_delay = s2mpa01->ramp_delay24;
159 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
160 ramp_reg = S2MPA01_REG_RAMP1;
163 s2mpa01->ramp_delay5 = ramp_delay;
164 ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
167 if (ramp_delay > s2mpa01->ramp_delay16)
168 s2mpa01->ramp_delay16 = ramp_delay;
170 ramp_delay = s2mpa01->ramp_delay16;
172 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
175 s2mpa01->ramp_delay7 = ramp_delay;
176 ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
181 if (ramp_delay > s2mpa01->ramp_delay8910)
182 s2mpa01->ramp_delay8910 = ramp_delay;
184 ramp_delay = s2mpa01->ramp_delay8910;
186 ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
195 /* Ramp delay can be enabled/disabled only for buck[1234] */
196 if (rdev_get_id(rdev) >= S2MPA01_BUCK1 &&
197 rdev_get_id(rdev) <= S2MPA01_BUCK4) {
198 ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
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, S2MPA01_REG_RAMP1,
213 1 << enable_shift, 0);
216 static struct regulator_ops s2mpa01_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 s2mpa01_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 = s2mpa01_regulator_set_voltage_time_sel,
236 .set_ramp_delay = s2mpa01_set_ramp_delay,
239 #define regulator_desc_ldo1(num) { \
241 .id = S2MPA01_LDO##num, \
242 .ops = &s2mpa01_ldo_ops, \
243 .type = REGULATOR_VOLTAGE, \
244 .owner = THIS_MODULE, \
245 .min_uV = S2MPA01_LDO_MIN, \
246 .uV_step = S2MPA01_LDO_STEP1, \
247 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
248 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
249 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
250 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
251 .enable_mask = S2MPA01_ENABLE_MASK \
253 #define regulator_desc_ldo2(num) { \
255 .id = S2MPA01_LDO##num, \
256 .ops = &s2mpa01_ldo_ops, \
257 .type = REGULATOR_VOLTAGE, \
258 .owner = THIS_MODULE, \
259 .min_uV = S2MPA01_LDO_MIN, \
260 .uV_step = S2MPA01_LDO_STEP2, \
261 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
262 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
263 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
264 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
265 .enable_mask = S2MPA01_ENABLE_MASK \
268 #define regulator_desc_buck1_4(num) { \
269 .name = "BUCK"#num, \
270 .id = S2MPA01_BUCK##num, \
271 .ops = &s2mpa01_buck_ops, \
272 .type = REGULATOR_VOLTAGE, \
273 .owner = THIS_MODULE, \
274 .min_uV = S2MPA01_BUCK_MIN1, \
275 .uV_step = S2MPA01_BUCK_STEP1, \
276 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
277 .ramp_delay = S2MPA01_RAMP_DELAY, \
278 .vsel_reg = S2MPA01_REG_B1CTRL2 + (num - 1) * 2, \
279 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
280 .enable_reg = S2MPA01_REG_B1CTRL1 + (num - 1) * 2, \
281 .enable_mask = S2MPA01_ENABLE_MASK \
284 #define regulator_desc_buck5 { \
286 .id = S2MPA01_BUCK5, \
287 .ops = &s2mpa01_buck_ops, \
288 .type = REGULATOR_VOLTAGE, \
289 .owner = THIS_MODULE, \
290 .min_uV = S2MPA01_BUCK_MIN2, \
291 .uV_step = S2MPA01_BUCK_STEP1, \
292 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
293 .ramp_delay = S2MPA01_RAMP_DELAY, \
294 .vsel_reg = S2MPA01_REG_B5CTRL2, \
295 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
296 .enable_reg = S2MPA01_REG_B5CTRL1, \
297 .enable_mask = S2MPA01_ENABLE_MASK \
300 #define regulator_desc_buck6_7(num) { \
301 .name = "BUCK"#num, \
302 .id = S2MPA01_BUCK##num, \
303 .ops = &s2mpa01_buck_ops, \
304 .type = REGULATOR_VOLTAGE, \
305 .owner = THIS_MODULE, \
306 .min_uV = S2MPA01_BUCK_MIN1, \
307 .uV_step = S2MPA01_BUCK_STEP1, \
308 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
309 .ramp_delay = S2MPA01_RAMP_DELAY, \
310 .vsel_reg = S2MPA01_REG_B6CTRL2 + (num - 6) * 2, \
311 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
312 .enable_reg = S2MPA01_REG_B6CTRL1 + (num - 6) * 2, \
313 .enable_mask = S2MPA01_ENABLE_MASK \
316 #define regulator_desc_buck8 { \
318 .id = S2MPA01_BUCK8, \
319 .ops = &s2mpa01_buck_ops, \
320 .type = REGULATOR_VOLTAGE, \
321 .owner = THIS_MODULE, \
322 .min_uV = S2MPA01_BUCK_MIN2, \
323 .uV_step = S2MPA01_BUCK_STEP2, \
324 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
325 .ramp_delay = S2MPA01_RAMP_DELAY, \
326 .vsel_reg = S2MPA01_REG_B8CTRL2, \
327 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
328 .enable_reg = S2MPA01_REG_B8CTRL1, \
329 .enable_mask = S2MPA01_ENABLE_MASK \
332 #define regulator_desc_buck9 { \
334 .id = S2MPA01_BUCK9, \
335 .ops = &s2mpa01_buck_ops, \
336 .type = REGULATOR_VOLTAGE, \
337 .owner = THIS_MODULE, \
338 .min_uV = S2MPA01_BUCK_MIN4, \
339 .uV_step = S2MPA01_BUCK_STEP2, \
340 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
341 .ramp_delay = S2MPA01_RAMP_DELAY, \
342 .vsel_reg = S2MPA01_REG_B9CTRL2, \
343 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
344 .enable_reg = S2MPA01_REG_B9CTRL1, \
345 .enable_mask = S2MPA01_ENABLE_MASK \
348 #define regulator_desc_buck10 { \
350 .id = S2MPA01_BUCK10, \
351 .ops = &s2mpa01_buck_ops, \
352 .type = REGULATOR_VOLTAGE, \
353 .owner = THIS_MODULE, \
354 .min_uV = S2MPA01_BUCK_MIN3, \
355 .uV_step = S2MPA01_BUCK_STEP2, \
356 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
357 .ramp_delay = S2MPA01_RAMP_DELAY, \
358 .vsel_reg = S2MPA01_REG_B10CTRL2, \
359 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
360 .enable_reg = S2MPA01_REG_B10CTRL1, \
361 .enable_mask = S2MPA01_ENABLE_MASK \
364 static struct regulator_desc regulators[] = {
365 regulator_desc_ldo2(1),
366 regulator_desc_ldo1(2),
367 regulator_desc_ldo1(3),
368 regulator_desc_ldo1(4),
369 regulator_desc_ldo1(5),
370 regulator_desc_ldo2(6),
371 regulator_desc_ldo1(7),
372 regulator_desc_ldo1(8),
373 regulator_desc_ldo1(9),
374 regulator_desc_ldo1(10),
375 regulator_desc_ldo2(11),
376 regulator_desc_ldo1(12),
377 regulator_desc_ldo1(13),
378 regulator_desc_ldo1(14),
379 regulator_desc_ldo1(15),
380 regulator_desc_ldo1(16),
381 regulator_desc_ldo1(17),
382 regulator_desc_ldo1(18),
383 regulator_desc_ldo1(19),
384 regulator_desc_ldo1(20),
385 regulator_desc_ldo1(21),
386 regulator_desc_ldo2(22),
387 regulator_desc_ldo2(23),
388 regulator_desc_ldo1(24),
389 regulator_desc_ldo1(25),
390 regulator_desc_ldo1(26),
391 regulator_desc_buck1_4(1),
392 regulator_desc_buck1_4(2),
393 regulator_desc_buck1_4(3),
394 regulator_desc_buck1_4(4),
395 regulator_desc_buck5,
396 regulator_desc_buck6_7(6),
397 regulator_desc_buck6_7(7),
398 regulator_desc_buck8,
399 regulator_desc_buck9,
400 regulator_desc_buck10,
403 static int s2mpa01_pmic_probe(struct platform_device *pdev)
405 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
406 struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
407 struct of_regulator_match rdata[S2MPA01_REGULATOR_MAX];
408 struct device_node *reg_np = NULL;
409 struct regulator_config config = { };
410 struct s2mpa01_info *s2mpa01;
413 s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
417 for (i = 0; i < S2MPA01_REGULATOR_CNT; i++)
418 rdata[i].name = regulators[i].name;
420 if (iodev->dev->of_node) {
421 reg_np = of_get_child_by_name(iodev->dev->of_node,
425 "could not find regulators sub-node\n");
429 of_regulator_match(&pdev->dev, reg_np, rdata,
430 S2MPA01_REGULATOR_MAX);
434 platform_set_drvdata(pdev, s2mpa01);
436 config.dev = &pdev->dev;
437 config.regmap = iodev->regmap_pmic;
438 config.driver_data = s2mpa01;
440 for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
441 struct regulator_dev *rdev;
443 config.init_data = pdata->regulators[i].initdata;
445 config.init_data = rdata[i].init_data;
448 config.of_node = rdata[i].of_node;
450 rdev = devm_regulator_register(&pdev->dev,
451 ®ulators[i], &config);
453 dev_err(&pdev->dev, "regulator init failed for %d\n",
455 return PTR_ERR(rdev);
462 static const struct platform_device_id s2mpa01_pmic_id[] = {
463 { "s2mpa01-pmic", 0},
466 MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
468 static struct platform_driver s2mpa01_pmic_driver = {
470 .name = "s2mpa01-pmic",
471 .owner = THIS_MODULE,
473 .probe = s2mpa01_pmic_probe,
474 .id_table = s2mpa01_pmic_id,
477 module_platform_driver(s2mpa01_pmic_driver);
479 /* Module information */
480 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
481 MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
482 MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
483 MODULE_LICENSE("GPL");