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_get_id(rdev)) {
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_get_id(rdev)) {
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;
121 enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
127 if (ramp_delay > s2mpa01->ramp_delay24)
128 s2mpa01->ramp_delay24 = ramp_delay;
130 ramp_delay = s2mpa01->ramp_delay24;
132 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
133 ramp_reg = S2MPA01_REG_RAMP1;
136 enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
142 s2mpa01->ramp_delay3 = ramp_delay;
143 ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
144 ramp_reg = S2MPA01_REG_RAMP1;
147 enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
153 if (ramp_delay > s2mpa01->ramp_delay24)
154 s2mpa01->ramp_delay24 = ramp_delay;
156 ramp_delay = s2mpa01->ramp_delay24;
158 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
159 ramp_reg = S2MPA01_REG_RAMP1;
162 s2mpa01->ramp_delay5 = ramp_delay;
163 ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
166 if (ramp_delay > s2mpa01->ramp_delay16)
167 s2mpa01->ramp_delay16 = ramp_delay;
169 ramp_delay = s2mpa01->ramp_delay16;
171 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
174 s2mpa01->ramp_delay7 = ramp_delay;
175 ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
180 if (ramp_delay > s2mpa01->ramp_delay8910)
181 s2mpa01->ramp_delay8910 = ramp_delay;
183 ramp_delay = s2mpa01->ramp_delay8910;
185 ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
194 /* Ramp delay can be enabled/disabled only for buck[1234] */
195 if (rdev_get_id(rdev) >= S2MPA01_BUCK1 &&
196 rdev_get_id(rdev) <= S2MPA01_BUCK4) {
197 ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
198 1 << enable_shift, 1 << enable_shift);
200 dev_err(&rdev->dev, "failed to enable ramp rate\n");
205 ramp_val = get_ramp_delay(ramp_delay);
207 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
208 ramp_val << ramp_shift);
211 return regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
212 1 << enable_shift, 0);
215 static struct regulator_ops s2mpa01_ldo_ops = {
216 .list_voltage = regulator_list_voltage_linear,
217 .map_voltage = regulator_map_voltage_linear,
218 .is_enabled = regulator_is_enabled_regmap,
219 .enable = regulator_enable_regmap,
220 .disable = regulator_disable_regmap,
221 .get_voltage_sel = regulator_get_voltage_sel_regmap,
222 .set_voltage_sel = regulator_set_voltage_sel_regmap,
223 .set_voltage_time_sel = regulator_set_voltage_time_sel,
226 static struct regulator_ops s2mpa01_buck_ops = {
227 .list_voltage = regulator_list_voltage_linear,
228 .map_voltage = regulator_map_voltage_linear,
229 .is_enabled = regulator_is_enabled_regmap,
230 .enable = regulator_enable_regmap,
231 .disable = regulator_disable_regmap,
232 .get_voltage_sel = regulator_get_voltage_sel_regmap,
233 .set_voltage_sel = regulator_set_voltage_sel_regmap,
234 .set_voltage_time_sel = s2mpa01_regulator_set_voltage_time_sel,
235 .set_ramp_delay = s2mpa01_set_ramp_delay,
238 #define regulator_desc_ldo(num, step) { \
240 .id = S2MPA01_LDO##num, \
241 .ops = &s2mpa01_ldo_ops, \
242 .type = REGULATOR_VOLTAGE, \
243 .owner = THIS_MODULE, \
244 .min_uV = MIN_800_MV, \
246 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
247 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
248 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
249 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
250 .enable_mask = S2MPA01_ENABLE_MASK \
253 #define regulator_desc_buck1_4(num) { \
254 .name = "BUCK"#num, \
255 .id = S2MPA01_BUCK##num, \
256 .ops = &s2mpa01_buck_ops, \
257 .type = REGULATOR_VOLTAGE, \
258 .owner = THIS_MODULE, \
259 .min_uV = MIN_600_MV, \
260 .uV_step = STEP_6_25_MV, \
261 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
262 .ramp_delay = S2MPA01_RAMP_DELAY, \
263 .vsel_reg = S2MPA01_REG_B1CTRL2 + (num - 1) * 2, \
264 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
265 .enable_reg = S2MPA01_REG_B1CTRL1 + (num - 1) * 2, \
266 .enable_mask = S2MPA01_ENABLE_MASK \
269 #define regulator_desc_buck5 { \
271 .id = S2MPA01_BUCK5, \
272 .ops = &s2mpa01_buck_ops, \
273 .type = REGULATOR_VOLTAGE, \
274 .owner = THIS_MODULE, \
275 .min_uV = MIN_800_MV, \
276 .uV_step = STEP_6_25_MV, \
277 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
278 .ramp_delay = S2MPA01_RAMP_DELAY, \
279 .vsel_reg = S2MPA01_REG_B5CTRL2, \
280 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
281 .enable_reg = S2MPA01_REG_B5CTRL1, \
282 .enable_mask = S2MPA01_ENABLE_MASK \
285 #define regulator_desc_buck6_10(num, min, step) { \
286 .name = "BUCK"#num, \
287 .id = S2MPA01_BUCK##num, \
288 .ops = &s2mpa01_buck_ops, \
289 .type = REGULATOR_VOLTAGE, \
290 .owner = THIS_MODULE, \
293 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
294 .ramp_delay = S2MPA01_RAMP_DELAY, \
295 .vsel_reg = S2MPA01_REG_B6CTRL2 + (num - 6) * 2, \
296 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
297 .enable_reg = S2MPA01_REG_B6CTRL1 + (num - 6) * 2, \
298 .enable_mask = S2MPA01_ENABLE_MASK \
301 static const struct regulator_desc regulators[] = {
302 regulator_desc_ldo(1, STEP_25_MV),
303 regulator_desc_ldo(2, STEP_50_MV),
304 regulator_desc_ldo(3, STEP_50_MV),
305 regulator_desc_ldo(4, STEP_50_MV),
306 regulator_desc_ldo(5, STEP_50_MV),
307 regulator_desc_ldo(6, STEP_25_MV),
308 regulator_desc_ldo(7, STEP_50_MV),
309 regulator_desc_ldo(8, STEP_50_MV),
310 regulator_desc_ldo(9, STEP_50_MV),
311 regulator_desc_ldo(10, STEP_50_MV),
312 regulator_desc_ldo(11, STEP_25_MV),
313 regulator_desc_ldo(12, STEP_50_MV),
314 regulator_desc_ldo(13, STEP_50_MV),
315 regulator_desc_ldo(14, STEP_50_MV),
316 regulator_desc_ldo(15, STEP_50_MV),
317 regulator_desc_ldo(16, STEP_50_MV),
318 regulator_desc_ldo(17, STEP_50_MV),
319 regulator_desc_ldo(18, STEP_50_MV),
320 regulator_desc_ldo(19, STEP_50_MV),
321 regulator_desc_ldo(20, STEP_50_MV),
322 regulator_desc_ldo(21, STEP_50_MV),
323 regulator_desc_ldo(22, STEP_25_MV),
324 regulator_desc_ldo(23, STEP_25_MV),
325 regulator_desc_ldo(24, STEP_50_MV),
326 regulator_desc_ldo(25, STEP_50_MV),
327 regulator_desc_ldo(26, STEP_50_MV),
328 regulator_desc_buck1_4(1),
329 regulator_desc_buck1_4(2),
330 regulator_desc_buck1_4(3),
331 regulator_desc_buck1_4(4),
332 regulator_desc_buck5,
333 regulator_desc_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
334 regulator_desc_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
335 regulator_desc_buck6_10(8, MIN_800_MV, STEP_12_5_MV),
336 regulator_desc_buck6_10(9, MIN_1500_MV, STEP_12_5_MV),
337 regulator_desc_buck6_10(10, MIN_1000_MV, STEP_12_5_MV),
340 static int s2mpa01_pmic_probe(struct platform_device *pdev)
342 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
343 struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
344 struct of_regulator_match rdata[S2MPA01_REGULATOR_MAX] = { };
345 struct device_node *reg_np = NULL;
346 struct regulator_config config = { };
347 struct s2mpa01_info *s2mpa01;
350 s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
354 for (i = 0; i < S2MPA01_REGULATOR_CNT; i++)
355 rdata[i].name = regulators[i].name;
357 if (iodev->dev->of_node) {
358 reg_np = of_get_child_by_name(iodev->dev->of_node,
362 "could not find regulators sub-node\n");
366 of_regulator_match(&pdev->dev, reg_np, rdata,
367 S2MPA01_REGULATOR_MAX);
371 platform_set_drvdata(pdev, s2mpa01);
373 config.dev = &pdev->dev;
374 config.regmap = iodev->regmap_pmic;
375 config.driver_data = s2mpa01;
377 for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
378 struct regulator_dev *rdev;
380 config.init_data = pdata->regulators[i].initdata;
382 config.init_data = rdata[i].init_data;
385 config.of_node = rdata[i].of_node;
387 rdev = devm_regulator_register(&pdev->dev,
388 ®ulators[i], &config);
390 dev_err(&pdev->dev, "regulator init failed for %d\n",
392 return PTR_ERR(rdev);
399 static const struct platform_device_id s2mpa01_pmic_id[] = {
400 { "s2mpa01-pmic", 0},
403 MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
405 static struct platform_driver s2mpa01_pmic_driver = {
407 .name = "s2mpa01-pmic",
409 .probe = s2mpa01_pmic_probe,
410 .id_table = s2mpa01_pmic_id,
413 module_platform_driver(s2mpa01_pmic_driver);
415 /* Module information */
416 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
417 MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
418 MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
419 MODULE_LICENSE("GPL");