1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd
4 // http://www.samsung.com
8 #include <linux/slab.h>
9 #include <linux/module.h>
11 #include <linux/regmap.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/machine.h>
15 #include <linux/regulator/of_regulator.h>
16 #include <linux/mfd/samsung/core.h>
17 #include <linux/mfd/samsung/s2mpa01.h>
28 static int get_ramp_delay(int ramp_delay
)
30 unsigned char cnt
= 0;
35 ramp_delay
= ramp_delay
>> 1;
47 static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
48 unsigned int old_selector
,
49 unsigned int new_selector
)
51 struct s2mpa01_info
*s2mpa01
= rdev_get_drvdata(rdev
);
52 unsigned int ramp_delay
= 0;
53 int old_volt
, new_volt
;
55 switch (rdev_get_id(rdev
)) {
58 ramp_delay
= s2mpa01
->ramp_delay24
;
61 ramp_delay
= s2mpa01
->ramp_delay3
;
64 ramp_delay
= s2mpa01
->ramp_delay5
;
68 ramp_delay
= s2mpa01
->ramp_delay16
;
71 ramp_delay
= s2mpa01
->ramp_delay7
;
76 ramp_delay
= s2mpa01
->ramp_delay8910
;
81 ramp_delay
= rdev
->desc
->ramp_delay
;
83 old_volt
= rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* old_selector
);
84 new_volt
= rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* new_selector
);
86 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
89 static int s2mpa01_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
91 struct s2mpa01_info
*s2mpa01
= rdev_get_drvdata(rdev
);
92 unsigned int ramp_val
, ramp_shift
, ramp_reg
= S2MPA01_REG_RAMP2
;
93 unsigned int ramp_enable
= 1, enable_shift
= 0;
96 switch (rdev_get_id(rdev
)) {
98 enable_shift
= S2MPA01_BUCK1_RAMP_EN_SHIFT
;
104 if (ramp_delay
> s2mpa01
->ramp_delay16
)
105 s2mpa01
->ramp_delay16
= ramp_delay
;
107 ramp_delay
= s2mpa01
->ramp_delay16
;
109 ramp_shift
= S2MPA01_BUCK16_RAMP_SHIFT
;
112 enable_shift
= S2MPA01_BUCK2_RAMP_EN_SHIFT
;
118 if (ramp_delay
> s2mpa01
->ramp_delay24
)
119 s2mpa01
->ramp_delay24
= ramp_delay
;
121 ramp_delay
= s2mpa01
->ramp_delay24
;
123 ramp_shift
= S2MPA01_BUCK24_RAMP_SHIFT
;
124 ramp_reg
= S2MPA01_REG_RAMP1
;
127 enable_shift
= S2MPA01_BUCK3_RAMP_EN_SHIFT
;
133 s2mpa01
->ramp_delay3
= ramp_delay
;
134 ramp_shift
= S2MPA01_BUCK3_RAMP_SHIFT
;
135 ramp_reg
= S2MPA01_REG_RAMP1
;
138 enable_shift
= S2MPA01_BUCK4_RAMP_EN_SHIFT
;
144 if (ramp_delay
> s2mpa01
->ramp_delay24
)
145 s2mpa01
->ramp_delay24
= ramp_delay
;
147 ramp_delay
= s2mpa01
->ramp_delay24
;
149 ramp_shift
= S2MPA01_BUCK24_RAMP_SHIFT
;
150 ramp_reg
= S2MPA01_REG_RAMP1
;
153 s2mpa01
->ramp_delay5
= ramp_delay
;
154 ramp_shift
= S2MPA01_BUCK5_RAMP_SHIFT
;
157 if (ramp_delay
> s2mpa01
->ramp_delay16
)
158 s2mpa01
->ramp_delay16
= ramp_delay
;
160 ramp_delay
= s2mpa01
->ramp_delay16
;
162 ramp_shift
= S2MPA01_BUCK16_RAMP_SHIFT
;
165 s2mpa01
->ramp_delay7
= ramp_delay
;
166 ramp_shift
= S2MPA01_BUCK7_RAMP_SHIFT
;
171 if (ramp_delay
> s2mpa01
->ramp_delay8910
)
172 s2mpa01
->ramp_delay8910
= ramp_delay
;
174 ramp_delay
= s2mpa01
->ramp_delay8910
;
176 ramp_shift
= S2MPA01_BUCK8910_RAMP_SHIFT
;
185 /* Ramp delay can be enabled/disabled only for buck[1234] */
186 if (rdev_get_id(rdev
) >= S2MPA01_BUCK1
&&
187 rdev_get_id(rdev
) <= S2MPA01_BUCK4
) {
188 ret
= regmap_update_bits(rdev
->regmap
, S2MPA01_REG_RAMP1
,
189 1 << enable_shift
, 1 << enable_shift
);
191 dev_err(&rdev
->dev
, "failed to enable ramp rate\n");
196 ramp_val
= get_ramp_delay(ramp_delay
);
198 return regmap_update_bits(rdev
->regmap
, ramp_reg
, 0x3 << ramp_shift
,
199 ramp_val
<< ramp_shift
);
202 return regmap_update_bits(rdev
->regmap
, S2MPA01_REG_RAMP1
,
203 1 << enable_shift
, 0);
206 static const struct regulator_ops s2mpa01_ldo_ops
= {
207 .list_voltage
= regulator_list_voltage_linear
,
208 .map_voltage
= regulator_map_voltage_linear
,
209 .is_enabled
= regulator_is_enabled_regmap
,
210 .enable
= regulator_enable_regmap
,
211 .disable
= regulator_disable_regmap
,
212 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
213 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
214 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
217 static const struct regulator_ops s2mpa01_buck_ops
= {
218 .list_voltage
= regulator_list_voltage_linear
,
219 .map_voltage
= regulator_map_voltage_linear
,
220 .is_enabled
= regulator_is_enabled_regmap
,
221 .enable
= regulator_enable_regmap
,
222 .disable
= regulator_disable_regmap
,
223 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
224 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
225 .set_voltage_time_sel
= s2mpa01_regulator_set_voltage_time_sel
,
226 .set_ramp_delay
= s2mpa01_set_ramp_delay
,
229 #define regulator_desc_ldo(num, step) { \
231 .of_match = of_match_ptr("LDO"#num), \
232 .regulators_node = of_match_ptr("regulators"), \
233 .id = S2MPA01_LDO##num, \
234 .ops = &s2mpa01_ldo_ops, \
235 .type = REGULATOR_VOLTAGE, \
236 .owner = THIS_MODULE, \
237 .min_uV = MIN_800_MV, \
239 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
240 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
241 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
242 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
243 .enable_mask = S2MPA01_ENABLE_MASK \
246 #define regulator_desc_buck1_4(num) { \
247 .name = "BUCK"#num, \
248 .of_match = of_match_ptr("BUCK"#num), \
249 .regulators_node = of_match_ptr("regulators"), \
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 .of_match = of_match_ptr("BUCK5"), \
267 .regulators_node = of_match_ptr("regulators"), \
268 .id = S2MPA01_BUCK5, \
269 .ops = &s2mpa01_buck_ops, \
270 .type = REGULATOR_VOLTAGE, \
271 .owner = THIS_MODULE, \
272 .min_uV = MIN_800_MV, \
273 .uV_step = STEP_6_25_MV, \
274 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
275 .ramp_delay = S2MPA01_RAMP_DELAY, \
276 .vsel_reg = S2MPA01_REG_B5CTRL2, \
277 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
278 .enable_reg = S2MPA01_REG_B5CTRL1, \
279 .enable_mask = S2MPA01_ENABLE_MASK \
282 #define regulator_desc_buck6_10(num, min, step) { \
283 .name = "BUCK"#num, \
284 .of_match = of_match_ptr("BUCK"#num), \
285 .regulators_node = of_match_ptr("regulators"), \
286 .id = S2MPA01_BUCK##num, \
287 .ops = &s2mpa01_buck_ops, \
288 .type = REGULATOR_VOLTAGE, \
289 .owner = THIS_MODULE, \
292 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
293 .ramp_delay = S2MPA01_RAMP_DELAY, \
294 .vsel_reg = S2MPA01_REG_B6CTRL2 + (num - 6) * 2, \
295 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
296 .enable_reg = S2MPA01_REG_B6CTRL1 + (num - 6) * 2, \
297 .enable_mask = S2MPA01_ENABLE_MASK \
300 static const struct regulator_desc regulators
[] = {
301 regulator_desc_ldo(1, STEP_25_MV
),
302 regulator_desc_ldo(2, STEP_50_MV
),
303 regulator_desc_ldo(3, STEP_50_MV
),
304 regulator_desc_ldo(4, STEP_50_MV
),
305 regulator_desc_ldo(5, STEP_25_MV
),
306 regulator_desc_ldo(6, STEP_25_MV
),
307 regulator_desc_ldo(7, STEP_50_MV
),
308 regulator_desc_ldo(8, STEP_50_MV
),
309 regulator_desc_ldo(9, STEP_50_MV
),
310 regulator_desc_ldo(10, STEP_50_MV
),
311 regulator_desc_ldo(11, STEP_50_MV
),
312 regulator_desc_ldo(12, STEP_50_MV
),
313 regulator_desc_ldo(13, STEP_50_MV
),
314 regulator_desc_ldo(14, STEP_50_MV
),
315 regulator_desc_ldo(15, STEP_50_MV
),
316 regulator_desc_ldo(16, STEP_50_MV
),
317 regulator_desc_ldo(17, STEP_50_MV
),
318 regulator_desc_ldo(18, STEP_50_MV
),
319 regulator_desc_ldo(19, STEP_50_MV
),
320 regulator_desc_ldo(20, STEP_50_MV
),
321 regulator_desc_ldo(21, STEP_50_MV
),
322 regulator_desc_ldo(22, STEP_50_MV
),
323 regulator_desc_ldo(23, STEP_50_MV
),
324 regulator_desc_ldo(24, STEP_50_MV
),
325 regulator_desc_ldo(25, STEP_50_MV
),
326 regulator_desc_ldo(26, STEP_25_MV
),
327 regulator_desc_buck1_4(1),
328 regulator_desc_buck1_4(2),
329 regulator_desc_buck1_4(3),
330 regulator_desc_buck1_4(4),
331 regulator_desc_buck5
,
332 regulator_desc_buck6_10(6, MIN_600_MV
, STEP_6_25_MV
),
333 regulator_desc_buck6_10(7, MIN_600_MV
, STEP_6_25_MV
),
334 regulator_desc_buck6_10(8, MIN_800_MV
, STEP_12_5_MV
),
335 regulator_desc_buck6_10(9, MIN_1500_MV
, STEP_12_5_MV
),
336 regulator_desc_buck6_10(10, MIN_1000_MV
, STEP_12_5_MV
),
339 static int s2mpa01_pmic_probe(struct platform_device
*pdev
)
341 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
342 struct regulator_config config
= { };
343 struct s2mpa01_info
*s2mpa01
;
346 s2mpa01
= devm_kzalloc(&pdev
->dev
, sizeof(*s2mpa01
), GFP_KERNEL
);
350 config
.dev
= iodev
->dev
;
351 config
.regmap
= iodev
->regmap_pmic
;
352 config
.driver_data
= s2mpa01
;
354 for (i
= 0; i
< S2MPA01_REGULATOR_MAX
; i
++) {
355 struct regulator_dev
*rdev
;
357 rdev
= devm_regulator_register(&pdev
->dev
,
358 ®ulators
[i
], &config
);
360 dev_err(&pdev
->dev
, "regulator init failed for %d\n",
362 return PTR_ERR(rdev
);
369 static const struct platform_device_id s2mpa01_pmic_id
[] = {
370 { "s2mpa01-pmic", 0},
373 MODULE_DEVICE_TABLE(platform
, s2mpa01_pmic_id
);
375 static struct platform_driver s2mpa01_pmic_driver
= {
377 .name
= "s2mpa01-pmic",
378 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
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");