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>
20 #define S2MPA01_REGULATOR_CNT ARRAY_SIZE(regulators)
23 struct of_regulator_match rdata
[S2MPA01_REGULATOR_MAX
];
32 static int get_ramp_delay(int ramp_delay
)
34 unsigned char cnt
= 0;
39 ramp_delay
= ramp_delay
>> 1;
51 static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
52 unsigned int old_selector
,
53 unsigned int new_selector
)
55 struct s2mpa01_info
*s2mpa01
= rdev_get_drvdata(rdev
);
56 unsigned int ramp_delay
= 0;
57 int old_volt
, new_volt
;
59 switch (rdev_get_id(rdev
)) {
62 ramp_delay
= s2mpa01
->ramp_delay24
;
65 ramp_delay
= s2mpa01
->ramp_delay3
;
68 ramp_delay
= s2mpa01
->ramp_delay5
;
72 ramp_delay
= s2mpa01
->ramp_delay16
;
75 ramp_delay
= s2mpa01
->ramp_delay7
;
80 ramp_delay
= s2mpa01
->ramp_delay8910
;
85 ramp_delay
= rdev
->desc
->ramp_delay
;
87 old_volt
= rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* old_selector
);
88 new_volt
= rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* new_selector
);
90 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
93 static int s2mpa01_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
95 struct s2mpa01_info
*s2mpa01
= rdev_get_drvdata(rdev
);
96 unsigned int ramp_val
, ramp_shift
, ramp_reg
= S2MPA01_REG_RAMP2
;
97 unsigned int ramp_enable
= 1, enable_shift
= 0;
100 switch (rdev_get_id(rdev
)) {
102 enable_shift
= S2MPA01_BUCK1_RAMP_EN_SHIFT
;
108 if (ramp_delay
> s2mpa01
->ramp_delay16
)
109 s2mpa01
->ramp_delay16
= ramp_delay
;
111 ramp_delay
= s2mpa01
->ramp_delay16
;
113 ramp_shift
= S2MPA01_BUCK16_RAMP_SHIFT
;
116 enable_shift
= S2MPA01_BUCK2_RAMP_EN_SHIFT
;
122 if (ramp_delay
> s2mpa01
->ramp_delay24
)
123 s2mpa01
->ramp_delay24
= ramp_delay
;
125 ramp_delay
= s2mpa01
->ramp_delay24
;
127 ramp_shift
= S2MPA01_BUCK24_RAMP_SHIFT
;
128 ramp_reg
= S2MPA01_REG_RAMP1
;
131 enable_shift
= S2MPA01_BUCK3_RAMP_EN_SHIFT
;
137 s2mpa01
->ramp_delay3
= ramp_delay
;
138 ramp_shift
= S2MPA01_BUCK3_RAMP_SHIFT
;
139 ramp_reg
= S2MPA01_REG_RAMP1
;
142 enable_shift
= S2MPA01_BUCK4_RAMP_EN_SHIFT
;
148 if (ramp_delay
> s2mpa01
->ramp_delay24
)
149 s2mpa01
->ramp_delay24
= ramp_delay
;
151 ramp_delay
= s2mpa01
->ramp_delay24
;
153 ramp_shift
= S2MPA01_BUCK24_RAMP_SHIFT
;
154 ramp_reg
= S2MPA01_REG_RAMP1
;
157 s2mpa01
->ramp_delay5
= ramp_delay
;
158 ramp_shift
= S2MPA01_BUCK5_RAMP_SHIFT
;
161 if (ramp_delay
> s2mpa01
->ramp_delay16
)
162 s2mpa01
->ramp_delay16
= ramp_delay
;
164 ramp_delay
= s2mpa01
->ramp_delay16
;
166 ramp_shift
= S2MPA01_BUCK16_RAMP_SHIFT
;
169 s2mpa01
->ramp_delay7
= ramp_delay
;
170 ramp_shift
= S2MPA01_BUCK7_RAMP_SHIFT
;
175 if (ramp_delay
> s2mpa01
->ramp_delay8910
)
176 s2mpa01
->ramp_delay8910
= ramp_delay
;
178 ramp_delay
= s2mpa01
->ramp_delay8910
;
180 ramp_shift
= S2MPA01_BUCK8910_RAMP_SHIFT
;
189 /* Ramp delay can be enabled/disabled only for buck[1234] */
190 if (rdev_get_id(rdev
) >= S2MPA01_BUCK1
&&
191 rdev_get_id(rdev
) <= S2MPA01_BUCK4
) {
192 ret
= regmap_update_bits(rdev
->regmap
, S2MPA01_REG_RAMP1
,
193 1 << enable_shift
, 1 << enable_shift
);
195 dev_err(&rdev
->dev
, "failed to enable ramp rate\n");
200 ramp_val
= get_ramp_delay(ramp_delay
);
202 return regmap_update_bits(rdev
->regmap
, ramp_reg
, 0x3 << ramp_shift
,
203 ramp_val
<< ramp_shift
);
206 return regmap_update_bits(rdev
->regmap
, S2MPA01_REG_RAMP1
,
207 1 << enable_shift
, 0);
210 static const struct regulator_ops s2mpa01_ldo_ops
= {
211 .list_voltage
= regulator_list_voltage_linear
,
212 .map_voltage
= regulator_map_voltage_linear
,
213 .is_enabled
= regulator_is_enabled_regmap
,
214 .enable
= regulator_enable_regmap
,
215 .disable
= regulator_disable_regmap
,
216 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
217 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
218 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
221 static const struct regulator_ops s2mpa01_buck_ops
= {
222 .list_voltage
= regulator_list_voltage_linear
,
223 .map_voltage
= regulator_map_voltage_linear
,
224 .is_enabled
= regulator_is_enabled_regmap
,
225 .enable
= regulator_enable_regmap
,
226 .disable
= regulator_disable_regmap
,
227 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
228 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
229 .set_voltage_time_sel
= s2mpa01_regulator_set_voltage_time_sel
,
230 .set_ramp_delay
= s2mpa01_set_ramp_delay
,
233 #define regulator_desc_ldo(num, step) { \
235 .id = S2MPA01_LDO##num, \
236 .ops = &s2mpa01_ldo_ops, \
237 .type = REGULATOR_VOLTAGE, \
238 .owner = THIS_MODULE, \
239 .min_uV = MIN_800_MV, \
241 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
242 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
243 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
244 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
245 .enable_mask = S2MPA01_ENABLE_MASK \
248 #define regulator_desc_buck1_4(num) { \
249 .name = "BUCK"#num, \
250 .id = S2MPA01_BUCK##num, \
251 .ops = &s2mpa01_buck_ops, \
252 .type = REGULATOR_VOLTAGE, \
253 .owner = THIS_MODULE, \
254 .min_uV = MIN_600_MV, \
255 .uV_step = STEP_6_25_MV, \
256 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
257 .ramp_delay = S2MPA01_RAMP_DELAY, \
258 .vsel_reg = S2MPA01_REG_B1CTRL2 + (num - 1) * 2, \
259 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
260 .enable_reg = S2MPA01_REG_B1CTRL1 + (num - 1) * 2, \
261 .enable_mask = S2MPA01_ENABLE_MASK \
264 #define regulator_desc_buck5 { \
266 .id = S2MPA01_BUCK5, \
267 .ops = &s2mpa01_buck_ops, \
268 .type = REGULATOR_VOLTAGE, \
269 .owner = THIS_MODULE, \
270 .min_uV = MIN_800_MV, \
271 .uV_step = STEP_6_25_MV, \
272 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
273 .ramp_delay = S2MPA01_RAMP_DELAY, \
274 .vsel_reg = S2MPA01_REG_B5CTRL2, \
275 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
276 .enable_reg = S2MPA01_REG_B5CTRL1, \
277 .enable_mask = S2MPA01_ENABLE_MASK \
280 #define regulator_desc_buck6_10(num, min, step) { \
281 .name = "BUCK"#num, \
282 .id = S2MPA01_BUCK##num, \
283 .ops = &s2mpa01_buck_ops, \
284 .type = REGULATOR_VOLTAGE, \
285 .owner = THIS_MODULE, \
288 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
289 .ramp_delay = S2MPA01_RAMP_DELAY, \
290 .vsel_reg = S2MPA01_REG_B6CTRL2 + (num - 6) * 2, \
291 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
292 .enable_reg = S2MPA01_REG_B6CTRL1 + (num - 6) * 2, \
293 .enable_mask = S2MPA01_ENABLE_MASK \
296 static const struct regulator_desc regulators
[] = {
297 regulator_desc_ldo(1, STEP_25_MV
),
298 regulator_desc_ldo(2, STEP_50_MV
),
299 regulator_desc_ldo(3, STEP_50_MV
),
300 regulator_desc_ldo(4, STEP_50_MV
),
301 regulator_desc_ldo(5, STEP_25_MV
),
302 regulator_desc_ldo(6, STEP_25_MV
),
303 regulator_desc_ldo(7, STEP_50_MV
),
304 regulator_desc_ldo(8, STEP_50_MV
),
305 regulator_desc_ldo(9, STEP_50_MV
),
306 regulator_desc_ldo(10, STEP_50_MV
),
307 regulator_desc_ldo(11, STEP_50_MV
),
308 regulator_desc_ldo(12, STEP_50_MV
),
309 regulator_desc_ldo(13, STEP_50_MV
),
310 regulator_desc_ldo(14, STEP_50_MV
),
311 regulator_desc_ldo(15, STEP_50_MV
),
312 regulator_desc_ldo(16, STEP_50_MV
),
313 regulator_desc_ldo(17, STEP_50_MV
),
314 regulator_desc_ldo(18, STEP_50_MV
),
315 regulator_desc_ldo(19, STEP_50_MV
),
316 regulator_desc_ldo(20, STEP_50_MV
),
317 regulator_desc_ldo(21, STEP_50_MV
),
318 regulator_desc_ldo(22, STEP_50_MV
),
319 regulator_desc_ldo(23, STEP_50_MV
),
320 regulator_desc_ldo(24, STEP_50_MV
),
321 regulator_desc_ldo(25, STEP_50_MV
),
322 regulator_desc_ldo(26, STEP_25_MV
),
323 regulator_desc_buck1_4(1),
324 regulator_desc_buck1_4(2),
325 regulator_desc_buck1_4(3),
326 regulator_desc_buck1_4(4),
327 regulator_desc_buck5
,
328 regulator_desc_buck6_10(6, MIN_600_MV
, STEP_6_25_MV
),
329 regulator_desc_buck6_10(7, MIN_600_MV
, STEP_6_25_MV
),
330 regulator_desc_buck6_10(8, MIN_800_MV
, STEP_12_5_MV
),
331 regulator_desc_buck6_10(9, MIN_1500_MV
, STEP_12_5_MV
),
332 regulator_desc_buck6_10(10, MIN_1000_MV
, STEP_12_5_MV
),
335 static int s2mpa01_pmic_probe(struct platform_device
*pdev
)
337 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
338 struct sec_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
339 struct device_node
*reg_np
= NULL
;
340 struct regulator_config config
= { };
341 struct of_regulator_match
*rdata
;
342 struct s2mpa01_info
*s2mpa01
;
345 s2mpa01
= devm_kzalloc(&pdev
->dev
, sizeof(*s2mpa01
), GFP_KERNEL
);
349 rdata
= s2mpa01
->rdata
;
350 for (i
= 0; i
< S2MPA01_REGULATOR_CNT
; i
++)
351 rdata
[i
].name
= regulators
[i
].name
;
353 if (iodev
->dev
->of_node
) {
354 reg_np
= of_get_child_by_name(iodev
->dev
->of_node
,
358 "could not find regulators sub-node\n");
362 of_regulator_match(&pdev
->dev
, reg_np
, rdata
,
363 S2MPA01_REGULATOR_MAX
);
367 platform_set_drvdata(pdev
, s2mpa01
);
369 config
.dev
= &pdev
->dev
;
370 config
.regmap
= iodev
->regmap_pmic
;
371 config
.driver_data
= s2mpa01
;
373 for (i
= 0; i
< S2MPA01_REGULATOR_MAX
; i
++) {
374 struct regulator_dev
*rdev
;
376 config
.init_data
= pdata
->regulators
[i
].initdata
;
378 config
.init_data
= rdata
[i
].init_data
;
381 config
.of_node
= rdata
[i
].of_node
;
383 rdev
= devm_regulator_register(&pdev
->dev
,
384 ®ulators
[i
], &config
);
386 dev_err(&pdev
->dev
, "regulator init failed for %d\n",
388 return PTR_ERR(rdev
);
395 static const struct platform_device_id s2mpa01_pmic_id
[] = {
396 { "s2mpa01-pmic", 0},
399 MODULE_DEVICE_TABLE(platform
, s2mpa01_pmic_id
);
401 static struct platform_driver s2mpa01_pmic_driver
= {
403 .name
= "s2mpa01-pmic",
405 .probe
= s2mpa01_pmic_probe
,
406 .id_table
= s2mpa01_pmic_id
,
409 module_platform_driver(s2mpa01_pmic_driver
);
411 /* Module information */
412 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
413 MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
414 MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
415 MODULE_LICENSE("GPL");