1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd
4 // http://www.samsung.com
8 #include <linux/gpio.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/mfd/samsung/core.h>
18 #include <linux/mfd/samsung/s2mpa01.h>
29 static int get_ramp_delay(int ramp_delay)
31 unsigned char cnt = 0;
36 ramp_delay = ramp_delay >> 1;
48 static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
49 unsigned int old_selector,
50 unsigned int new_selector)
52 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
53 unsigned int ramp_delay = 0;
54 int old_volt, new_volt;
56 switch (rdev_get_id(rdev)) {
59 ramp_delay = s2mpa01->ramp_delay24;
62 ramp_delay = s2mpa01->ramp_delay3;
65 ramp_delay = s2mpa01->ramp_delay5;
69 ramp_delay = s2mpa01->ramp_delay16;
72 ramp_delay = s2mpa01->ramp_delay7;
77 ramp_delay = s2mpa01->ramp_delay8910;
82 ramp_delay = rdev->desc->ramp_delay;
84 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
85 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
87 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
90 static int s2mpa01_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
92 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
93 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPA01_REG_RAMP2;
94 unsigned int ramp_enable = 1, enable_shift = 0;
97 switch (rdev_get_id(rdev)) {
99 enable_shift = S2MPA01_BUCK1_RAMP_EN_SHIFT;
105 if (ramp_delay > s2mpa01->ramp_delay16)
106 s2mpa01->ramp_delay16 = ramp_delay;
108 ramp_delay = s2mpa01->ramp_delay16;
110 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
113 enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
119 if (ramp_delay > s2mpa01->ramp_delay24)
120 s2mpa01->ramp_delay24 = ramp_delay;
122 ramp_delay = s2mpa01->ramp_delay24;
124 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
125 ramp_reg = S2MPA01_REG_RAMP1;
128 enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
134 s2mpa01->ramp_delay3 = ramp_delay;
135 ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
136 ramp_reg = S2MPA01_REG_RAMP1;
139 enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
145 if (ramp_delay > s2mpa01->ramp_delay24)
146 s2mpa01->ramp_delay24 = ramp_delay;
148 ramp_delay = s2mpa01->ramp_delay24;
150 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
151 ramp_reg = S2MPA01_REG_RAMP1;
154 s2mpa01->ramp_delay5 = ramp_delay;
155 ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
158 if (ramp_delay > s2mpa01->ramp_delay16)
159 s2mpa01->ramp_delay16 = ramp_delay;
161 ramp_delay = s2mpa01->ramp_delay16;
163 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
166 s2mpa01->ramp_delay7 = ramp_delay;
167 ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
172 if (ramp_delay > s2mpa01->ramp_delay8910)
173 s2mpa01->ramp_delay8910 = ramp_delay;
175 ramp_delay = s2mpa01->ramp_delay8910;
177 ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
186 /* Ramp delay can be enabled/disabled only for buck[1234] */
187 if (rdev_get_id(rdev) >= S2MPA01_BUCK1 &&
188 rdev_get_id(rdev) <= S2MPA01_BUCK4) {
189 ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
190 1 << enable_shift, 1 << enable_shift);
192 dev_err(&rdev->dev, "failed to enable ramp rate\n");
197 ramp_val = get_ramp_delay(ramp_delay);
199 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
200 ramp_val << ramp_shift);
203 return regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
204 1 << enable_shift, 0);
207 static const struct regulator_ops s2mpa01_ldo_ops = {
208 .list_voltage = regulator_list_voltage_linear,
209 .map_voltage = regulator_map_voltage_linear,
210 .is_enabled = regulator_is_enabled_regmap,
211 .enable = regulator_enable_regmap,
212 .disable = regulator_disable_regmap,
213 .get_voltage_sel = regulator_get_voltage_sel_regmap,
214 .set_voltage_sel = regulator_set_voltage_sel_regmap,
215 .set_voltage_time_sel = regulator_set_voltage_time_sel,
218 static const struct regulator_ops s2mpa01_buck_ops = {
219 .list_voltage = regulator_list_voltage_linear,
220 .map_voltage = regulator_map_voltage_linear,
221 .is_enabled = regulator_is_enabled_regmap,
222 .enable = regulator_enable_regmap,
223 .disable = regulator_disable_regmap,
224 .get_voltage_sel = regulator_get_voltage_sel_regmap,
225 .set_voltage_sel = regulator_set_voltage_sel_regmap,
226 .set_voltage_time_sel = s2mpa01_regulator_set_voltage_time_sel,
227 .set_ramp_delay = s2mpa01_set_ramp_delay,
230 #define regulator_desc_ldo(num, step) { \
232 .of_match = of_match_ptr("LDO"#num), \
233 .regulators_node = of_match_ptr("regulators"), \
234 .id = S2MPA01_LDO##num, \
235 .ops = &s2mpa01_ldo_ops, \
236 .type = REGULATOR_VOLTAGE, \
237 .owner = THIS_MODULE, \
238 .min_uV = MIN_800_MV, \
240 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
241 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
242 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
243 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
244 .enable_mask = S2MPA01_ENABLE_MASK \
247 #define regulator_desc_buck1_4(num) { \
248 .name = "BUCK"#num, \
249 .of_match = of_match_ptr("BUCK"#num), \
250 .regulators_node = of_match_ptr("regulators"), \
251 .id = S2MPA01_BUCK##num, \
252 .ops = &s2mpa01_buck_ops, \
253 .type = REGULATOR_VOLTAGE, \
254 .owner = THIS_MODULE, \
255 .min_uV = MIN_600_MV, \
256 .uV_step = STEP_6_25_MV, \
257 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
258 .ramp_delay = S2MPA01_RAMP_DELAY, \
259 .vsel_reg = S2MPA01_REG_B1CTRL2 + (num - 1) * 2, \
260 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
261 .enable_reg = S2MPA01_REG_B1CTRL1 + (num - 1) * 2, \
262 .enable_mask = S2MPA01_ENABLE_MASK \
265 #define regulator_desc_buck5 { \
267 .of_match = of_match_ptr("BUCK5"), \
268 .regulators_node = of_match_ptr("regulators"), \
269 .id = S2MPA01_BUCK5, \
270 .ops = &s2mpa01_buck_ops, \
271 .type = REGULATOR_VOLTAGE, \
272 .owner = THIS_MODULE, \
273 .min_uV = MIN_800_MV, \
274 .uV_step = STEP_6_25_MV, \
275 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
276 .ramp_delay = S2MPA01_RAMP_DELAY, \
277 .vsel_reg = S2MPA01_REG_B5CTRL2, \
278 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
279 .enable_reg = S2MPA01_REG_B5CTRL1, \
280 .enable_mask = S2MPA01_ENABLE_MASK \
283 #define regulator_desc_buck6_10(num, min, step) { \
284 .name = "BUCK"#num, \
285 .of_match = of_match_ptr("BUCK"#num), \
286 .regulators_node = of_match_ptr("regulators"), \
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_25_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_50_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_50_MV),
324 regulator_desc_ldo(23, STEP_50_MV),
325 regulator_desc_ldo(24, STEP_50_MV),
326 regulator_desc_ldo(25, STEP_50_MV),
327 regulator_desc_ldo(26, STEP_25_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 regulator_config config = { };
344 struct s2mpa01_info *s2mpa01;
347 s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
351 config.dev = iodev->dev;
352 config.regmap = iodev->regmap_pmic;
353 config.driver_data = s2mpa01;
355 for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
356 struct regulator_dev *rdev;
358 rdev = devm_regulator_register(&pdev->dev,
359 ®ulators[i], &config);
361 dev_err(&pdev->dev, "regulator init failed for %d\n",
363 return PTR_ERR(rdev);
370 static const struct platform_device_id s2mpa01_pmic_id[] = {
371 { "s2mpa01-pmic", 0},
374 MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
376 static struct platform_driver s2mpa01_pmic_driver = {
378 .name = "s2mpa01-pmic",
380 .probe = s2mpa01_pmic_probe,
381 .id_table = s2mpa01_pmic_id,
384 module_platform_driver(s2mpa01_pmic_driver);
386 /* Module information */
387 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
388 MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
389 MODULE_DESCRIPTION("Samsung S2MPA01 Regulator Driver");
390 MODULE_LICENSE("GPL");