1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) STMicroelectronics 2018
3 // Author: Pascal Paillet <p.paillet@st.com> for STMicroelectronics.
5 #include <linux/interrupt.h>
6 #include <linux/mfd/stpmic1.h>
7 #include <linux/module.h>
8 #include <linux/of_irq.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/driver.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/of_regulator.h>
16 * stpmic1 regulator description
17 * @desc: regulator framework description
18 * @mask_reset_reg: mask reset register address
19 * @mask_reset_mask: mask rank and mask reset register mask
20 * @icc_reg: icc register address
21 * @icc_mask: icc register mask
23 struct stpmic1_regulator_cfg {
24 struct regulator_desc desc;
32 * stpmic1 regulator data: this structure is used as driver data
33 * @regul_id: regulator id
34 * @reg_node: DT node of regulator (unused on non-DT platforms)
35 * @cfg: stpmic specific regulator description
36 * @mask_reset: mask_reset bit value
37 * @irq_curlim: current limit interrupt number
38 * @regmap: point to parent regmap structure
40 struct stpmic1_regulator {
41 unsigned int regul_id;
42 struct device_node *reg_node;
43 const struct stpmic1_regulator_cfg *cfg;
46 struct regmap *regmap;
49 static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode);
50 static unsigned int stpmic1_get_mode(struct regulator_dev *rdev);
51 static int stpmic1_set_icc(struct regulator_dev *rdev);
52 static int stpmic1_regulator_parse_dt(void *driver_data);
53 static unsigned int stpmic1_map_mode(unsigned int mode);
66 STPMIC1_VREF_DDR = 10,
68 STPMIC1_VBUS_OTG = 12,
72 /* Enable time worst case is 5000mV/(2250uV/uS) */
73 #define PMIC_ENABLE_TIME_US 2200
75 #define STPMIC1_BUCK_MODE_NORMAL 0
76 #define STPMIC1_BUCK_MODE_LP BUCK_HPLP_ENABLE_MASK
78 static const struct regulator_linear_range buck1_ranges[] = {
79 REGULATOR_LINEAR_RANGE(600000, 0, 30, 25000),
80 REGULATOR_LINEAR_RANGE(1350000, 31, 63, 0),
83 static const struct regulator_linear_range buck2_ranges[] = {
84 REGULATOR_LINEAR_RANGE(1000000, 0, 17, 0),
85 REGULATOR_LINEAR_RANGE(1050000, 18, 19, 0),
86 REGULATOR_LINEAR_RANGE(1100000, 20, 21, 0),
87 REGULATOR_LINEAR_RANGE(1150000, 22, 23, 0),
88 REGULATOR_LINEAR_RANGE(1200000, 24, 25, 0),
89 REGULATOR_LINEAR_RANGE(1250000, 26, 27, 0),
90 REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
91 REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
92 REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
93 REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
94 REGULATOR_LINEAR_RANGE(1500000, 36, 63, 0),
97 static const struct regulator_linear_range buck3_ranges[] = {
98 REGULATOR_LINEAR_RANGE(1000000, 0, 19, 0),
99 REGULATOR_LINEAR_RANGE(1100000, 20, 23, 0),
100 REGULATOR_LINEAR_RANGE(1200000, 24, 27, 0),
101 REGULATOR_LINEAR_RANGE(1300000, 28, 31, 0),
102 REGULATOR_LINEAR_RANGE(1400000, 32, 35, 0),
103 REGULATOR_LINEAR_RANGE(1500000, 36, 55, 100000),
104 REGULATOR_LINEAR_RANGE(3400000, 56, 63, 0),
107 static const struct regulator_linear_range buck4_ranges[] = {
108 REGULATOR_LINEAR_RANGE(600000, 0, 27, 25000),
109 REGULATOR_LINEAR_RANGE(1300000, 28, 29, 0),
110 REGULATOR_LINEAR_RANGE(1350000, 30, 31, 0),
111 REGULATOR_LINEAR_RANGE(1400000, 32, 33, 0),
112 REGULATOR_LINEAR_RANGE(1450000, 34, 35, 0),
113 REGULATOR_LINEAR_RANGE(1500000, 36, 60, 100000),
114 REGULATOR_LINEAR_RANGE(3900000, 61, 63, 0),
117 static const struct regulator_linear_range ldo1_ranges[] = {
118 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
119 REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
120 REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
123 static const struct regulator_linear_range ldo2_ranges[] = {
124 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
125 REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
126 REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
129 static const struct regulator_linear_range ldo3_ranges[] = {
130 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
131 REGULATOR_LINEAR_RANGE(1700000, 8, 24, 100000),
132 REGULATOR_LINEAR_RANGE(3300000, 25, 30, 0),
133 /* with index 31 LDO3 is in DDR mode */
134 REGULATOR_LINEAR_RANGE(500000, 31, 31, 0),
137 static const struct regulator_linear_range ldo5_ranges[] = {
138 REGULATOR_LINEAR_RANGE(1700000, 0, 7, 0),
139 REGULATOR_LINEAR_RANGE(1700000, 8, 30, 100000),
140 REGULATOR_LINEAR_RANGE(3900000, 31, 31, 0),
143 static const struct regulator_linear_range ldo6_ranges[] = {
144 REGULATOR_LINEAR_RANGE(900000, 0, 24, 100000),
145 REGULATOR_LINEAR_RANGE(3300000, 25, 31, 0),
148 static const struct regulator_ops stpmic1_ldo_ops = {
149 .list_voltage = regulator_list_voltage_linear_range,
150 .map_voltage = regulator_map_voltage_linear_range,
151 .is_enabled = regulator_is_enabled_regmap,
152 .enable = regulator_enable_regmap,
153 .disable = regulator_disable_regmap,
154 .get_voltage_sel = regulator_get_voltage_sel_regmap,
155 .set_voltage_sel = regulator_set_voltage_sel_regmap,
156 .set_pull_down = regulator_set_pull_down_regmap,
157 .set_over_current_protection = stpmic1_set_icc,
160 static const struct regulator_ops stpmic1_ldo3_ops = {
161 .list_voltage = regulator_list_voltage_linear_range,
162 .map_voltage = regulator_map_voltage_iterate,
163 .is_enabled = regulator_is_enabled_regmap,
164 .enable = regulator_enable_regmap,
165 .disable = regulator_disable_regmap,
166 .get_voltage_sel = regulator_get_voltage_sel_regmap,
167 .set_voltage_sel = regulator_set_voltage_sel_regmap,
168 .set_pull_down = regulator_set_pull_down_regmap,
169 .get_bypass = regulator_get_bypass_regmap,
170 .set_bypass = regulator_set_bypass_regmap,
171 .set_over_current_protection = stpmic1_set_icc,
174 static const struct regulator_ops stpmic1_ldo4_fixed_regul_ops = {
175 .is_enabled = regulator_is_enabled_regmap,
176 .enable = regulator_enable_regmap,
177 .disable = regulator_disable_regmap,
178 .set_pull_down = regulator_set_pull_down_regmap,
179 .set_over_current_protection = stpmic1_set_icc,
182 static const struct regulator_ops stpmic1_buck_ops = {
183 .list_voltage = regulator_list_voltage_linear_range,
184 .map_voltage = regulator_map_voltage_linear_range,
185 .is_enabled = regulator_is_enabled_regmap,
186 .enable = regulator_enable_regmap,
187 .disable = regulator_disable_regmap,
188 .get_voltage_sel = regulator_get_voltage_sel_regmap,
189 .set_voltage_sel = regulator_set_voltage_sel_regmap,
190 .set_pull_down = regulator_set_pull_down_regmap,
191 .set_mode = stpmic1_set_mode,
192 .get_mode = stpmic1_get_mode,
193 .set_over_current_protection = stpmic1_set_icc,
196 static const struct regulator_ops stpmic1_vref_ddr_ops = {
197 .is_enabled = regulator_is_enabled_regmap,
198 .enable = regulator_enable_regmap,
199 .disable = regulator_disable_regmap,
200 .set_pull_down = regulator_set_pull_down_regmap,
203 static const struct regulator_ops stpmic1_switch_regul_ops = {
204 .is_enabled = regulator_is_enabled_regmap,
205 .enable = regulator_enable_regmap,
206 .disable = regulator_disable_regmap,
207 .set_over_current_protection = stpmic1_set_icc,
210 #define REG_LDO(ids, base) { \
212 .id = STPMIC1_##ids, \
214 .ops = &stpmic1_ldo_ops, \
215 .linear_ranges = base ## _ranges, \
216 .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
217 .type = REGULATOR_VOLTAGE, \
218 .owner = THIS_MODULE, \
219 .vsel_reg = ids##_ACTIVE_CR, \
220 .vsel_mask = LDO_VOLTAGE_MASK, \
221 .enable_reg = ids##_ACTIVE_CR, \
222 .enable_mask = LDO_ENABLE_MASK, \
225 .enable_time = PMIC_ENABLE_TIME_US, \
226 .pull_down_reg = ids##_PULL_DOWN_REG, \
227 .pull_down_mask = ids##_PULL_DOWN_MASK, \
228 .supply_name = #base, \
231 #define REG_LDO3(ids, base) { \
233 .id = STPMIC1_##ids, \
235 .ops = &stpmic1_ldo3_ops, \
236 .linear_ranges = ldo3_ranges, \
237 .n_linear_ranges = ARRAY_SIZE(ldo3_ranges), \
238 .type = REGULATOR_VOLTAGE, \
239 .owner = THIS_MODULE, \
240 .vsel_reg = LDO3_ACTIVE_CR, \
241 .vsel_mask = LDO_VOLTAGE_MASK, \
242 .enable_reg = LDO3_ACTIVE_CR, \
243 .enable_mask = LDO_ENABLE_MASK, \
246 .enable_time = PMIC_ENABLE_TIME_US, \
247 .bypass_reg = LDO3_ACTIVE_CR, \
248 .bypass_mask = LDO_BYPASS_MASK, \
249 .bypass_val_on = LDO_BYPASS_MASK, \
250 .bypass_val_off = 0, \
251 .pull_down_reg = ids##_PULL_DOWN_REG, \
252 .pull_down_mask = ids##_PULL_DOWN_MASK, \
253 .supply_name = #base, \
256 #define REG_LDO4(ids, base) { \
258 .id = STPMIC1_##ids, \
260 .ops = &stpmic1_ldo4_fixed_regul_ops, \
261 .type = REGULATOR_VOLTAGE, \
262 .owner = THIS_MODULE, \
264 .fixed_uV = 3300000, \
265 .enable_reg = LDO4_ACTIVE_CR, \
266 .enable_mask = LDO_ENABLE_MASK, \
269 .enable_time = PMIC_ENABLE_TIME_US, \
270 .pull_down_reg = ids##_PULL_DOWN_REG, \
271 .pull_down_mask = ids##_PULL_DOWN_MASK, \
272 .supply_name = #base, \
275 #define REG_BUCK(ids, base) { \
277 .id = STPMIC1_##ids, \
278 .ops = &stpmic1_buck_ops, \
280 .linear_ranges = base ## _ranges, \
281 .n_linear_ranges = ARRAY_SIZE(base ## _ranges), \
282 .type = REGULATOR_VOLTAGE, \
283 .owner = THIS_MODULE, \
284 .vsel_reg = ids##_ACTIVE_CR, \
285 .vsel_mask = BUCK_VOLTAGE_MASK, \
286 .enable_reg = ids##_ACTIVE_CR, \
287 .enable_mask = BUCK_ENABLE_MASK, \
290 .enable_time = PMIC_ENABLE_TIME_US, \
291 .of_map_mode = stpmic1_map_mode, \
292 .pull_down_reg = ids##_PULL_DOWN_REG, \
293 .pull_down_mask = ids##_PULL_DOWN_MASK, \
294 .supply_name = #base, \
297 #define REG_VREF_DDR(ids, base) { \
299 .id = STPMIC1_##ids, \
301 .ops = &stpmic1_vref_ddr_ops, \
302 .type = REGULATOR_VOLTAGE, \
303 .owner = THIS_MODULE, \
305 .fixed_uV = 500000, \
306 .enable_reg = VREF_DDR_ACTIVE_CR, \
307 .enable_mask = BUCK_ENABLE_MASK, \
310 .enable_time = PMIC_ENABLE_TIME_US, \
311 .pull_down_reg = ids##_PULL_DOWN_REG, \
312 .pull_down_mask = ids##_PULL_DOWN_MASK, \
313 .supply_name = #base, \
316 #define REG_SWITCH(ids, base, reg, mask, val) { \
318 .id = STPMIC1_##ids, \
320 .ops = &stpmic1_switch_regul_ops, \
321 .type = REGULATOR_VOLTAGE, \
322 .owner = THIS_MODULE, \
324 .fixed_uV = 5000000, \
325 .enable_reg = (reg), \
326 .enable_mask = (mask), \
327 .enable_val = (val), \
329 .enable_time = PMIC_ENABLE_TIME_US, \
330 .supply_name = #base, \
333 static const struct stpmic1_regulator_cfg stpmic1_regulator_cfgs[] = {
335 .desc = REG_BUCK(BUCK1, buck1),
336 .icc_reg = BUCKS_ICCTO_CR,
338 .mask_reset_reg = BUCKS_MASK_RESET_CR,
339 .mask_reset_mask = BIT(0),
342 .desc = REG_BUCK(BUCK2, buck2),
343 .icc_reg = BUCKS_ICCTO_CR,
345 .mask_reset_reg = BUCKS_MASK_RESET_CR,
346 .mask_reset_mask = BIT(1),
349 .desc = REG_BUCK(BUCK3, buck3),
350 .icc_reg = BUCKS_ICCTO_CR,
352 .mask_reset_reg = BUCKS_MASK_RESET_CR,
353 .mask_reset_mask = BIT(2),
356 .desc = REG_BUCK(BUCK4, buck4),
357 .icc_reg = BUCKS_ICCTO_CR,
359 .mask_reset_reg = BUCKS_MASK_RESET_CR,
360 .mask_reset_mask = BIT(3),
363 .desc = REG_LDO(LDO1, ldo1),
364 .icc_reg = LDOS_ICCTO_CR,
366 .mask_reset_reg = LDOS_MASK_RESET_CR,
367 .mask_reset_mask = BIT(0),
370 .desc = REG_LDO(LDO2, ldo2),
371 .icc_reg = LDOS_ICCTO_CR,
373 .mask_reset_reg = LDOS_MASK_RESET_CR,
374 .mask_reset_mask = BIT(1),
377 .desc = REG_LDO3(LDO3, ldo3),
378 .icc_reg = LDOS_ICCTO_CR,
380 .mask_reset_reg = LDOS_MASK_RESET_CR,
381 .mask_reset_mask = BIT(2),
384 .desc = REG_LDO4(LDO4, ldo4),
385 .icc_reg = LDOS_ICCTO_CR,
387 .mask_reset_reg = LDOS_MASK_RESET_CR,
388 .mask_reset_mask = BIT(3),
391 .desc = REG_LDO(LDO5, ldo5),
392 .icc_reg = LDOS_ICCTO_CR,
394 .mask_reset_reg = LDOS_MASK_RESET_CR,
395 .mask_reset_mask = BIT(4),
398 .desc = REG_LDO(LDO6, ldo6),
399 .icc_reg = LDOS_ICCTO_CR,
401 .mask_reset_reg = LDOS_MASK_RESET_CR,
402 .mask_reset_mask = BIT(5),
404 [STPMIC1_VREF_DDR] = {
405 .desc = REG_VREF_DDR(VREF_DDR, vref_ddr),
406 .mask_reset_reg = LDOS_MASK_RESET_CR,
407 .mask_reset_mask = BIT(6),
410 .desc = REG_SWITCH(BOOST, boost, BST_SW_CR,
413 .icc_reg = BUCKS_ICCTO_CR,
416 [STPMIC1_VBUS_OTG] = {
417 .desc = REG_SWITCH(VBUS_OTG, pwr_sw1, BST_SW_CR,
418 USBSW_OTG_SWITCH_ENABLED,
419 USBSW_OTG_SWITCH_ENABLED),
420 .icc_reg = BUCKS_ICCTO_CR,
424 .desc = REG_SWITCH(SW_OUT, pwr_sw2, BST_SW_CR,
427 .icc_reg = BUCKS_ICCTO_CR,
432 static unsigned int stpmic1_map_mode(unsigned int mode)
435 case STPMIC1_BUCK_MODE_NORMAL:
436 return REGULATOR_MODE_NORMAL;
437 case STPMIC1_BUCK_MODE_LP:
438 return REGULATOR_MODE_STANDBY;
440 return REGULATOR_MODE_INVALID;
444 static unsigned int stpmic1_get_mode(struct regulator_dev *rdev)
448 regmap_read(rdev->regmap, rdev->desc->enable_reg, &value);
450 if (value & STPMIC1_BUCK_MODE_LP)
451 return REGULATOR_MODE_STANDBY;
453 return REGULATOR_MODE_NORMAL;
456 static int stpmic1_set_mode(struct regulator_dev *rdev, unsigned int mode)
461 case REGULATOR_MODE_NORMAL:
462 value = STPMIC1_BUCK_MODE_NORMAL;
464 case REGULATOR_MODE_STANDBY:
465 value = STPMIC1_BUCK_MODE_LP;
471 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
472 STPMIC1_BUCK_MODE_LP, value);
475 static int stpmic1_set_icc(struct regulator_dev *rdev)
477 struct stpmic1_regulator *regul = rdev_get_drvdata(rdev);
479 /* enable switch off in case of over current */
480 return regmap_update_bits(regul->regmap, regul->cfg->icc_reg,
481 regul->cfg->icc_mask, regul->cfg->icc_mask);
484 static irqreturn_t stpmic1_curlim_irq_handler(int irq, void *data)
486 struct regulator_dev *rdev = (struct regulator_dev *)data;
488 regulator_lock(rdev);
490 /* Send an overcurrent notification */
491 regulator_notifier_call_chain(rdev,
492 REGULATOR_EVENT_OVER_CURRENT,
495 regulator_unlock(rdev);
500 static int stpmic1_regulator_init(struct platform_device *pdev,
501 struct regulator_dev *rdev)
503 struct stpmic1_regulator *regul = rdev_get_drvdata(rdev);
507 if (regul->mask_reset && regul->cfg->mask_reset_reg != 0) {
508 ret = regmap_update_bits(regul->regmap,
509 regul->cfg->mask_reset_reg,
510 regul->cfg->mask_reset_mask,
511 regul->cfg->mask_reset_mask);
513 dev_err(&pdev->dev, "set mask reset failed\n");
518 /* setup an irq handler for over-current detection */
519 if (regul->irq_curlim > 0) {
520 ret = devm_request_threaded_irq(&pdev->dev,
521 regul->irq_curlim, NULL,
522 stpmic1_curlim_irq_handler,
523 IRQF_ONESHOT | IRQF_SHARED,
526 dev_err(&pdev->dev, "Request IRQ failed\n");
533 #define MATCH(_name, _id) \
534 [STPMIC1_##_id] = { \
536 .desc = &stpmic1_regulator_cfgs[STPMIC1_##_id].desc, \
539 static struct of_regulator_match stpmic1_regulators_matches[] = {
550 MATCH(vref_ddr, VREF_DDR),
552 MATCH(pwr_sw1, VBUS_OTG),
553 MATCH(pwr_sw2, SW_OUT),
556 static int stpmic1_regulator_parse_dt(void *driver_data)
558 struct stpmic1_regulator *regul =
559 (struct stpmic1_regulator *)driver_data;
564 if (of_get_property(regul->reg_node, "st,mask-reset", NULL))
565 regul->mask_reset = 1;
567 regul->irq_curlim = of_irq_get(regul->reg_node, 0);
573 regulator_dev *stpmic1_regulator_register(struct platform_device *pdev, int id,
574 struct regulator_init_data *init_data,
575 struct stpmic1_regulator *regul)
577 struct stpmic1 *pmic_dev = dev_get_drvdata(pdev->dev.parent);
578 struct regulator_dev *rdev;
579 struct regulator_config config = {};
581 config.dev = &pdev->dev;
582 config.init_data = init_data;
583 config.of_node = stpmic1_regulators_matches[id].of_node;
584 config.regmap = pmic_dev->regmap;
585 config.driver_data = regul;
587 regul->regul_id = id;
588 regul->reg_node = config.of_node;
589 regul->cfg = &stpmic1_regulator_cfgs[id];
590 regul->regmap = pmic_dev->regmap;
592 rdev = devm_regulator_register(&pdev->dev, ®ul->cfg->desc, &config);
594 dev_err(&pdev->dev, "failed to register %s regulator\n",
595 regul->cfg->desc.name);
601 static int stpmic1_regulator_probe(struct platform_device *pdev)
603 struct regulator_dev *rdev;
604 struct stpmic1_regulator *regul;
605 struct regulator_init_data *init_data;
606 struct device_node *np;
609 np = pdev->dev.of_node;
611 ret = of_regulator_match(&pdev->dev, np,
612 stpmic1_regulators_matches,
613 ARRAY_SIZE(stpmic1_regulators_matches));
616 "Error in PMIC regulator device tree node");
620 regul = devm_kzalloc(&pdev->dev, ARRAY_SIZE(stpmic1_regulator_cfgs) *
621 sizeof(struct stpmic1_regulator),
626 for (i = 0; i < ARRAY_SIZE(stpmic1_regulator_cfgs); i++) {
627 /* Parse DT & find regulators to register */
628 init_data = stpmic1_regulators_matches[i].init_data;
630 init_data->regulator_init = &stpmic1_regulator_parse_dt;
632 rdev = stpmic1_regulator_register(pdev, i, init_data, regul);
634 return PTR_ERR(rdev);
636 ret = stpmic1_regulator_init(pdev, rdev);
639 "failed to initialize regulator %d\n", ret);
646 dev_dbg(&pdev->dev, "stpmic1_regulator driver probed\n");
651 static const struct of_device_id of_pmic_regulator_match[] = {
652 { .compatible = "st,stpmic1-regulators" },
656 MODULE_DEVICE_TABLE(of, of_pmic_regulator_match);
658 static struct platform_driver stpmic1_regulator_driver = {
660 .name = "stpmic1-regulator",
661 .of_match_table = of_match_ptr(of_pmic_regulator_match),
663 .probe = stpmic1_regulator_probe,
666 module_platform_driver(stpmic1_regulator_driver);
668 MODULE_DESCRIPTION("STPMIC1 PMIC voltage regulator driver");
669 MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>");
670 MODULE_LICENSE("GPL v2");